0

I have try to find the solution in stackoverflow to fix my problem but no one of them match with my problem.

my problem is;

I have if statement inside my javascript which inside if statement have a popup. I want my javascript display the popup for 1 time only if if statement meet the condition. I try to use break inside if statement, but it still show a popup for a several time.

code:

<?php
if(isset($_POST['submit'])){ 

$reserve_stime = htmlentities($_POST['starttime']);
$reserve_etime = htmlentities($_POST['endtime']);

$reservedstime = "SELECT startmeetingtime, endmeetingtime 
                  FROM meeting_room_reservation 
                  WHERE room_reserve_session = '$varsession' 
                  AND room_reserve_date = '$convardate'
                  AND room_id = $idroom";
$result7=mysql_query($reservedstime) or die("invalid query:".mysql_error()); 
while($row7=mysql_fetch_assoc(@$result7)){
     $reservedstime2 = "SELECT timereserve_start_id
                        FROM timereserve_start 
                        WHERE timereserve_start_id BETWEEN " . $row7['startmeetingtime'] . " AND " . $row7['endmeetingtime'] . "";
     $result8=mysql_query($reservedstime2) or die("invalid query:".mysql_error()); 

     while ($row8=mysql_fetch_assoc(@$result8)) {
           $timealreadyreserved = $row8['timereserve_start_id'];

           echo "<script type='text/javascript'>

                    for (i=$reserve_stime; i <= $reserve_etime ; i++){

                      if (i == $timealreadyreserved){
                        alert('Time is not Available');
                        break;
                      }
                      else{
                        //do nothing
                      }
                    }

                 </script>";

     }

  }
}
?>

my friends said, the problem is before for loop have do while loop, so that is why the popup still display for a several time even already put the break inside if statement

any idea? thank you, faizal

Fai Zal Dong
  • 323
  • 6
  • 25
  • Well, your friend is onto something. Your JavaScript block is being repeated inside your php while loop. Now that the problem is clear, how have you tried to solve it, and what exactly are you stuck on? – Krease Dec 03 '15 at 02:45
  • what I stuck now is, the popup keep display several time, I want the popup display for 1 time only. Once meet the if condition display popup then terminate all loop.. @Chris – Fai Zal Dong Dec 03 '15 at 02:49

1 Answers1

1

You can use a boolean variable(like $flag) to check whether the condition has been met or not, and subsequently break out of the loop, like this:

// your code

while($row7=mysql_fetch_assoc(@$result7)){
    $reservedstime2 = "SELECT timereserve_start_id
                        FROM timereserve_start 
                        WHERE timereserve_start_id BETWEEN " . $row7['startmeetingtime'] . " AND " . $row7['endmeetingtime'] . "";
    $result8=mysql_query($reservedstime2) or die("invalid query:".mysql_error()); 

    $flag = false;
    while($row8=mysql_fetch_assoc(@$result8)) {
        $timealreadyreserved = $row8['timereserve_start_id'];

        for ($i=$reserve_stime; $i <= $reserve_etime ; $i++){
            if($i == $timealreadyreserved){
                $flag = true;
                ?>
                <script>alert("Time is not Available");</script>
                <?php
                break;
            }
        }
        if($flag){
            break;
        }
    }
    if($flag){
        break;
    }

}

// your code 

Caution: Do not mix javascript and php in your code, like you did in inner for loop.

Sidenote: Don't use mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37