在php中重复num行的结果

时间:2012-10-17 05:56:38

标签: php while-loop repeat mysql-num-rows

  

可能重复:
  i can’t make num rows inside while looping

我尝试分配在循环中生成num行以获取另一个表的行数

$select_sub_cat = mysql_query("SELECT * FROM sub_cat WHERE ct_id='".$row_main['id']."' LIMIT 8 ");
while($row_sub = mysql_fetch_array($select_sub_cat))
{
  $select_num_sub = mysql_query("SELECT * FROM market WHERE sub_cat='".$row_sub['id']."'  ");
  while($row_num_sub = mysql_fetch_array($select_num_sub))
  {
    $n = mysql_num_rows($select_num_sub);
    $smarty->assign('n',$n);
  }

 $sub_cats[] = $row_sub;
}
$smarty->assign('sub_cats',$sub_cats);

2 个答案:

答案 0 :(得分:1)

这句话毫无意义:

$n = mysql_num_rows($select_num_sub);
$smarty->assign('n',$n);

内循环内部。如果要通过smarty显示查询的行数,只需make:

$select_num_sub = mysql_query("SELECT * FROM market WHERE sub_cat='".$row_sub['id']."' 
$n = mysql_num_rows($select_num_sub);
$smarty->assign('n',$n);

在外圈内,你会没事的。

答案 1 :(得分:1)

mysql_num_rows函数用于计算表中的行,因此它将在while循环之前使用。

$select_sub_cat = mysql_query("SELECT * FROM sub_cat WHERE ct_id='".$row_main['id']."' LIMIT 8 ");
while($row_sub = mysql_fetch_array($select_sub_cat))
{
  $select_num_sub = mysql_query("SELECT * FROM market WHERE sub_cat='".$row_sub['id']."'  ");
  $n = mysql_num_rows($select_num_sub);
  while($row_num_sub = mysql_fetch_array($select_num_sub))
  {

    $smarty->assign('n',$n);
  }

 $sub_cats[] = $row_sub;
}
$smarty->assign('sub_cats',$sub_cats);