如何修复嵌套循环,因为它显示每行的所有值而不是每行一个值?

时间:2011-07-11 22:42:41

标签: php cakephp loops foreach nested-loops

<?PHP
    // areaList
    //  [0] = Upstairs Tables
    //  [1] = Upstairs Seats
    //  [2] = Upstairs TV
    //  [3] = Upstairs Computers
    //  [4] = Lounges SL
    //  [5] = Lounges BL
    //  [6] = Conference Room 216
    //  [7] = Conference Room 218
    //  [8] = Conference Room 219
    //  [9] = Conference Room 220
    // [10] = Cash 204
    // [11] = Cash 205
    // [12] = Cash 207
    // [13] = Downstairs Lobby
    // [14] = Downstairs Center
    // [15] = Downstairs Rec
    // [16] = Downstairs Computers
    // [17] = Games Bowling
    // [18] = Games Billiards
    // [19] = Games Arcade
    // [20] = Plaza  1
    // [21] = Plaza  2
    // [22] = Plaza  3
    // [23] = Plaza  4
    // [24] = Plaza  5
    // [25] = Plaza  6
    // [26] = Plaza Res

$areaList = 
    array (1=>
        "<b>Upstairs Tables", "<b>Upstairs Seats", "<b>Upstairs TV", "<b>Upstairs Computers", 
        "<b>Lounges SL", "<b>Lounges BL", "<b>Conference</b><br />Room 216", "<b>Conference</b><br />Room 218",
        "<b>Conference</b><br />Room 219", "<b>Conference</b><br />Room 220", "<b>Cash 204" , "<b>Cash 205", 
        "<b>Cash 207", "Downstairs Lobby", "<b>Downstairs Center", "<b>Downstairs Rec", 
        "<b>Downstairs Computers", "<b>Games  Bowling", "<b>Games  Billiards", "<b>Games  Arcade",
        "<b>Plaza 1", "<b>Plaza 2", "<b>Plaza 3", "<b>Plaza 4", "<b>Plaza 5", 
        "<b>Plaza 6", "<b>Plaza Res" 

    );  
    //array_unshift($areaList, "");
    //unset($areaList[0]);  
?>

<div id="add_content">                          
    <table class="listing" width="100%">
        <tr>
            <th width="25%">Area</th>
            <th width="5%">Count</th>
            <th width="40%">Comment</th>
            <th width="10%"></th>
        </tr>

        <?PHP
        $odd = 1;
        for ( $i = 1 ; $i < count($areaList) ; $i++ )
        {
            $odd=$odd+1;?>
           <form name="add" action="/directory/filename/" method="post" enctype="multipart/form-data">
            <tr <?PHP if($odd%2==1) echo "class=\"odd\"" ?>>
            <td style="text-align:center;">
            <?php echo $areaList[$i]; ?>
            <input type="hidden" name="data[TableName][area]" value="<?PHP echo $i;?>" />
            </td>
            <td>
            <?PHP 
            foreach ($upc as $uvalue) //$upc is an array from sql statement in Model.."SELECT area FROM table_name WHERE table_reports_id = ".$id." ORDER by area ASC"
            // there are 27 areas and it is displaying 27 counts for each Area instead of 1 count per area. I need to tell it to only grab the count for this area based on $i.
            {
                echo $html->input('Table/count'.$i, array('class'=>'inputbox','size'=>'3', 'value'=> $uvalue['table_name']['count'] ));

            } ?>
            </td> 
            <td width="25%"style="text-align:center;">
            <?php echo $html->input('Table/comments'.$i, array('size' => '50'), array('maxlength' => '60'));?></td>
            <td class="tdsubmitbutton" colspan="4" style="text-align:right">
            <?php echo $html->submit('Submit') ?></td>
            </tr>

         </form>
        <?PHP unset ($uvalue); }
            ?>
    </table>
    </div>
    ?>

2 个答案:

答案 0 :(得分:0)

你需要以某种方式构建该数组,以便知道什么值是什么索引,然后打印出类似$ upc [$ i]

的值

此外,如果那是你真正的sql语句,你应该总是用''括起参数,并确保转义所有输入。

答案 1 :(得分:0)

解决方案是:数组$upc,索引为$i减去1。

echo $html->input('Table/count'.$i, array('class'=>'inputbox','size'=>'3', 'value'=> $upc[$i-1]['table_name']['count'] ));