简体   繁体   English

MySQL查询遍历多个变量

[英]Mysql Query looping through multiple variables

I'm trying to display a variety of results with a mysqli query and i feel like i am using up too much code. 我正在尝试通过mysqli查询显示各种结果,我感觉我正在使用过多的代码。 I'm wondering if anyone can tell me if i can loop through multiple variables. 我想知道是否有人可以告诉我是否可以遍历多个变量。

For the example i have multiple Agents and Multiple columns for the agents wich are equal for all the agents. 对于该示例,我有多个Agent,并且Agent的Multiple列对于所有代理都是相等的。 I want to run the query so it will cycle through all the columns for and agent then output the data and then move on to the next agent and do the same 我想运行查询,以便它将遍历所有列和代理,然后输出数据,然后移至下一个代理并执行相同的操作

echo "<table>
<tr><th>Van</th><th>Aantal</th></tr>";

$sql = ("SELECT SUM(Case_closed) as sum_closed FROM productie WHERE Agentnaam='Agent1' ");
    $result = mysqli_query($connect, $sql)  ;
            $Case_closed = $result->fetch_object()->sum_closed;

echo "<tr><td>Cases closed</td>";
echo "<td>$Case_closed</td></tr>";

$sql = ("SELECT SUM(Case_handled) as sum_handled FROM productie WHERE Agentnaam='Agent1' ");
    $result = mysqli_query($connect, $sql)  ;
        $Case_handled = $result->fetch_object()->sum_handled ;

echo "<tr><td>Cases handled</td>";
echo "<td>$Case_handled </td></tr></table>";

I found the solution with : Foreach 我找到了解决方案:Foreach

  $connect = new mysqli ('localhost', $username, $password, $database);
if (!$connect) {
    die('Could not connect: ' . mysqli_error($connect));
} else {

    $normal_array = array('Agent1','Agent2','Agent3');
    foreach($normal_array as $value){


    $s = "SELECT * FROM productie WHERE Agentname='$value' AND Date='$actual_time'"; 
    $result = mysqli_query($connect,$s);
    $row = mysqli_fetch_row($result);



    Echo "</br></br><table><tr><th>$value</th><th>Ammount</th></tr>";

    Echo "<tr><td>Cases closed      :</td><td> " .$row[2].       "</td></tr>";
    Echo "<tr><td>Cases handled     :</td><td> " .$row[3].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[4].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[5].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[6].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[7].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[8].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[9].       "</td></tr>";
    Echo "<tr><td>another column    :</td><td> " .$row[10].      "</td></tr></table></br>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM