Jump to content

Problem storing values from mysql into an array


taymax

Recommended Posts

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'];

:o

 

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 :'(

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

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  :D

 

		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];

 

 

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

Thank u for your response

 

This is such a stupid mistake  :D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.