Page 1 of 1
while or foreach loop ?
Posted: Tue Mar 09, 2010 2:11 pm
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 !!
Re: while or foreach loop ?
Posted: Tue Mar 09, 2010 2:47 pm
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.
Re: while or foreach loop ?
Posted: Tue Mar 09, 2010 3:11 pm
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 ?
}
?>
Re: while or foreach loop ?
Posted: Tue Mar 09, 2010 3:53 pm
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'];
}
Re: while or foreach loop ?
Posted: Tue Mar 09, 2010 5:45 pm
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>";
Re: while or foreach loop ?
Posted: Tue Mar 09, 2010 5:48 pm
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> ";
}
Hope you can help
