i can't make num rows inside while looping

2019-03-05 02:12发布

i have tow tables first table : For products Second Table : For Categories

i want to show in browser like this

category1 (20) category2 (5) category3 (3)

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

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

2条回答
叛逆
2楼-- · 2019-03-05 02:55

Try this:

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

 $sub_cats[] = $row_sub;
 $sub_cats['count']=mysql_num_rows($select_num_sub);
}
$smarty->assign('sub_cats',$sub_cats);
查看更多
淡お忘
3楼-- · 2019-03-05 03:10

Try

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

and then call on your smarty :

{$sub_cats.name} ({$sub_cats.count}) // $sub_cats.name should be 'name' of category

查看更多
登录 后发表回答