doushan6161 2014-11-30 06:03
浏览 56
已采纳

嵌套'while'循环需要根据两个查询结果相等来返回信息

I am trying to display a set of offices ordered under a header that reflects the state in which the offices are located, like so:

State
 Office
 Office
 ...
State
 Office
 ...

I have beat my head against this one for a while. There is a single table 'offices' in the MySQL db which has each office's State listed under 'office_state'. Here's what I've come up with - but nothing is returned.

// Here I return a list of the distinct States:
$stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
$stateResult = mysql_query($stateQuery);

// Here I return each office's information:
$officeQuery = 'SELECT office_information, office_state FROM offices';
$officeResult = mysql_query($officeQuery);

// And here's where I'm stuck:
while ($rows = mysql_fetch_assoc($stateResult)) {

    // This returns the distinct States:
    echo $rows['state'];

    // Here is my effort at listing each office within the State:
    $rows1 = mysql_fetch_assoc($officeResult);
    if ($rows['state'] == $rows1['office_state']) {
      echo $rows1['office_information'];

    }      
}

Any help would be much appreciated.

  • 写回答

1条回答 默认 最新

  • dougang2749 2014-11-30 09:02
    关注

    try this

    <?php
    // Here I return a list of the distinct States:
    $stateQuery = 'SELECT DISTINCT office_state AS state FROM offices';
    $stateResult = mysql_query($stateQuery);
    
    // Here I return each office's information:
    $officeQuery = 'SELECT office_information, office_state FROM offices';
    $officeResult = mysql_query($officeQuery);
    
    // And here's where I'm stuck:
    while ($rows = mysql_fetch_array($stateResult)) {
    
        // Here is my effort at listing each office within the State:
        while($rows1 = mysql_fetch_array($officeResult)){
            if ($rows['state'] == $rows1['office_state']) {
              echo $rows1['office_information'];
            }
        }      
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法