在另一个while循环中使用while循环来输出特定数据

时间:2016-04-04 15:29:32

标签: php

我有一个while循环,输出基于mysql查询打开票证的用户。我还有另一个while循环输出所有打开的票证。他们都工作。我尝试将第二个while循环放在第一个循环中,这样我就可以用打开的票证列出每个用户。我使用" in_array()。问题在于它输出第一个用户和票证,但其余部分都没有。

$dbc = mysqli_connect('localhost', 'root', 'root', 'tickets2') or die('Error connecting to Mysql Server');

$query1 = " SELECT DISTINCT number, username, subject, o.name " .
"FROM ost_ticket t, ost_staff s, ost_ticket__cdata d, ost_user u, ost_organization o " .
"WHERE status_id = 1 AND t.staff_id=s.staff_id AND d.ticket_id=t.ticket_id AND u.id=t.user_id AND o.id=u.org_id " .
"ORDER by username" ;

$query2 = " SELECT DISTINCT username " .
"FROM ost_ticket t, ost_staff s, ost_ticket__cdata d, ost_user u, ost_organization o " .
"WHERE status_id = 1 AND t.staff_id=s.staff_id AND d.ticket_id=t.ticket_id AND u.id=t.user_id AND o.id=u.org_id " .
"ORDER by username" ;

$tickets = mysqli_query($dbc, $query1) or die ('Error');

$users = mysqli_query($dbc, $query2) or die ('Error');

while($row = mysqli_fetch_assoc($users)){
  print_r($row);
  echo "<br />";
  $user = $row['username'];
   while($row1 = mysqli_fetch_assoc($tickets)){
   if(in_array($user, $row1)){
     print_r($row1);
     echo "<br />";
    }
 }

}

while($row1 = mysqli_fetch_assoc($tickets)){
print_r($row1);
echo "<br />";
}

这是我得到的输出

Array ( [username] => aolusola )
Array ( [number] => 905495 [username] => aolusola [subject] => PAssword change Tool [name] =>  A Hotel )
Array ( [number] => 770279 [username] => aolusola [subject] => Laundry posting  [name] => Bfish Hotel )
Array ( [number] => 972356 [username] => aolusola [subject] => New TC  RVC 8 [name] => Inter Hotel )
Array ( [username] => cakaluka )
Array ( [username] => fmbah )
Array ( [username] => habel )
Array ( [username] => iadebanjo )
Array ( [username] => iakintade )
Array ( [username] => jajani )
Array ( [username] => jakinmosin )
Array ( [username] => jomaivboje )
Array ( [username] => logunleye )
Array ( [username] => oogundimu )
Array ( [username] => panawe )
Array ( [username] => solushola )
Array ( [username] => ugouvietesivwi )

第一个用户按计划输出,但其余的不是那么多。任何想法我缺少什么

由于

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

$users = mysqli_query($dbc, $query2) or die ('Error');

while($row = mysqli_fetch_assoc($users)){
  print_r($row);
  echo "<br />";
  $user = $row['username'];
$tickets = mysqli_query($dbc, $query1) or die ('Error');
   while($row1 = mysqli_fetch_assoc($tickets)){
   if(in_array($user, $row1)){
     print_r($row1);
     echo "<br />";
    }
 }

}

在while循环中执行查询,因为mysqli_fetch_assoc仅是转发。因此,每次循环运行时都需要执行它。或者您只能通过以下方式获得当前用户的门票:

$dbc = mysqli_connect('localhost', 'root', 'root', 'tickets2') or die('Error connecting to Mysql Server');

$query1 = " SELECT DISTINCT number, username, subject, o.name " .
"FROM ost_ticket t, ost_staff s, ost_ticket__cdata d, ost_user u, ost_organization o " .
"WHERE status_id = 1 AND t.staff_id=s.staff_id AND d.ticket_id=t.ticket_id AND u.id=t.user_id AND o.id=u.org_id " .
"ORDER by username";

$query2 = " SELECT DISTINCT username " .
"FROM ost_ticket t, ost_staff s, ost_ticket__cdata d, ost_user u, ost_organization o " .
"WHERE status_id = 1 AND t.staff_id=s.staff_id AND d.ticket_id=t.ticket_id AND u.id=t.user_id AND o.id=u.org_id " .
"ORDER by username";

$users = mysqli_query($dbc, $query2) or die ('Error');

while($row = mysqli_fetch_assoc($users)){
  print_r($row);
  echo "<br />";
  $user = $row['username'];
$query3 = " SELECT DISTINCT number, username, subject, o.name " .
"FROM ost_ticket t, ost_staff s, ost_ticket__cdata d, ost_user u, ost_organization o " .
"WHERE `user_name` = '{$user}' status_id = 1 AND t.staff_id=s.staff_id AND d.ticket_id=t.ticket_id AND u.id=t.user_id AND o.id=u.org_id " .
"ORDER by username" ;
$tickets = mysqli_query($dbc, $query1) or die ('Error');
   while($row1 = mysqli_fetch_assoc($tickets)){
     print_r($row1);
     echo "<br />";
 }

}

$tickets = mysqli_query($dbc, $query1) or die ('Error');
while($row1 = mysqli_fetch_assoc($tickets)){
print_r($row1);
echo "<br />";
}

答案 1 :(得分:0)

It is very bad practice, to select data from db in loops. If you have many rows, your code will be very slow. You should make only one select, which will take data from both tables. Use JOIN (http://www.w3schools.com/sql/sql_join.asp)