结合多个foreach循环的输出php

时间:2014-07-17 04:33:11

标签: php sql loops foreach irc

此代码,是从SQL数据库中选择在线用户及其用户模式,在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循环,然后根据需要将结果合并到单个输出字符串中。

如果有人能够帮助解决这个问题,那就太好了。

2 个答案:

答案 0 :(得分:0)

把这个:

while ($row = mysql_fetch_assoc($hmodsinlistsql)) {
 $rows[] = $row;
}
在您的查询之间

并在循环之前声明数组。像这样:

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

答案 1 :(得分: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);