Page 1 of 1

How do I display 2 individual values in a single array loop

Posted: Tue Nov 21, 2006 2:46 pm
by amir
I have a question about arrays and how to loop thru 'em.

I need to loop through a query where the loop needs to run 5 times, basically for 5 weeks in a month. Each time it checks to see if a date is between the 1st and 7th, then 8th to 14th, then 15th to 22nd , then 23rd to 29th and then 29th to whatever last date is. So how do i frame my array and use the foreach loop.

If it was just one value I am looping through i know how to do that, but how do i do it for 2 values in one cycle?

Posted: Tue Nov 21, 2006 2:47 pm
by John Cartwright
what have you done so far?

Posted: Tue Nov 21, 2006 2:49 pm
by amir
Here is part of my query:

Code: Select all

WHERE date_created >= '2006-" . $fromMonth . "-01' AND date_created <= '2006-" . $fromMonth . "-07
The 01 and 07 would change on every loop, like i mentioned in my earlier comment

Posted: Tue Nov 21, 2006 2:56 pm
by Chris Corbyn
I have no idea what you're asking. You want to loop over something which contains those two values to change? You need a 2-dimensional array like:

Code: Select all

$array = array();
$array[] = array('01', '03');
$array[] = array('07', '08');

$query = "select * from tbl_foo where month between '?x?' and '?y?'";

foreach ($array as $sub_array)
{
    $result = mysql_query(str_replace(array('?x?', '?y?'), $sub_array, $query));
}

Posted: Tue Nov 21, 2006 3:06 pm
by John Cartwright

Code: Select all

for ($x = 1; $x < 6; $x++) {
	$start = !isset($start) ? 1 : $start + 7;
	$end = ($x == 5) ? date('t', strtotime('2006-'.$fromMonth.'-01')) : $x * 7;
}
I think your looking for something more along the lines of this..

Posted: Tue Nov 21, 2006 4:49 pm
by RobertGonzalez
I suggest you select a complete month worth of data, then loop the result. It will be a heck of a lot less intensive than querying inside a loop.