taymax Posted June 12, 2010 Share Posted June 12, 2010 Hi Im creating a nav bar and i need to store the multiple fields from multiple rows of mysql into an array so i can return from a function. Ive tried 2d arrays and im sure i screwed up there. The array will store data in an string location. for e.g $navArr['idnav'] = $row['idnav']; here is a code thts i donr which is incomplete n screwd up really while($row = mysql_fetch_array($res)) { $navArr = array(); $navArr = $title = array("title"=>$row['title']); return $navArr; } The goal is to have $navArr[0] [titlevalues] $navArr[1] [titlevalues] sorry if i dont make sense. first time im using help forum and havent touched php for a few yrs n my brain is gne explode soon :'( Link to comment https://forums.phpfreaks.com/topic/204585-problem-storing-values-from-mysql-into-an-array/ Share on other sites More sharing options...
kenrbnsn Posted June 12, 2010 Share Posted June 12, 2010 Change <?php while($row = mysql_fetch_array($res)) { $navArr = array(); $navArr = $title = array("title"=>$row['title']); return $navArr; } ?> to <?php $navArr = array(); while($row = mysql_fetch_assoc($res)) { $navArr[] = $row['title']; } ?> Notes: You were re-initializing the array each time through the loop. The "return" statement is only used in functions. Ken Link to comment https://forums.phpfreaks.com/topic/204585-problem-storing-values-from-mysql-into-an-array/#findComment-1071180 Share on other sites More sharing options...
taymax Posted June 12, 2010 Author Share Posted June 12, 2010 Tnx for the reply. I made a mistake in the code. i just copied it out, from fraustration and i think this is a more clear version function get_nav(){ $sql = "SELECT * FROM nav"; $res = mysql_query($sql)or die(mysql_error()); $navArr = array(); $field = array(); while($row = mysql_fetch_array($res)) { $navArr[] = $field["title" => $row['title'],"link" => $row['link']]; return $navArr; } output should be something like : $navArr[0] = $field["title" = Home,"link" = index.php]; $navArr[1] = $field["title" = Page2,"link" = page2.php]; $navArr[2] = $field["title" = Page3,"link" = page3.php]; Link to comment https://forums.phpfreaks.com/topic/204585-problem-storing-values-from-mysql-into-an-array/#findComment-1071233 Share on other sites More sharing options...
kenrbnsn Posted June 12, 2010 Share Posted June 12, 2010 Move the "return" statement outside the while loop. Also why are you using $field? Just do: <?php function get_nav(){ $sql = "SELECT title, link FROM nav"; $res = mysql_query($sql)or die(mysql_error()); $navArr = array(); while($row = mysql_fetch_assoc($res)) { $navArr[] = array("title" => $row['title'],"link" => $row['link']); } return ($navArr); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/204585-problem-storing-values-from-mysql-into-an-array/#findComment-1071235 Share on other sites More sharing options...
taymax Posted June 12, 2010 Author Share Posted June 12, 2010 Thank u for your response This is such a stupid mistake I didnt put the stupid square brackets in front of "$navArr" and its was overwriting the prevoius data "obviously". plus i screwed up the return. This seems to work : function get_nav(){ $sql = "SELECT * FROM nav"; $res = mysql_query($sql)or die(mysql_error()); $navArr = array(); while($row = mysql_fetch_array($res)) { $navArr[] = array("title" => $row['title'],"link" => $row['link']); } return $navArr; } Output: Array ( [0] => Array ( [title] => Home [link] => index.php ) [1] => Array ( [title] => Page 2 [link] => ?pid=2 ) ) Thank you for your time Link to comment https://forums.phpfreaks.com/topic/204585-problem-storing-values-from-mysql-into-an-array/#findComment-1071238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.