开发者

PHP/MySQL convert 'while' statements to 'foreach'

开发者 https://www.devze.com 2023-01-22 18:58 出处:网络
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.

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" c开发者_开发百科lass="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>


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.


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)


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消