如何使用php在一个mysql查询中运行两个循环

时间:2014-12-18 14:22:19

标签: php mysql

嗨朋友我有两个阵列 一个来自数据库

$row1 = mysqli_fetch_array($result1);
          $alluser = $row1['user_id'];

另一个是关联数组

 $inputArray["NoContact"] = " and candidate_joborder.status='100'";
 $inputArray["Contacted"] = " and candidate_joborder.status='200'";
 $inputArray["Qualifying"] = " and candidate_joborder.status='300'";
 $inputArray["Submitted"] = " and candidate_joborder.status='400'";
 $inputArray["Interviewing"] = " and candidate_joborder.status='500'";
 $inputArray["Offered"] = " and candidate_joborder.status='600'";
 $inputArray["ClientDeclined"] = " and candidate_joborder.status='700'";
 $inputArray["Placed"] = " and candidate_joborder.status='800'";
 $inputArray["NoStatus"] = " and candidate_joborder.status='0'";
 $inputArray["NotInConsideration"] = " and candidate_joborder.status='650'";
 $inputArray["Candidate Responded"] = " and candidate_joborder.status='250'";

这里的MYSQL查询如下:

SELECT count(candidate_joborder.joborder_id) as counts FROM candidate_joborder JOIN joborder ON joborder.joborder_id = candidate_joborder.joborder_id where candidate_joborder.added_by= $alluser and candidate_joborder.status = $inputArray

这里我需要像这样

<table border =1>
  <tr>
    <th>user name</th> 
    <th>No_Contact</th>
    <th>Contacted</th>
    <th>Qualifying</th>
    <th>Submitted</th>
    <th>Interviewing</th>
    <th>Offered</th>
    <th>Client_Declined</th>
    <th>Placed</th>
    <th>No_Status</th>
    <th>Not in Consideration</th>
    <th>Candidate_Responded</th>
  </tr>

根据查询

执行列和行

同样在用户名列中将包含$ alluser值 在这里,我尝试过使用 PHP Foreach ,但我现在无法理解 请帮助我

这里我使用foreach循环来获取我的代码在下面的关联数组值它只适用于关联数组

foreach($inputArray as $x => $x_value)
        {
            //concatenates the SQL query with its status through the temporary variable
                $temp = $sql . $x_value;
            //executes the sql query and returns the resultset
                $resultSet = mysqli_query($con,$temp);
            //assigns the returned rows in the resultset to the variable using the mysqli_fetch_array()
                $row = mysqli_fetch_array($resultSet);
            //assigns the count to the second array for all the status using the index
                $outputArray[$x]=$row['counts'];
        }

        echo "<table align = center border =1><tr>";
        //foreach loop which loops through outputArray
        foreach($outputArray as $x => $x_value)
        {
            echo '<th>'.$x .'</th>';
        }
        echo "</tr><tr>";
        //foreach loop which loops through outputArray
        foreach($outputArray as $x)
        {
            echo '<td>'.$x .'</td>';
        }

        echo "</tr></table>";

其实我在这里使用$ sql变量连接MYSQL查询 这里我连接$ sql而没有$ alluser 喜欢mysql

SELECT candidate_joborder.joborder_id as counts,joborder.title as title FROM candidate_joborder JOIN joborder ON joborder.joborder_id = candidate_joborder.joborder_id where candidate_joborder.status= **$x_value**

2 个答案:

答案 0 :(得分:1)

看起来你想要返回一行,其中包含该行中每个状态的计数。

如果是这样,可以这样做: -

SELECT SUM(IF(candidate_joborder.status='100', 1, 0)) AS NoContact,
        SUM(IF(candidate_joborder.status='200', 1, 0)) AS Contacted,
        SUM(IF(candidate_joborder.status='300', 1, 0)) AS Qualifying,
        SUM(IF(candidate_joborder.status='400', 1, 0)) AS Submitted,
        SUM(IF(candidate_joborder.status='500', 1, 0)) AS Interviewing,
        SUM(IF(candidate_joborder.status='600', 1, 0)) AS Offered,
        SUM(IF(candidate_joborder.status='700', 1, 0)) AS ClientDeclined,
        SUM(IF(candidate_joborder.status='800', 1, 0)) AS Placed,
        SUM(IF(candidate_joborder.status='0', 1, 0)) AS NoStatus,
        SUM(IF(candidate_joborder.status='650', 1, 0)) AS NotInConsideration,
        SUM(IF(candidate_joborder.status='250', 1, 0)) AS Candidate Responded
FROM candidate_joborder 
JOIN joborder 
ON joborder.joborder_id = candidate_joborder.joborder_id 
WHERE candidate_joborder.added_by= $alluser 

但对于单个用户来说,这似乎毫无意义,最好将多个行恢复,每个状态一个并循环结果: -

SELECT candidate_joborder.status, 
        COUNT(*) 
FROM candidate_joborder 
JOIN joborder 
ON joborder.joborder_id = candidate_joborder.joborder_id 
WHERE candidate_joborder.added_by= $alluser 
AND candidate_joborder.status IN ('100', 200','300', '400', '500', '600', '700', '800', '0', '650', '250')
GROUP BY candidate_joborder.status

答案 1 :(得分:0)

两个数组都在PHP中(无论它们是否从DB填充都是无关紧要的),这是在服务器端,你需要形成HTML代码(在客户端执行)。

使用foreach迭代所需的数组,然后使用echo形成HTML输出。