spenceddd Posted February 9, 2010 Share Posted February 9, 2010 Hello, I am just trying to return the table below in the following format but I am having some trouble in getting the syntax right. Still a php novice! mysql table (id colomn hidden): portfolioItems category 104 4 103 48 100 7 100 5 105 5 105 4 105 18 104 7 105 48 105 7 104 5 103 7 100 4 100 18 The format I would like to return is: Array( [portfolioItem01] = Array( [category01] [category02] [category03] [category04] [etc] ) [portfolioItem02] = Array( [category01] [category02] [category03] [category04] [etc] ) [portfolioItem03] = Array( [category01] [category02] [category03] [category04] [etc] ) ) This is the code I am using currently but it's not working, $query = "SELECT category, portfolioItem FROM PIC ORDER BY portfolioItem, category DESC"; $result = mysql_query($query) or die(mysql_error()); $catResultArray[] = array(); $currentPItem = -1; // Print out result while($row = mysql_fetch_array($result)){ if ($currentPItem!=$row['portfolioItem']){ $currentPItem = $row['portfolioItem']; array_push($catResultArray, $currentPItem); } array_push($catResultArray[$currentPItem], $row['category']); //echo $row['portfolioItem']. " - ". $row['category']; //echo "<br />"; } print_r($catResultArray); ....can anybody help please? Thanks very much Spencer Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/ Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 instead of order by, try using group by portfolioItem Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/#findComment-1009517 Share on other sites More sharing options...
Catfish Posted February 9, 2010 Share Posted February 9, 2010 $query = "SELECT category, portfolioItem FROM PIC ORDER BY portfolioItem, category DESC"; $result = mysql_query($query) or die(mysql_error()); $catResultArray[] = array(); // build array while($row = mysql_fetch_array($result)){ $catResultArray[$row['portfolioItems']][] = $row['category']; // this line builds the array in the format you want } // output all items in array: foreach ($catResultArray as $portfolioItem => $catData) { print($portfolioItem.': <br/>'."\n"); // print portfolioItem foreach ($catData as $num => $category) print($category.'<br/>'."\n"); // print each category under portfolio print('<br/>'."\n"); } } print_r($catResultArray); Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/#findComment-1009532 Share on other sites More sharing options...
spenceddd Posted February 9, 2010 Author Share Posted February 9, 2010 Great thanks for the help guys. I haven't had time to try it out yet bt thanks a lot for the response! Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/#findComment-1009581 Share on other sites More sharing options...
spenceddd Posted February 9, 2010 Author Share Posted February 9, 2010 Catfish I tried your code and it printed this: Array ( [0] => Array ( ) [] => Array ( [0] => 18 [1] => 7 [2] => 5 [3] => 4 [4] => 48 [5] => 7 [6] => 7 [7] => 5 [8] => 4 [9] => 48 [10] => 18 [11] => 7 [12] => 5 [13] => 4 ) ) Looks like it's nearly there. Ill try and work out what went wrong. Thanks again. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/#findComment-1009620 Share on other sites More sharing options...
spenceddd Posted February 9, 2010 Author Share Posted February 9, 2010 Got it. Just a small typo - 'portfolioItems' Quote Link to comment https://forums.phpfreaks.com/topic/191506-returning-an-array-in-a-specific-format-from-mysql-request/#findComment-1009633 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.