Page 1 of 1

Array Problem

Posted: Fri Sep 02, 2005 11:30 am
by Crew
Ok I feel like a right fool at the moment, and I'm sure one of you will make me feel worse lol.

Code: Select all

$username_area_id=$_SESSION['username_login'];
	$result_area_id=mysql_query("SELECT helpdesk_areas.id, helpdesk_areas.aname FROM helpdesk_users, helpdesk_areas2users, helpdesk_areas WHERE helpdesk_users.username='$username_area_id' AND helpdesk_users.id=helpdesk_areas2users.user_id AND helpdesk_areas2users.area_id=helpdesk_areas.id");
	$num_area_id=mysql_numrows($result_area_id);
	$i_area_id=0;
	$array_area_info = array();
	while ($i_area_id < $num_area_id) {
		$temp_area_id = mysql_result($result_area_id,$i_area_id,"helpdesk_areas.id");
		$temp_area_aname = mysql_result($result_area_id,$i_area_id,"helpdesk_areas.aname");

		$array_area_info = array($i_area_id => array("id" => "$temp_area_id","aname" => "$temp_area_aname"));		 
		$i_area_id++;
	}
I cant seem to get the array to remember the previous while loops. Does anyone have any idea on a better way for me to do this? The only problem I'm having is the data seems to be added to the array, then the array id counts up on the next while loop and the previous data is gone. grrr

Re: Array Problem

Posted: Fri Sep 02, 2005 11:32 am
by feyd

Code: Select all

$username_area_id=$_SESSION['username_login'];
	$result_area_id=mysql_query("SELECT helpdesk_areas.id, helpdesk_areas.aname FROM helpdesk_users, helpdesk_areas2users, helpdesk_areas WHERE helpdesk_users.username='$username_area_id' AND helpdesk_users.id=helpdesk_areas2users.user_id AND helpdesk_areas2users.area_id=helpdesk_areas.id");
	$num_area_id=mysql_numrows($result_area_id);
	$i_area_id=0;
	$array_area_info = array();
	while ($i_area_id < $num_area_id) {
		$temp_area_id = mysql_result($result_area_id,$i_area_id,"helpdesk_areas.id");
		$temp_area_aname = mysql_result($result_area_id,$i_area_id,"helpdesk_areas.aname");

		$array_area_info[$i_area_id++] = array("id" => $temp_area_id,"aname" => $temp_area_aname);		 
	}

Posted: Fri Sep 02, 2005 11:32 am
by Burrito
you're resetting the array varible with every iteration of the loop.

you need to just set the var above the loop and then append to it.

edit:

damn you feyd, you're too fast!

Posted: Fri Sep 02, 2005 11:37 am
by Crew
Don't you just hate it when you've spent 2 hours fiddling and its that simple. Thanks a lot guys its much appreciated. And I can't believe how quick that was lol.

Posted: Fri Sep 02, 2005 11:38 am
by s.dot
a lot of the time you concentrate on the specifics of the code and the problem is WHAM right in your face :P

Posted: Fri Sep 02, 2005 11:50 am
by John Cartwright
Thats why its good practice to have a lot of debugging output and tests :wink: