我有一个问题是使用php计算foreach中的总记录。
我的代码在这里
$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);
}
目前的结果是TOTAL=12 TOTAL=4 TOTAL=0
但它应该像TOTAL = 16
请帮帮我
答案 0 :(得分:0)
您是否只是尝试对变量中的每个值求和?
$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;
答案 1 :(得分:0)
在循环外定义变量$total
$total = 0;
foreach($userskill as $skill)
{
...
$total = $total + $value=mysql_num_rows($tb_job); // add the $value to $total
}
echo $total;
答案 2 :(得分:0)
试试这个
$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;