Search code examples
phpsqlloopsforeachirc

Combining Output of Multiple foreach loops php


This Code, is to Select Online users and their user mode from an SQL database, add their status prefix (~, &, @, %, +) before their username in the php output.

<<  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>';
}  

Above Code gives me this:

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

Desired Output:

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

My issue is that i can get the hmod results fine, however i'm not sure how to have multiple foreach loops on separate queries, and then merge the results into a single output string as desired above.

if anyone is able to help with this, that would be great.


Solution

  • Put this:

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

    in between your querys and declare the array before the loops. Like this:

    $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>'; 
    }