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,825
  • 9
  • 67
  • 90
user3523177
  • 51
  • 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,064
  • 7
  • 18
  • 22
  • 1
    @user3523177 if it did helped you and solved the problem kindly upvote or accept this answer to repay the answerers effort. good day :) – user1978142 May 08 '14 at 06:51
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
  • 44,951
  • 10
  • 95
  • 146
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