我必须在一个表中显示SQL记录(以divs显示),在该表中,我只能在一行中显示2条记录。记录的总数(即n)不是固定的,也不是数组。行数将取决于可用的记录数。我尝试了几种方法来定义“ define('TABLE_COLS',2);”。并在我的while循环中使用for循环,但它们没有给我所需的结果。我正在处理的代码如下:
echo "<table>";
$sql = "Some SQL";
$result = mysql_query($sql);
$num = mysql_numrows($result);
$i = 0;
define('TABLE_COLS', 2);
echo "<tr>";
while ($i < $num) {
// for($i=0; $i<=2; $i++){
$name = mysql_result($result,$i,"name");
$Count = mysql_result($result,$i,"count");
// echo "<tr>";
echo "<td>";
echo "<div>";
echo "<div>";
echo "$name";
echo "</div>";
echo "<div>";
echo "$Count";
echo "</div>";
echo "</div>";
echo "</td>";
// echo "</tr>";
$i++;
}
echo "</tr>";
mysql_free_result($result);
echo "</table>";
我要查找的结果是:现在,我必须添加相应事件ID的计数值,并将其显示在结果表中,如下所示。我是PHP的新手,请帮助我。任何评论,不胜感激。
+------------+ +--------------+
| Abby Bory | | Manny Mua |
+------------+ +--------------+
| 4 | | 22 |
+------------+ +--------------+
+------------+ +--------------+
| Senyz Dory| | Kory Bua |
+------------+ +--------------+
| 8 | | 27 |
+------------+ +--------------+
您可以尝试使用此帮助来解决您的问题。我将声明多个变量以使事情更易于管理,例如更改每行的列。给定的代码应适应每行任意数量的列。让我知道这是否是您所需要的。另外,请考虑使用mysqli
代替mysql
。
更新1:包括样本数据库格式以标准化格式
database.php
<?php
$db_servername = "DATABASE SERVER NAME";
$db_username = "DATABASE USERNAME";
$db_password = "DATABASE PASSWORD";
$db_database = "DATABASE TABLE";
// Create connection
$con = new mysqli($db_servername, $db_username, $db_password, $db_database);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
范例程式码
require "database.php";
global $con;
echo "<table>";
$sql = "SOME SQL";
$result = mysqli_query($con, $sql);
// define number of columns per row
$num_col = 2;
// calculate total number of row based on columns per row defined
$total_row = intVal(mysqli_num_rows($result) / $num_col);
// calculate left over result if the division result is not perfect number
$left_over = mysqli_num_rows($result) % $num_col;
$counter = 0;
while($counter < $total_row){
echo '<tr>';
for($i = 0; $i < $num_col; $i++){
$row = mysqli_fetch_array($result);
echo "<td>";
echo "<div>".$row["name"]."</div>";
echo "<div>".$row["count"]."</div>";
echo "</td>";
}
echo '</tr>';
$counter++;
}
echo "<tr>";
while($left_over > 0 ){
$row = mysqli_fetch_array($result);
echo "<td>";
echo "<div>".$row["name"]."</div>";
echo "<div>".$row["count"]."</div>";
echo "</td>";
$left_over--;
}
echo "</tr>";
echo "</table>";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句