0

可能重复:
循环时我无法在里面创建 num 行

我尝试在循环中分配 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);
4

2 回答 2

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);
于 2012-10-17T06:36:33.477 回答
1

放置以下几行是没有意义的:

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

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

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

在外循环里面,你会没事的。

于 2012-10-17T06:02:45.470 回答