webent Posted June 10, 2008 Share Posted June 10, 2008 Hi all, so here I am again... So, come to find out, that when an array is passed into a function, the function actually acts as a loop... did not know that... was quite interesting to find out... since that is the case however, it creates a small problem for me... let me show you what I have for code first, then I'll tell you what I'm trying to make it do... function showProducts($fields) { echo ' <tr> <td>' . $fields['products_name'] . '<br />' . $fields['products_description'] . '</td> <td>' . $fields['products_name'] . '<br />' . $fields['products_description'] . '</td> <td>' . $fields['products_name'] . '<br />' . $fields['products_description'] . '</td> </tr> '; } I'm sure some of you are laughing right about now, go ahead, I would too... but I need to be able to create columns and cells and populate each individual cell with one particular product's information from the array... Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/ Share on other sites More sharing options...
webent Posted June 10, 2008 Author Share Posted June 10, 2008 My intention is not to bump my post... but some logic just came to me, I just need help with part of it... Here's what I came up with... I just can't figure out what the SOMETHING is supposed to be... ? function showProducts($fields) { echo '<tr>'; $count = 0; while (SOMETHING < 4)) { echo'<td>' . $fields['products_name'] . '</td>'; $count++; } echo '</tr>'; } Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562208 Share on other sites More sharing options...
hansford Posted June 10, 2008 Share Posted June 10, 2008 when you pass an array to a function it's still an array. You have to create a loop in the function if you want to get it's values in a logical manner. Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562214 Share on other sites More sharing options...
webent Posted June 10, 2008 Author Share Posted June 10, 2008 So what I can't seem to find any information on anywhere is, how to iterate a loop by each dataset within an array? Here's an example print_r of some of it... How do I, with a loop, determine when one product starts and another stops? Array ( [0] => 124 [products_id] => 124 [1] => anskunumber [products_sku] => anskunumber [2] => [products_model] => [3] => Jim Wagner Reality-Based Knife Defense DVD [products_name] => Jim Wagner Reality-Based Knife Defense DVD [4] => Jim Wagner Reality-Based Knife Defense DVD [products_description] => Jim Wagner Reality-Based Knife Defense DVD [5] => 0.0000 [products_cost] => 0.0000 [6] => 0.0000 [products_price] => 0.0000 [7] => 0.0000 [products_msrp] => 0.0000 [8] => [products_map] => [9] => 0.0000 [products_map_price] => 0.0000 [10] => 0.2 [products_weight] => 0.2 [11] => 0 [products_length] => 0 [12] => 0 [products_width] => 0 [13] => 0 [products_height] => 0 [14] => 6 [products_qty] => 6 [15] => 0 [products_sort_order] => 0 [16] => 2008-06-10 [products_date_added] => 2008-06-10 [17] => 0000-00-00 [products_date_modified] => 0000-00-00 [18] => [products_metatags_title] => [19] => [products_metatags_keywords] => [20] => [products_metatags_description] => [21] => 19 [manufacturers_id] => 19 [22] => 1 [product_types_id] => 1 ) Array ( [0] => 1024 [products_id] => 1024 [1] => anskunumber [products_sku] => anskunumber [2] => [products_model] => [3] => SAS Jungle Survival [products_name] => SAS Jungle Survival [4] => Practical survival handbook based on SAS training and techniques, covering every aspect of survival in the worlds most inhospitable places. 160 pages with over 75 illustrations. [products_description] => Practical survival handbook based on SAS training and techniques, covering every aspect of survival in the worlds most inhospitable places. 160 pages with over 75 illustrations. [5] => 0.0000 [products_cost] => 0.0000 [6] => 0.0000 [products_price] => 0.0000 [7] => 0.0000 [products_msrp] => 0.0000 [8] => [products_map] => [9] => 0.0000 [products_map_price] => 0.0000 [10] => 0.5 [products_weight] => 0.5 [11] => 0 [products_length] => 0 [12] => 0 [products_width] => 0 [13] => 0 [products_height] => 0 [14] => 3 [products_qty] => 3 [15] => 0 [products_sort_order] => 0 [16] => 2008-06-10 [products_date_added] => 2008-06-10 [17] => 0000-00-00 [products_date_modified] => 0000-00-00 [18] => [products_metatags_title] => [19] => [products_metatags_keywords] => [20] => [products_metatags_description] => [21] => 3 [manufacturers_id] => 3 [22] => 1 [product_types_id] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562301 Share on other sites More sharing options...
kenrbnsn Posted June 10, 2008 Share Posted June 10, 2008 The general way is: <?php $q = "select * from yourtable"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); while ($rw = mysql_fetch_assoc($rs)) { // // do your processing for each record returned in the array $rw // } ?> What do you have? Ken Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562306 Share on other sites More sharing options...
Barand Posted June 10, 2008 Share Posted June 10, 2008 First, use mysql_fetch_assoc() or mysql_fetch_row(). Using fetch_array() gets all the values twice as in your array unless you use its optional second argument. then echo '<tr>'; foreach ($fields as $fld) { echo "<td>$fld</td>"; } echo '</tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562330 Share on other sites More sharing options...
webent Posted June 10, 2008 Author Share Posted June 10, 2008 Thanks Barand, switching from mysql_fetch_array() to mysql_fetch_assoc() got rid of the obsolete array data... Now as far as the foreach ($fields as $fld), I tried that before but couldn't and still can't figure out how to pull specific vars from the array, I tried this... echo '<tr>'; foreach ($fields as $fld) { echo '<td>' . $fld['products_name'] . '</td>'; } echo '</tr>'; but that just gave me gibberish... Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562593 Share on other sites More sharing options...
DarkWater Posted June 10, 2008 Share Posted June 10, 2008 What kind of gibberish? =/ Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562606 Share on other sites More sharing options...
Barand Posted June 10, 2008 Share Posted June 10, 2008 your question was how to iterate a loop by each dataset within an array? But now you just want specific items. Which solution do you really want? The most efficient way is select just the items you want in the original query instead of "SELECT * ..." And yes, that will give gibberish. $fields is the array, $fld is item within that array, changing to the next item with each iteration, as per your original question . Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562614 Share on other sites More sharing options...
webent Posted June 10, 2008 Author Share Posted June 10, 2008 Well, I didn't explain myself appropriately, what I'm trying to do is, when a category id is passed and queried, to pull up all of the products that have a relation with that category id... and then put the products (products_id, products_image, products_name, products_description, etc.) in a table, 3 columns wide and infinately long... with 65000 plus products, each category can have hundreds or thousands or products to display... Of course I will have to add paging to it, but I got to get this part working first... The reason I am putting into an array and then passing it into a function is because the array will have data related to that product from different tables... so I assembled it into an array and then passed it to the function. Quote Link to comment https://forums.phpfreaks.com/topic/109598-functions-and-arrays-and-loops-oh-my/#findComment-562620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.