0

我正在使用基于用户搜索的 PHP 从 MySQL 中检索结果

我想在结果中每 2 秒后自动添加一个

<?php
include_once("config.php");
isset( $_REQUEST['name'] ) ? $name=$_REQUEST['name'] : $name='';
$name = mysql_real_escape_string( $name );
if (strlen($name) >= 4) {

     $sql = "select * from places where speciality like '%$name%'";

     $rs = mysql_query( $sql ) or die('Database Error: ' . mysql_error
    $num = mysql_num_rows( $rs );

    if($num >= 1 ){     
        echo "<table id='result-table'><tr>";           
while($row = mysql_fetch_array( $rs )){

    echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s

 }

    }else{
        echo "no records found</table>";
    }
}
?>
4

3 回答 3

2
echo "<table id='result-table'><tr>";
$currentCount = -1;
while($row = mysql_fetch_array($rs))
{
    echo "<td>row[cname]</td>";
    $currentCount = ($currentCount + 1) % 2;
    if($currentCount == 1)
    {
        echo '</tr><tr>';
    }
}
于 2013-07-29T11:27:51.810 回答
2

使用$i = 1; if ($i % 2 == 0)$i++;

<?php 
include_once ("config.php");
isset ($_REQUEST['name']) ? $name = $_REQUEST['name'] : $name = '';
$name = mysql_real_escape_string($name);
if (strlen($name) >= 4) {
  $sql = "select * from places where speciality like '%$name%'";
  $rs = mysql_query($sql) or die('Database Error: '.mysql_error $num = mysql_num_rows($rs);
  if ($num >= 1) {
    $i = 1;
    echo "<table id='result-table'><tr>";
    while ($row = mysql_fetch_array($rs)) {
      echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s
      if ($i % 2 == 0)
        echo "</tr><tr>";
      $i++;
    }
  }
  else {
    echo "no records found</table>";
  }
}
?>
于 2013-07-29T11:29:43.437 回答
0

我建议根本不要把它们放在桌子上;而是简单地将它们放入<div>元素中,带有float:leftwidth:50%样式。

如果您必须将它们放入一个表中,并且您想按照您要求的方式进行操作,您可以使用 PHP 的模运算符 ( %) 将当前记录号除以 2 并获得余数。如果是 1,则添加<tr>标签:

if(++$rowNum % 2) {print "</tr><tr>";}
于 2013-07-29T11:30:30.790 回答