1

好的,我有这个代码:

<?
$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 组显示。我愿意提出建议,请帮我。谢谢你。

4

2 回答 2

0

一个函数

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>
于 2012-04-04T15:26:20.433 回答
0

在 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++;
    }
于 2012-04-04T15:33:02.037 回答