Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Foreach help?

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Foreach help?

    Hello All,

    Have a quick question. I have a foreach statement that runs with a loop in order to produce some queries. Here's a snippett of it:

    PHP Code:
    $i=1;
    $users = array (3,4);
    foreach (
    $users as $user) {

    switch (
    TRUE) {
      case (
    $i == 1) : $start='09:00:00';$end='09:30:00';break;
      case (
    $i == 2) : $start='09:30:00';$end='10:00:00';break;
    }

    print 
    $SQL "SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 $start' AND estimated_end_time <= '2010-12-21 $end' AND user_id = $user"."<br />";

    $i++;


    Which prints as:

    Code:
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:00:00' AND estimated_end_time <= '2010-12-21 09:30:00' AND user_id = 3
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:30:00' AND estimated_end_time <= '2010-12-21 10:00:00' AND user_id = 4
    But what I want to do is loop ALL the queries for user_id = 3 and then run back and run the same queries for user_id = 4, so my printed queries look like:

    Code:
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:00:00' AND estimated_end_time <= '2010-12-21 09:30:00' AND user_id = 3
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:30:00' AND estimated_end_time <= '2010-12-21 10:00:00' AND user_id = 3
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:00:00' AND estimated_end_time <= '2010-12-21 09:30:00' AND user_id = 4
    SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 09:30:00' AND estimated_end_time <= '2010-12-21 10:00:00' AND user_id = 4
    Hope i've explained this OK? Anyone please offer any advice?

    Thanks

    Chris
    Last edited by stfc_boy; 12-21-2010 at 01:55 PM.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    try this
    PHP Code:
    $users = array (3,4);
    foreach (
    $users as $user) {
        for(
    $i 1$i <= 2$i++) {
            switch (
    $i) {
                case 
    $start='09:00:00';$end='09:30:00';break;
                case 
    $start='09:30:00';$end='10:00:00';break;
            }
            print 
    $SQL "SELECT * FROM `tbl` WHERE estimated_start_time >= '2010-12-21 $start' AND estimated_end_time <= '2010-12-21 $end' AND user_id = $user"."<br />";
        }


  • Users who have thanked poyzn for this post:

    stfc_boy (12-21-2010)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts
    Magic, Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •