gpt4 book ai didi

php - 如果行值类似于第一行,则如何自动递增 1 并附加小数,否则为 2

转载 作者:行者123 更新时间:2023-11-29 12:26:11 24 4
gpt4 key购买 nike

我有这个代码来生成连接两个表的报告。

$agebracket= $_POST['age_bracket'];   //coming from dropdown list

$sql=mysql_query("select qshortcode from question_details where pans1 like '%$agebracket%'");
while($row=mysql_fetch_array($sql)){

$utility[] = preg_replace('/[0-9]+/','',$row['qshortcode']);
}
$slno=0;
for($i=0;$i<count($utility);$i++) {
$last=""; //as suggested
$check=mysql_query("select q.qshortcode,q.qcode,q.question,p.paid_rev from question_details q left outer join paid_response p on q.qcode=p.qno where q.qshortcode like '$utility[$i]%' group by q.qcode");
while($qscode=mysql_fetch_array($check)) {

if($last =substr($qscode['qcode'],strlen($last))) {
$last = $qscode['qcode'];
$slno++;
$slid = $slno ;
} else {
$slid .= ".1";
}

echo "<tr>";
echo "<td>".$slid.' '."</td>";
echo "<td>".$qscode['qshortcode'].' '."</td>";
echo "<td>".$qscode['qcode'].' '."</td>";
echo "<td>".$qscode['question'].' '."</td>";
echo "<td>".$qscode['paid_rev']."<br></td>";
echo "</tr>";
}
}
echo "</table>";

这会生成此报告,该报告从带有左外连接的两个表中获取,还获得空白收入

SL-No   Qcode     Question    Revenue
1 test whatever
1.1 test_1 zzz
2 test_2 xxx something //here it is becoming 2 but it should be 1.1.1
3 abc vvv
3.1 abc1 khkhk something

现在实际上我想用 1 自动递增 SL-No 那么如果 qcode 与第一行 qcode 匹配,它将是 1.1然后 1.1.1 然后当它不匹配时它将是 2。就像报告一样

SL-No      Qcode      Question    Revenue
1 test whatever
1.1 test_1 zzz
1.1.1 test_2 xxx something
2 abc vvv
2.1 abc1 khkhk something

最佳答案

我会做这样的事情(未经测试的代码):

$slno=0;
[...] // for loop omitted, just starting at 0 instead of 1, to increment properly after.
$last="-1"; // To avoid comparing two empty lines at first pass, which will obvisouly be alway true, sorry.
while ([mysql query ....]) {
if ($last != substr($qscode['qcode'],0, strlen($last))) {
$last = $qscode['qcode'];
$slno++;
$slid = strval($slno)
} else {
$slid .= ".1"
}

echo "<tr>";
echo "<td>".$slid.' '."</td>";
echo "<td>".$qscode['qshortcode'].' '."</td>";
[...] // rest omitted
}

关于php - 如果行值类似于第一行,则如何自动递增 1 并附加小数,否则为 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28275614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com