while or foreach loop ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

while or foreach loop ?

Post by Rippie »

Hi everybody,

Could i please have some thoughts on if you where stuck with 3 mysql queries, which would be the better way of getting data shown ? foreach loops or while loops ?

Currently i have following

mysql_query 1
While loop 1
mysql_query 2
while loop 2
mysql_query 3
while loop 3

Got an example where someone said use foreach instead, but of course that means i need arrays.

Do anyone have any thoughts on which is better to us ?

Cheers !!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: while or foreach loop ?

Post by AbraCadaver »

You can't foreach on a result set, that's why you are using while. If inside your while you assign the fetched row to an array, then later in the script you can foreach on that array of rows.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: while or foreach loop ?

Post by Rippie »

So something like this ? Lets say i want vendID and vendorName and in the foreach i would like to show vendorname but have a link that uses vendorID. How would i do that ?

Code: Select all

<?php
$query = mysql_query("SELECT vendorID, vendorName from Vendors ORDER BY vendorName asc");
 
$vendors = array();
 
while($row = mysql_fetch_array($query)) {
 
    // do something with the $vendors array ? (get data in ? but how ?
 
}
 
foreach($vendors as $vendorID=>$vendor) {
    // how do i output it ?
}
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: while or foreach loop ?

Post by AbraCadaver »

There's really no reason to do it the way you've shown it, you're just duplicating effort. Just do what you need in the while loop. If you need your rows agin later then something like this:

Code: Select all

$query = mysql_query("SELECT vendorID, vendorName from Vendors ORDER BY vendorName asc");
 
while($row = mysql_fetch_array($query)) {
    echo $row['vendorID'] . $row['vendorName'];
    $vendors[] = $row;
}
 
foreach($vendors as $vendor) {
   echo $vendor['vendorID'] . $vendor['vendorName'];
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: while or foreach loop ?

Post by Rippie »

This is great stuff.... THANK YOU ALOT !!!

Can i ask you for something else ? With my code below that shows vendors and products.... products belong to vendors, but how do i get the products that have same vendorid as the vendor have, to be shown ? guessing i would need the second foreach inside the other one. but how do i then only list products that should be in here ?

Code: Select all

$query = mysql_query("SELECT vendID, vendName from spVendors ORDER BY vendName asc");
$query2 = mysql_query("SELECT prodID, prodName, vendID from spProducts ORDER BY prodName asc"); // new query
 
while($row = mysql_fetch_array($query)) {
#echo "".$row['vendID']." ".$row['vendName']." <br />";
$vendors[] = $row;
}
 
while($row2 = mysql_fetch_array($query2)) {
#echo "".$row2['prodID']." ".$row2['prodName']." <br />";
$products[] = $row2;
}
 
echo "<p>";
echo "<strong>VENDORID - VENDORNAME</strong><br />";
foreach($vendors as $vendor) {
echo "".$vendor['vendID']." - ".$vendor['vendName']." <br />";
}
echo "</p>";
 
echo "<p>";
echo "<strong>PRODUCTID - PRODUCTNAME - VENDORID</strong><br />";
foreach($products as $product) {
echo "".$product['prodID']." - ".$product['prodName']." - ".$product['vendID']." <br />";
}
echo "</p>";
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: while or foreach loop ?

Post by Rippie »

Someone gave me some code that i cant get to work, but it have this in it:

Code: Select all

while($product = mysql_fetch_assoc($query2)) {
  $vendID = $product['vendID'];
  $productID = $product['prodID'];
  $vendors[$vendID]['products'][$productID] = $product;
}
 
foreach($vendors as $vendID=>$vendor) {
  echo "<strong>".$vendor['vendName']."</strong> <br />";
  foreach($vendor['products'] as $prodID=>$product) {
    echo "<span <a href=\"showproduct.php?prod=".$prodID."\">".$product['prodName']."</a> :</span>&nbsp;";
}
Hope you can help :)
Post Reply