j.smith1981 Posted January 13, 2011 Share Posted January 13, 2011 I am having a bit of trouble with the following (just for theoretical purposes of course!). Just wondered how you do the following code: $products = array(); // makes a empty array called $products $i = 1; $icol = 1; while($row = mysql_fetch_array($result)) { $products[$i]['product1'] = $row[$icol][0]; $i = $i++; } All its meant to do is: SELECT * FROM products Which the table contains just 3 columns: productid | product | price 1 | product1 | 25.55 That is it, very simple, but wanted to make a multi dimension array that will be 3 columns wide, then however many rows (hence the while loop and array fetch etc). Any help is appreciated, just coming from a VBA background when it comes to the arrays in VB lol. Thanks for any help in advance, Jeremy. Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 13, 2011 Author Share Posted January 13, 2011 Do not know whats going wrong with my brain today (not enough coffee I would have thought lol). Of course this is it: while($row = mysql_fetch_array($result)) { $products[$i]['productid'] = $row[0]; $products[$i]['product'] = $row[1]; $products[$i]['price'] = $row[2]; $i = $i++; print_r($products); } Is there out of pure interest any better way of doing this perhaps though? (just to not waste your time hopefully). I do apologise about my first post though lol, I am a douchebag sometimes lol. Thanks and I look forward to any replies, Jez. Quote Link to comment Share on other sites More sharing options...
smerny Posted January 13, 2011 Share Posted January 13, 2011 i'm having a bit of trouble trying to understand exactly what you are doing or why, what exactly are you planning to do with the $products array? when you loop through a mysql result like that, you can access the fields of each row like: $row['productid'] Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 13, 2011 Author Share Posted January 13, 2011 Ah of course, I mean but that would be the function mysql_fetch_assoc (assoc meaning associative array). I believe anyways, I mean if you use _array being the last bit of that function then it would mean using integers to represent the columns, am I thinking through this properly? Sorry its just to see how to fill up a multi dimension array just out of interest, want to try and write an ecommerce website, but before I do that I wanted to work out somethings I have had great trouble understanding in the past, finally getting somewhere with it though. Thanks ever so much, Jez. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2011 Share Posted January 13, 2011 Not exactly. mysql_fetch_row - returns an enumerated array mysql_fetch_assoc - returns an associative array mysql_fetch_array - returns either enumerated or associative array, or both depending on the modifier MYSQL_NUM, MYSQL_ASSOC, or MYSQL_BOTH. Defaults to MYSQL_BOTH if no modifier is specified. Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 13, 2011 Author Share Posted January 13, 2011 Fair enough then. I mean I only got that off one source, not always the best I know but hey, I stand corrected and thank you for clearing that up for me. Thanks again, Jez. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted January 13, 2011 Share Posted January 13, 2011 Much shorter and easier: while($row = mysql_fetch_assoc($result)) { $products[] = $row; } print_r($products); Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 14, 2011 Author Share Posted January 14, 2011 Much shorter and easier: while($row = mysql_fetch_assoc($result)) { $products[] = $row; } print_r($products); That would only be a single dimension surely? It is infact a multi dimension array I am after, if its only got one [] square bracket then its instantly a single dimension array, if its got 2 [][] or more then its a whatever number but a multi dimension array. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2011 Share Posted January 14, 2011 Did you try it and then see what the print_r() looks like? Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 14, 2011 Author Share Posted January 14, 2011 Sorry I dont think I made this clear enough. It's ok I went onto explain more, you probably missed it lol (I do that all the time its cool). But this issue has now been resolved it did when I posted my explanation, I just was an idiot not to realise what I was trying to do lol. Thanks for your help though really appreciate the effort! Thanks again, Jez. PS The post above is what I did: Do not know whats going wrong with my brain today (not enough coffee I would have thought lol). Of course this is it: while($row = mysql_fetch_array($result)) { $products[$i]['productid'] = $row[0]; $products[$i]['product'] = $row[1]; $products[$i]['price'] = $row[2]; $i = $i++; print_r($products); } Is there out of pure interest any better way of doing this perhaps though? (just to not waste your time hopefully). I do apologise about my first post though lol, I am a douchebag sometimes lol. Thanks and I look forward to any replies, Jez. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2011 Share Posted January 14, 2011 Did you try it and then see what the print_r() looks like? Sorry I dont think I made this clear enough. So in other words, no, you didn't try it? Perhaps you should have because this it what it returns with data in a test table I set up. Query: $query = "SELECT product_id, product, price FROM test_table"; $result = mysqli_query( $dbc, $query ) or die(mysqli_error($dbc)); while( $row = mysqli_fetch_assoc($result) ) { $products[] = $row; } echo '<pre>'; print_r($products); echo '</pre>'; Returns: Array ( [0] => Array ( [product_id] => 1 [product] => Boots [price] => 9.99 ) [1] => Array ( [product_id] => 2 [product] => Hat [price] => 7.99 ) [2] => Array ( [product_id] => 3 [product] => Coat [price] => 19.50 ) [3] => Array ( [product_id] => 4 [product] => Gloves [price] => 3.00 ) [4] => Array ( [product_id] => 5 [product] => Skis [price] => 199.99 ) [5] => Array ( [product_id] => 6 [product] => Goggles [price] => 10.00 ) ) Then if you wanted the main $products[] array index numbers to be the same as the product_id they represent, all you'd have to do would be this: $products[$row['product_id']] = $row; Quote Link to comment Share on other sites More sharing options...
j.smith1981 Posted January 17, 2011 Author Share Posted January 17, 2011 No relook at what I put, it worked, said more or less that this issue had been resolved, just asking if there was any better method. Resolved generally means it is working lol. Quote Link to comment 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.