gpt4 book ai didi

带有 MySQL 查询表的 PHP 函数

转载 作者:行者123 更新时间:2023-11-30 23:03:21 24 4
gpt4 key购买 nike

我有一个表,其中填充了 MySQL 查询的结果。我需要在表中插入一个额外的列作为 PHP 函数,它根据查询中字段的值生成一个字符串(我认为这就是需要解决的问题)。这可能吗?我试图将列设置为变量并在函数中调用该变量(在此代码中未设置为用户定义的函数),但我一定做错了什么,因为根本没有填充表。

$query1 = "SELECT `rank`.`term`,
`rank`.`acadplan`,
`rank`.`projlvl`, `rank`.`gpa`,
(SELECT COUNT(gpa) FROM `rank1`
WHERE `rank1`.`term` = '{$term}'
AND `rank1`.`acadplan`='{$acadplan}'
AND `rank1`.`projlvl`='{$projlvl}'
AND `rank1`.`gpa` > `rank`.`gpa`) + 1 AS 'ranking',
FROM `rank`
WHERE `rank`.`term` ='{$term}'
AND `rank`.`acadplan`='{$acadplan}'
AND `rank`.`projlvl`='{$projlvl}'
ORDER BY `rank`.`gpa` DESC";


$result = mysqli_query($query1);
echo "<table>";
echo "<th>Term</th>";
echo "<th>Plan</th>";
echo "<th>Level</th>";
echo "<th>GPA</th>";
echo "<th>Rank</th>";
echo "<th>NEWCOL</th>";
echo "<col width = '125'>";
echo "<col width = '125'>";
echo "<col width = '125'>";
echo "<col width = '125'>";
echo "<col width = '125'>";
echo "<col width = '250'>";


$num_rows = mysqli_num_rows($result);

这是需要执行的函数:

if ($num_rows <=24) {
switch ($num_rows) {
case ($rank >=1 and $rank <=3):
echo "1-3";
break;
case ($rank >=4 and $rank <=6):
echo "4-6";
break;
case ($rank >=7 and $rank <=9):
echo "7-9";
break;
case ($rank >=10 and $rank <=12):
echo "10-12";
break;
case ($rank >=13 and $rank <=15):
echo "13-15";
break;
case ($rank >=16 and $rank <=18):
echo "16-18";
break;
case ($rank >=19 and $rank <=21):
echo "19-21";
break;
case ($rank >=22 and $rank <=24):
echo "22-24";
break;
}
} elseif ($num_rows >=25 and $num_rows <=50) {
echo "greater than 24";
} elseif ($num_rows >=51 and $num_rows <=100) {
echo "greater than 50";
} elseif ($num_rows >=101 and $num_rows <=150) {
echo "greater than 100";
} elseif ($num_rows >=151 and $num_rows <=260) {
echo "greater than 150";
}
}

这是表格:

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['term'] . "</td>";
echo "<td>" . $row['acadplan'] . "</td>";
echo "<td>" . $row['projlvl'] . "</td>";
echo "<td>" . $row['gpa'] . "</td>";
echo "<td>" . $row['ranking'] . "</td>";
echo "</tr>";
}
echo "</table>";

最佳答案

像这样的解决方案怎么样:

 function someFunction($rank, $num_rows) {
if ($num_rows <=24) {
switch (true) {
case ($rank >= 1 AND $rank <=3):
return "1-3";
case ($rank <=6):
return "4-6";
case ($rank <=9):
return "7-9";
case ($rank <=12):
return "10-12";
case ($rank <=15):
return "13-15";
case ($rank <=18):
return "16-18";
case ($rank <=21):
return "19-21";
case ($rank <=24):
return "22-24";
}
} elseif ($num_rows >=25 and $num_rows <=50) {
return "greater than 24";
} elseif ($num_rows >=51 and $num_rows <=100) {
return "greater than 50";
} elseif ($num_rows >=101 and $num_rows <=150) {
return "greater than 100";
} elseif ($num_rows >=151 and $num_rows <=260) {
return "greater than 150";
}
return "";
}

然后是表格:

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['term'] . "</td>";
echo "<td>" . $row['acadplan'] . "</td>";
echo "<td>" . $row['projlvl'] . "</td>";
echo "<td>" . $row['gpa'] . "</td>";
echo "<td>" . $row['ranking'] . "</td>";
echo "<td>" . someFunction($row['ranking'], $num_rows) . "</td>";
echo "</tr>";
}
echo "</table>";

编辑:您可以像这样在 HTML 标记中使用 PHP:

<td><?php echo $row['ranking']; ?></td>

我发现在我的 View 中使用 PHP 值是更可取的方式。当然,如果可能的话,严格的业务逻辑应该分开,不要放在 HTML 标签中。

关于带有 MySQL 查询表的 PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22941190/

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