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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题