知道该记录是否显示了3条记录,如果是,则将另外3条记录放在新行中


Know if the record has been displayed 3 records and if so then put the another 3 records in a new line

好的,我有这个代码:

<?
$name=$_POST['name'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM items WHERE id='$name'");
$ss = ""
while($row = mysql_fetch_array($result))
{   
    $ss .= "<div style='border:1px solid red; float:left; width:100px;'><img src="Images/media'.$row['name'].'" />";
    $ss .= "<p>".$row['title']."</p>";
    $ss .= "<p>".$row['description']."</p>";
    $ss .= "<a href='".$row['link']."'>".$row['link']."</a></div>";
}
mysql_close($con);
?>
<? echo $ss; ?>

现在,我想组织显示,因此,我希望记录由3设置,但我遇到的唯一问题是我不知道如何使其按3设置显示。我愿意接受建议,请帮帮我。谢谢。

一个函数

function sqlArr($sql){
  $ret = array();
  $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
  if ($res) {
    while($row = mysql_fetch_array($res)){
      $ret[] = $row;
    }
  }
  return $ret;
}

一个代码

mysql_connect("localhost","root","");
mysql_select_db("juliver");
$name = mysql_real_escape_string($_POST['name']);
$data = sqlArr("SELECT * FROM items WHERE id='$name'");
$data = array_chunk($data,3);
include 'template.tpl.php';

模板

<table border='1'>
<? foreach ($data as $row): ?>
  <tr>
<?   foreach ($row as $cell): ?>
    <td><?=$cell['id']?><?=$cell['title']?></td>
<?   endforeach ?>
  </tr>
<? endforeach ?>
</table>

在while循环中,您可以设置条件来知道它何时执行3次,然后它将自动转到另一行。看看

int $a=0;
    while($row = mysql_fetch_array($result))
    {
        if($a%3==0){
    this will executes only if when the no of record dividable by 3. means 3,6,9,12..
                do this..
    }else {
     do this..
    }
    $a++;
    }