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

发布于 2024-11-19 16:23:18 字数 3709 浏览 3 评论 0原文

<?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>
    ?>
<?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>
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打 2024-11-26 16:23:18

您需要以某种方式构建该数组,以便知道索引处的值是什么,然后打印出像 $upc[$i] 这样的值。

此外,如果这是您真正的 sql 语句,您应该始终将参数用 '' 括起来,并确保转义所有输入。

You need to somehow build that array so you know what value is at what index and then print out the value like $upc[$i]

Also if that is your real sql statement you should always enclose parameters with '' and make sure to escape all input.

离线来电— 2024-11-26 16:23:18

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

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

solution was: array $upc with index as $i minus 1.

echo $html->input('Table/count'.$i, array('class'=>'inputbox','size'=>'3', 'value'=> $upc[$i-1]['table_name']['count'] ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文