Jump to content

Array Problem


murfitUK

Recommended Posts

I'm having difficulty working out how to solve this problem. I run a mysql query

// find other types of leave in period (flexi, sick, paid, unpaid)	$slq = "SELECT `type`, `status`, SUM(`len`)/2 AS `tot` FROM `lvd` WHERE `user`={$_SESSION['userid']} AND `period`={$row['id']} GROUP BY `type`, `status`;";	if (!($connection = @ mysql_connect($hostname, $dbuser, $dbpass))) die("Cannot connect");	if (!(mysql_select_db($dbname, $connection))) showerror();	if (!($slr = mysql_query($slq, $connection))) showerror();	while ($data[] = mysql_fetch_array($slr,MYSQL_ASSOC)) {}

and printing $data with <pre> tags gives:

Array(	[0] => Array		(			[type] => A			[status] => A			[tot] => 0.5		)	[1] => Array		(			[type] => A			[status] => P			[tot] => 2.0		)	[2] => Array		(			[type] => F			[status] => A			[tot] => 1.0		)	[3] => Array		(			[type] => F			[status] => P			[tot] => 0.5		)	[4] => )

Type A is annual leave, F-Flexi, S-sick leave, P-other paid leave, U-other unpaid leaveStatus A-approved, P-Pending, D-Declined.I now feed this info into a table but the data needs to appear in a specific order. How do I pull out the value tot for, eg, type F, status A?

Link to comment
Share on other sites

Other than just looping through the array, you can also use a function like array_walk to run a function on each array element:http://www.php.net/manual/en/function.array-walk.phpYou can use array_multisort to sort the array:http://www.php.net/manual/en/function.array-multisort.phpOr you can also sort the data in the SQL query, which would probably be more efficient.If you just want to look for a certain element in the array to print, it's probably best to just loop through the array until you find what you're looking for, then print it, then break out of the loop.

Link to comment
Share on other sites

you can access any array member by writing the index value of the arrayecho $data[2]['type']; //will echo Fecho $data[2]['tot']; //will echo 1.0echo $data[2]['status'];//will echo A

Link to comment
Share on other sites

The problem is that I don't know what info will be returned. Some staff might not have any sick leave (type S) or unpaid leave (type U) so I can't rely on $data[0] always containing type S info. If he hasn't had any sick leave then $data[0] might contain type F (flexi leave) info.Is there any way of taking the mysql result and turning it into an array with `type` as the keys. If that is possible then I might be able to get the info that way.

Link to comment
Share on other sites

justsomeguy already show the way..if you want to get the data based upon the type you may add AND type='$type' in WHERE clause. so that those data will be returned who had matched the $type.or may be putting some condition in foreach loop which will check the type and will do something based on the type.its depend on how you are going to work with the data.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...