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

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
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

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

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what have you done so far?
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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));
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply