Simple question about the WHILE Loop structure

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
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Simple question about the WHILE Loop structure

Post by santacrux »

Hello,

I'm unsure on how to structure a code correctly to give a command within my WHILE loop after executing my IF statement.

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
	
	if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
	
	print "Table $table is unavailable for $redate. Please select another table or another day.";

}
}

I would like to instruct it to print 'Available' if the 'IF' statement is not true.

My attempt:

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
	
	if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
	
	print "Table $table is unavailable for $redate. Please select another table or another day.";

} else {
print "Available";
}

But the "ELSE" statement doesn't appear to work on a "WHILE" loop correctly (I tried the "do {} while (); as well) .....
can you help me formatted?

Thank you
Last edited by Benjamin on Thu Apr 08, 2010 7:17 pm, edited 1 time in total.
Reason: Removed HELPPPPP from topic title - Rule Violation
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: HELLLPPPPPP Simple question about the WHILE Loop struct

Post by AbraCadaver »

Count your braces { and }. You are missing the closing } for the while loop. It helps to indent correctly to see, and possibly use an ending comment while you're learning. I suggest an editor with syntax highlighting:

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
       
        if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
            print "Table $table is unavailable for $redate. Please select another table or another day.";
        } else {
            print "Available";
        } //endif
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: HELLLPPPPPP Simple question about the WHILE Loop struct

Post by santacrux »

AbraCadaver wrote:Count your braces { and }. You are missing the closing } for the while loop. It helps to indent correctly to see, and possibly use an ending comment while you're learning. I suggest an editor with syntax highlighting:

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
       
        if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
            print "Table $table is unavailable for $redate. Please select another table or another day.";
        } else {
            print "Available";
        } //endif
I forgot to insert my last bracket, but its present on my file....but its still not working....check it >>
Thanks for the help

This is my full page coding

Code: Select all

<?php require_once('../Connections/Reservations.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_checkorders = 10;
$pageNum_checkorders = 0;
if (isset($_GET['pageNum_checkorders'])) {
  $pageNum_checkorders = $_GET['pageNum_checkorders'];
}
$startRow_checkorders = $pageNum_checkorders * $maxRows_checkorders;

mysql_select_db($database_Reservations, $Reservations);
$query_checkorders = "SELECT orders.tablenumber, orders.`date` FROM orders";
$query_limit_checkorders = sprintf("%s LIMIT %d, %d", $query_checkorders, $startRow_checkorders, $maxRows_checkorders);
$checkorders = mysql_query($query_limit_checkorders, $Reservations) or die(mysql_error());
$row_checkorders = mysql_fetch_assoc($checkorders);

if (isset($_GET['totalRows_checkorders'])) {
  $totalRows_checkorders = $_GET['totalRows_checkorders'];
} else {
  $all_checkorders = mysql_query($query_checkorders);
  $totalRows_checkorders = mysql_num_rows($all_checkorders);
}
$totalPages_checkorders = ceil($totalRows_checkorders/$maxRows_checkorders)-1;

$table = $_POST['table'];
$redate = $_POST['ADate'];


if ($table && $redate){
} else {
	print "Not submitted";
}	
// PROBLEM HERE >>>>>>>>>>>>>>>


while ($row_checkorders = mysql_fetch_assoc($checkorders)){
	
	if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
	
	print "Table $table is unavailable for $redate. Please select another table or another day.";

} else  {
	print "Available";
}
}
/*
//This wouldnt work either...
do { 
if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
	
	print "Table $table is unavailable for $redate. Please select another table or another day.";
} else {
	print "Available";
	}
} while ($row_checkorders = mysql_fetch_assoc($checkorders));

*/
?>
</body>
</html>
<?php
mysql_free_result($checkorders);
?>
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: HELLLPPPPPP Simple question about the WHILE Loop struct

Post by minorDemocritus »

Try adding the echo lines:

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    echo 'While loop entered.<br />';
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        echo 'If statement TRUE.<br />';
        print "Table $table is unavailable for $redate. Please select another table or another day.";
    } else {
        echo 'If statement FALSE.<br />';
        print "Available";
    }
}
What is the output of the script after adding those lines?
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: HELLLPPPPPP Simple question about the WHILE Loop struct

Post by minorDemocritus »

As an aside, this is pretty scary:

Code: Select all

if (isset($_GET['pageNum_checkorders'])) {
  $pageNum_checkorders = $_GET['pageNum_checkorders'];
}
$startRow_checkorders = $pageNum_checkorders * $maxRows_checkorders;
// SNIP
$query_limit_checkorders = sprintf("%s LIMIT %d, %d", $query_checkorders, $startRow_checkorders, $maxRows_checkorders);
$checkorders = mysql_query($query_limit_checkorders, $Reservations) or die(mysql_error());
You're apparently putting raw user input into a string, then putting that string into an SQL query, without escaping it first. That's a potentially huge SQL injection vulnerability. mysql_real_escape_string() is a nice function.
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: HELLLPPPPPP Simple question about the WHILE Loop struct

Post by santacrux »

minorDemocritus wrote:Try adding the echo lines:

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    echo 'While loop entered.<br />';
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        echo 'If statement TRUE.<br />';
        print "Table $table is unavailable for $redate. Please select another table or another day.";
    } else {
        echo 'If statement FALSE.<br />';
        print "Available";
    }
}
What is the output of the script after adding those lines?
Thanks for your response.
I tried using the echo instead, but no luck, maybe the script could be arranged differently,
this is what im trying to accomplish.

Table reservations on desired days, but if the table is reserved for a specific day that the user inputs the program should spit out that that table is reserved for that date.

Steps i took:
1. Connect to database
2. Receive user data from form (table and date)
3. Compare user data to mysql database table & date rows
4. If there is equal data on the table and date rows to what the user submitted, echo "Unavailable"
5. But if its not equal echo "Available"

I hope this helps, thanks for your help
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Simple question about the WHILE Loop structure

Post by minorDemocritus »

Well hold on... I didn't change anything, I just added lines to assist in debugging.

What is the output of the script as is?
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Simple question about the WHILE Loop structure

Post by minorDemocritus »

santacrux wrote:What im trying to accomplish.
See viewtopic.php?f=1&t=115106

Duplicate (or close to duplicate) threads are bad form, santacrux. Stick to one thread about a particular code block, so we (and you) don't get confused.

I recommend trying out my suggestion (adding the three echo statements), and run the code. Post the output from that while loop block, and we can see what's running, and what's not.
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: Simple question about the WHILE Loop structure

Post by santacrux »

minorDemocritus wrote:Well hold on... I didn't change anything, I just added lines to assist in debugging.

What is the output of the script as is?

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    echo 'While loop entered.<br />';
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        echo 'Table $table is unavailable for $redate. Please select another table or another day.<br />';
    } else {
        echo 'Available.<br />';
    }
}
For available the output is:

While loop entered.
Available.
While loop entered.
Available.
While loop entered.
Available.
While loop entered.
Available.
While loop entered.
Available

For unavailable the output is:

While loop entered.
Table 2 is unavailable for 04/10/2010. Please select another table or another day.
While loop entered.
Available.
While loop entered.
Available.
While loop entered.
Available.
While loop entered.
Available.

On the second line is says unavailable, but the script still prints out Available several times.
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Simple question about the WHILE Loop structure

Post by minorDemocritus »

santacrux wrote:On the second line is says unavailable, but the script still prints out Available several times.
Now THAT makes the problem much clearer, don't you agree?

Code: Select all

while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    echo 'While loop entered.<br />';
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        echo 'Table $table is unavailable for $redate. Please select another table or another day.<br />';
    } else {
        echo 'Available.<br />';
    }
}
This code loops over the $checkorders query resource. That query (in your example) is apparently grabbing 5 (EDIT, actually 6, see next post) rows from the database... see the manual page for mysql_fetch_assoc():
PHP Manual wrote: Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
Notice the emphasis. So, the reason I know that the $checkorders resource has 5 rows in it, is because each time the WHILE loop runs, it does one of two things:

Case 1. If $row_checkorders is not false, that is, if mysql_fetch_assoc() pulled a row from the $checkorders resource, then run the code in between the WHILE loop's brackets.
Case 2. If $row_checkorders is false, that is, if mysql_fetch_assoc() did not find any more rows, then skip the contents of the WHILE loop's bracket, and continue on with the rest of the script.

Therefore, case 1 happens 5 times, and then case 2 happens.


So, you're either expecting the SQL query to only return 1 row, or, you thought mysql_fetch_assoc() pulls the whole resource. Now tell me, what exactly do you expect (or want) the script to do?
Last edited by minorDemocritus on Fri Apr 09, 2010 3:36 pm, edited 5 times in total.
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Simple question about the WHILE Loop structure

Post by minorDemocritus »

Also, keep in mind that...

Code: Select all

$row_checkorders = mysql_fetch_assoc($checkorders);
...happened earlier in the script, way before the WHILE loop. So, the $checkorders resource has 6 rows in it.
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: Simple question about the WHILE Loop structure

Post by santacrux »

minorDemocritus wrote:Also, keep in mind that...

Code: Select all

$row_checkorders = mysql_fetch_assoc($checkorders);
...happened earlier in the script, way before the WHILE loop. So, the $checkorders resource has 6 rows in it.
Ok now i understand,
what i want the script to do, is that it should compare the data that a user submitted to all the rows in the database and if it finds a row that is the same then it should echo/print unavailable otherwise say available.

Which would be the best function to use and how should i stop the loop right after comparing all the rows to the submitted data?
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: Simple question about the WHILE Loop structure

Post by santacrux »

santacrux wrote:
minorDemocritus wrote:Also, keep in mind that...

Code: Select all

$row_checkorders = mysql_fetch_assoc($checkorders);
...happened earlier in the script, way before the WHILE loop. So, the $checkorders resource has 6 rows in it.
Ok now i understand,
what i want the script to do, is that it should compare the data that a user submitted to all the rows in the database and if it finds a row that is the same then it should echo/print unavailable otherwise say available.

Which would be the best function to use and how should i stop the loop right after comparing all the rows to the submitted data?
Ok i changed it, since i had declared

Code: Select all

$row_checkorders = mysql_fetch_assoc($checkorders);
earlier,
i took out the while() function afterwards using only the if(){} statement

Code: Select all

 if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        echo "Table $table is unavailable for $redate. Please select another table or another day.<br />";
    } else {
        echo 'Available.<br />';
    }
This time it would not loop and spit out the else condition everytime regardless if true or false,
but now the script is only comparing the user data to the first row, im stuck (i only have a couple months learning php) thanks for bearing with me
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: Simple question about the WHILE Loop structure

Post by minorDemocritus »

Code: Select all

$dateSame = false;
while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        $dateSame = true;
        break;
    }
}
if ($dateSame) {
    echo 'Table $table is unavailable for $redate. Please select another table or another day.<br />';
} else {
    echo 'Available.<br />';
}
If the while loop finds any rows that have the same date as was submitted, it will set $dateSame to true, and break out of the loop.
santacrux
Forum Newbie
Posts: 8
Joined: Thu Apr 08, 2010 12:51 pm

Re: Simple question about the WHILE Loop structure

Post by santacrux »

minorDemocritus wrote:

Code: Select all

$dateSame = false;
while ($row_checkorders = mysql_fetch_assoc($checkorders)){
    if ($_POST['table'] == $row_checkorders['tablenumber'] && $_POST['ADate'] == $row_checkorders['date']){
        $dateSame = true;
        break;
    }
}
if ($dateSame) {
    echo 'Table $table is unavailable for $redate. Please select another table or another day.<br />';
} else {
    echo 'Available.<br />';
}
If the while loop finds any rows that have the same date as was submitted, it will set $dateSame to true, and break out of the loop.


You're awesome, thank you very much it worked perfect!

I just had to change

Code: Select all

    echo 'Table $table is unavailable for $redate. Please select another table or another day.<br />';
to double quotes so the $table and $redate would propagate correctly, but other than that it worked!
thanks! :D
Post Reply