Array Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

Array Problem

Post 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
Last edited by Crew on Fri Sep 02, 2005 11:33 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Array Problem

Post 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);		 
	}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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!
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Thats why its good practice to have a lot of debugging output and tests :wink:
Post Reply