tags:

views:

51

answers:

4

Hi,

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>
+2  A: 

You can't replace your while for a foreach in this situation. mysql_fetch_array / mysql_fetch_assoc only returns one row and not the whole result set from the resource passed (if the internal pointer of the connection resource is at the end, false is returned).

In your case if you need the whole result set and then work with it in a foreach you can always fetch the whole result set in an array (with a while) and then work with the array with a foreach (see infinity answer for an example). This will take much more memory/slow down the process if you have a huge result set.

AlexV
A: 

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)

infinity
Brilliant - adapted my code with your example and it works perfectly! Many thanks to you and all other members.
ss888
A: 

You can't get rid of the while loop, as the mysql_fetch functions only return one row at a time.

If you really wanted a foreach() loop, you could use a while loop and mysql_fetch to populate the data into an array, and then do a foreach loop after that on that array. That would give you a foreach, but it wouldn't get rid of the while.

Spudley
A: 

You'd have to encapsulate the database access with a custom class that implements the Iterator interface or drop the mysql_*() functions in favour of another library that provides classes that already implement such interface, such as PDO.

I believe that this kind of change, although interesting in the long term, makes little sense in your current situation. You should first become proficient in the use of arrays.

Álvaro G. Vicario