douzhi1879 2013-11-15 21:35
浏览 39
已采纳

在列中显示单元格,然后在PHP中显示行

I have a table that I would like to display 5 columns or 5 cells across before starting the next row. Example

If I had 25 records they would be displayed in a table 5 columns wide by 5 rows long instead of 25 rows.

Below is my code.

<?php require_once('../../Connections/rec.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",  $theNotDefinedValue = "") 
{
 if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

mysql_select_db($database_rec, $rec);
$query_Teams = "SELECT * FROM rec_manager";
$Teams = mysql_query($query_Teams, $rec) or die(mysql_error());
$row_Teams = mysql_fetch_assoc($Teams);
$totalRows_Teams = mysql_num_rows($Teams);
?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
body {
background-color: #000;
}
.Manager {
color: #FFF;
text-align: center;
text-decoration:none
}
a:link
{
color:white;
text-decoration:none

}
</style>
</head>

<body>
<table border="0">

 <?php do { ?>
 <tr>
   <td><img src="<?php echo $row_Teams['Team']; ?>" width="138" height="92" /></td>
</tr> 
 <tr class="Manager">
   <td><a href="mailto:<?php echo $row_Teams['Email']; ?>"><?php echo $row_Teams['First_Name']; ?> <?php echo $row_Teams['Last_Name']; ?></a></td>
 </tr>
 <?php } while ($row_Teams = mysql_fetch_assoc($Teams)); ?>
 </table>
</body>
</html>
<?php
mysql_free_result($Teams);
?>
  • 写回答

1条回答 默认 最新

  • duanheye7423 2013-11-16 00:49
    关注

    You could set a counter and increment it each time through the while loop so you can then close off the row and start a new one when 5 columns have been added - so with your code from above, cut down to simplify the example, it would look something like:

    $column_count = 1;
    echo '<tr>';
    do {
    
        echo '<td>'.$row_Teams['Team'].'</td>';
    
        if ($column_count < 5) {   
    
            $column_count++;
    
        } else {
    
            echo '</tr><tr>';
            $column_count = 1;
       }
    
    } while ($row_Teams = mysql_fetch_assoc($Teams));
    
    echo '</tr>';
    

    If you can't guarantee that you will always have 25 results, or another multiple of 5, you would want to add something after the loop to add in any extra empty cells needed to complete the last row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?