SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loop within a loop

    Hi guys

    Hope you can help me


    My first loop creates the times


    PHP Code:
    $StartTime StrToTime("$start_date");
    $EndTime StrToTime("$end_date");

    for(
    $i $StartTime$i <= $EndTime$i += 60 60){ //1 hour. I put the one so that it's easier for you to change to, say, 2 hours etc

    $temp_array[] = $i;




    I then need to run this array against it

    PHP Code:
    $check = array("08:30""10:30"); 
    and if it matches then echo a statement. The problem I have it shows every time twice like this


    07:30 Add Booking
    07:30 Add Booking
    08:30 Match
    08:30 Add Booking
    09:30 Add Booking
    09:30 Add Booking
    10:30 Add Booking
    10:30 Match
    11:30 Add Booking
    11:30 Add Booking
    12:30 Add Booking
    12:30 Add Booking
    13:30 Add Booking
    13:30 Add Booking
    14:30 Add Booking
    14:30 Add Booking
    15:30 Add Booking
    15:30 Add Booking
    16:30 Add Booking
    16:30 Add Booking

    It works but shows every time twice. How can I stop this but still the matched times


    Thanks in advance

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Peterborough, Ontario, Canada
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the code you have for displaying the echo (e.g. "Match")?

    I would just nest an if statement into your for loop that checks each entry in your $check array.
    Have I helped you? You could help me.
    Like my business on facebook!
    Text message polls with real-time, live results.

  3. #3
    SitePoint Member BeetMan's Avatar
    Join Date
    May 2009
    Location
    Fiji
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you give a larger snippet of the code specifically the code that outputs the text I can help you more.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing you're using something similar to the code below. Since in your second loop you are checking against more than one value (08:30 and 10:30), you can only use one condition, if (). You were probably using if else. The code below should work.
    PHP Code:
    $start_date '7:30';
    $end_date '16:30';
    $StartTime strtotime($start_date);
    $EndTime strtotime($end_date);

    for(
    $i $StartTime$i <= $EndTime$i += 60 60) { //1 hour. I put the one so that it's easier for you to change to, say, 2 hours etc
      
    $temp_array[] = $i;


    $check = array('08:30''10:30'); 

    foreach (
    $temp_array as $time1) {

      
    $status ' Add Booking';
      foreach (
    $check as $time2) {
        if (
    $time1 == strtotime($time2)) {
          
    $status ' Match';
        }
      }
      print 
    date('H:i',$time1).$status."<BR>\n";




Bookmarks

Posting Permissions

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