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?
How do I display 2 individual values in a single array loop
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Here is part of my query:
The 01 and 07 would change on every loop, like i mentioned in my earlier comment
Code: Select all
WHERE date_created >= '2006-" . $fromMonth . "-01' AND date_created <= '2006-" . $fromMonth . "-07- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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));
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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;
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA