douxian1923 2010-10-29 12:47
浏览 73
已采纳

PHP / MySQL将'while'语句转换为'foreach'

Following on from a previous question, shown here, I was wondering how I would go about changing the code below into 'foreach' statements? Any help greatly appreciated, S.

<div id="banner-wrap">          
    <div id="banner" class="gallery">               
        <ul class="galleryBar">               

                <?php       
                $homeB=mysql_query("select * from istable where fpGallery = '1' ORDER BY RAND() LIMIT 0, 5");
                while($homeG=mysql_fetch_array($homeB)) {   
                $linkcode = $homeG['title'];
                $linkcode = str_replace(" ","",$linkcode);
                echo '
                <li>
                <a href="'.$wwwUrl.'images/'.$homeG['image'].'" rel="'.$linkcode.'">
                <img src="'.$wwwUrl.'images/tn/'.$homeG['image'].'" width="75" height="55" alt="'.$homeG['title'].'" />
                </a>
                </li>                       
                ';
                }                                   

        echo '</ul>';
         echo '</div>'; 

                while($homeGal=mysql_fetch_array($homeB)) {                             
                echo '
                <div id="'.$linkcode.'" class="overlay">
                    <h3>'.$homeGal['title'].'</h3>
                    <h4>'.$homeGal['location'].'</h4>                           
                </div>                                              
                ';
                }

                ?>          
    </div>
  • 写回答

5条回答 默认 最新

  • doubi3929 2010-10-29 12:50
    关注

    You can do something like:

    $data = array();
    while($homeG=mysql_fetch_array($homeB)) {   
        $data[] = $homeG;
    }
    
    foreach($data as $row) {
        // your code hre
    }
    

    But my advice to you is split your programming logic and view logic/code. You'd better try to create something as Model View Controller [ http://en.wikipedia.org/wiki/Model–View–Controller ]

    or use a PHP Framework (CakePHP, Zend Framework, CodeIgniter)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制