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

时间:2013-11-15 21:35:27

标签: php

我有一张表,我希望在开始下一行之前显示5列或5个单元格。 示例

如果我有25条记录,它们将显示在5列宽5行而不是25行的表中。

以下是我的代码。

<?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 个答案:

答案 0 :(得分:1)

您可以设置一个计数器,并在每次循环时递增它,这样您就可以关闭该行并在添加了5列时开始一个新的 - 所以使用上面的代码,减少以简化示例,它看起来像:

$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>';

如果你不能保证你总是有25个结果,或者是5的另一个倍数,你会想要在循环之后添加一些东西来添加完成最后一行所需的任何额外的空单元格。