1

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 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

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

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.

Comments

0

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.

Comments

0

While loop foreach()

DB CREATE

  1. id
  2. title
  3. href
  4. img
<?php 
   $this = array();

   $db = mysql_query('SELECT * FROM `'.DB_PREFIX.'product` where id order by id asc', ConnectDb()); // ConnectDb() to PHP Deprecated: mysql_query() error fix

   while($see=mysql_fetch_array($db)) {   
         $this[] = $see;
   }
?>

<ul class="product">
  <?php foreach ($this as $product) { ?>
    <li><a href="<?php echo $product['href']; ?>" title="<?php echo $product['title']; ?>"><img src="<?php echo $product['img']; ?>" alt="<?php echo $product['title']; ?>"><?php echo $product['title']; ?></a></li>
  <?php } ?>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.