0

I have a problem to count total record inside foreach using php.

My code is here

$User_Line=@mysql_fetch_array(mysql_query("select * from tb_user where user_login_id='$_SESSION[SEEKER_ID]'"));
$userskill=explode(",",$User_Line['user_skills_id']);
foreach($userskill as $skill) 
{
   $tb_job=mysql_query("select * from tb_job where job_keyskills like '%$skill%'");
   $tb_job2=mysql_fetch_array($tb_job);
   echo "TOTAL=".$value=mysql_num_rows($tb_job);
}

Current result is coming like TOTAL=12 TOTAL=4 TOTAL=0 But It should be like TOTAL = 16

Please help me

Aniket Kulkarni
  • 12,517
  • 9
  • 66
  • 88
user3523177
  • 41
  • 1
  • 4
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil May 08 '14 at 06:12

3 Answers3

0

Did you just try to sum each values in a variable?

$User_Line=@mysql_fetch_array(mysql_query("select * from tb_user where user_login_id='$_SESSION[SEEKER_ID]'"));
$userskill=explode(",",$User_Line['user_skills_id']);
$total = 0;
foreach($userskill as $skill) 
{    
    $tb_job=mysql_query("select * from tb_job where job_keyskills like '%$skill%'");
    $tb_job2=mysql_fetch_array($tb_job);
    $total +=  mysql_num_rows($tb_job);        
}
echo "TOTAL=".$total;
Gwenc37
  • 2,058
  • 7
  • 17
  • 22
0

Outside your loop define the variable $total

$total = 0;

foreach($userskill as $skill) 
{
...
$total = $total + $value=mysql_num_rows($tb_job); // add the $value to $total
}

echo $total;
l'L'l
  • 42,597
  • 8
  • 87
  • 135
0

Try this

$User_Line=@mysql_fetch_array(mysql_query("select * from tb_user where user_login_id='$_SESSION[SEEKER_ID]'"));
$userskill=explode(",",$User_Line['user_skills_id']);

$Total=0;

foreach($userskill as $skill) 
{
   $tb_job=mysql_query("select * from tb_job where job_keyskills like '%$skill%'");
   $tb_job2=mysql_fetch_array($tb_job);
   $value=mysql_num_rows($tb_job);
   $Total=$Total+$Value;
}
echo "TOTAL=".$Total;
Fahad Hussain
  • 1,165
  • 1
  • 10
  • 17