2014-07-17 42 views
0

這段代碼是從SQL數據庫中選擇在線用戶及其用戶模式,在他們的用戶名之前在php輸出中添加他們的狀態前綴(〜,&,@,%,+)。結合多個foreach循環的輸出php

<< SQL Connection Data Goes here >> 

//Half Mod/Half Op 

$hmodsinlistsql = mysql_query("SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lh = 'Y' AND ison.mode_lo = 'N' 
AND user.away = 'N'"); 

//Voiced/Temp Mod 

$tempmodsinlistsql = mysql_query("SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lv = 'Y' 
AND user.away = 'N'"); 

while ($row = mysql_fetch_assoc($hmodsinlistsql)) { 
$rows[] = $row; 
} 

foreach($rows as $row) { 

echo '<tr><td>%' . $row[nick] .', </td></tr>'; 
} 

上面的代碼給了我這樣的:

%hmod2, %hmod3, %hmod4, %hmod1, %hmod5, 

所需的輸出:

%hmod2, %hmod3, %hmod4, %hmod1, %hmod5, +tmod1, +tmod2, +tmod3 

我的問題是,我可以得到HMOD結果正常,但是我不知道如何在單獨的查詢中有多個foreach循環,然後根據需要將結果合併到單個輸出字符串中。

如果任何人能夠幫助,這將是偉大的。

+0

您永遠不會合並這兩個搜索結果,並且您只有一個迴響名稱的循環。 –

回答

0

把這個:

while ($row = mysql_fetch_assoc($hmodsinlistsql)) { 
$rows[] = $row; 
} 

在querys之間並聲明循環前陣。像這樣:

$rows = []; // Declaring an array correctly before trying to fill it 

$hmodsinlistsql = mysql_query("SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lh = 'Y' AND ison.mode_lo = 'N' 
AND user.away = 'N'"); 

while ($row = mysql_fetch_assoc($hmodsinlistsql)) { 
array_push($rows, $row); // Push the value to the array 
} 

//Voiced/Temp Mod 

$tempmodsinlistsql = mysql_query("SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lv = 'Y' 
AND user.away = 'N'"); 

while ($row = mysql_fetch_assoc($hmodsinlistsql)) { 
array_push($rows, $row); // Push the value to the array 
} 

foreach($rows as $row) { 

echo '<tr><td>%' . $row['nick'] .', </td></tr>'; 
} 
+0

所以我這樣做,我返回一個空白頁。這裏是我的完整代碼,不包括sql連接:http://pastebin.com/ZnYXZR9G – entropy

+0

我錯誤地沒有正確地在我最後的答案中聲明數組,對不起。我解決了這個問題,並且我還將'$ row [nick]'改成了'$ row ['nick']'。看到我的編輯,如果解決了 - 請將我的答案標記爲已解決。如果沒有 - 給我留言,我會給你更多的幫助來解決這個問題:-)你也應該看看這個http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection -in-php?rq = 1 – simeg

+0

在正確聲明數組之後,它按預期工作!非常感謝!! – entropy

0

我還沒有測試過,但試試這個。你真的需要停止使用不推薦使用的mysql擴展。你不僅會擁有更多的功能,而且還會有更多的安全性。正如你所看到的,如果你使用過程風格的語法幾乎是相同的。

$connect = mysqli_connect("localhost", "user", "pw", "db"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

//Half Mod/Half Op 

$query = "SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lh = 'Y' AND ison.mode_lo = 'N' 
AND user.away = 'N'"; 

//Voiced/Temp Mod 

$query .= "SELECT user.nick FROM `ison` INNER JOIN `user` on 
`ison`.`nickid` = `user`.`nickid` INNER JOIN `chan` ON `ison`.`chanid` = 
`chan`.`chanid` WHERE chan.channel = '#Channel' AND user.nick NOT LIKE 
'%bot%' AND user.nick NOT LIKE '%Serv%' and user.realname NOT LIKE 
'%bot%' AND user.realname NOT LIKE '%Serv' AND user.nick NOT LIKE 
'%STAT%' AND user.realname NOT LIKE '%EVA%APB%' AND ison.mode_lv = 'Y' 
AND user.away = 'N'"; 

/* execute multi query */ 
if (mysqli_multi_query($connect, $query)) { 
    do { 
     /* store first result set */ 
     if ($result = mysqli_store_result($connect)) { 
      while ($row = mysqli_fetch_row($result)) { 
       $rows[] = $row; 
      } 
     foreach($rows as $row) { 
      echo '<tr><td>%' . $row[nick] .', </td></tr>'; 
      } 
      mysqli_free_result($result); 
     } 
    } 
} 

/* close connection */ 
mysqli_close($connect);