0

Possible Duplicate:
i can’t make num rows inside while looping

i try allot of to make num rows in while looping to get number of rows for another tables

$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);
Community
  • 1
  • 1
  • 4
    What is the question? Your problem is not clear from the post currently. – jheddings Oct 17 '12 at 05:59
  • Looks suspiciously like [i can't make num rows inside while looping](http://stackoverflow.com/questions/12927269/i-cant-make-num-rows-inside-while-looping) – jheddings Oct 17 '12 at 06:01

2 Answers2

1

It has no sense to put this lines:

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

Inside an inner loop. If you want to display the number of rows of a query via smarty, just 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);

Inside the outer loop and you'll be fine.

Hernan Velasquez
  • 2,770
  • 14
  • 21
1

mysql_num_rows function is used to count rows in table so it will used before the while-loop.

$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);
Prem
  • 697
  • 3
  • 10