View Full Version : While Looping Problem
kochier
12-11-2008, 08:36 PM
$result = mysql_query("SELECT Word1 FROM greendoor") or die(mysql_error());
while($row = mysql_fetch_array($result)){
$Word1 = $row['Word1'];
if ($Word1 == $Nam)
{
$result2 = mysql_query("SELECT Word1, Word2, Plural FROM greendoor WHERE Word1 = '$Nam'") or die(mysql_error());
while($row = mysql_fetch_array($result2)){
$Word2 = $row['Word2'];
$Plural = $row['Plural'];
{
if ($Plural == '0')
{
echo 'There is a '. $Nam. ' through the Green Glass Door, but there are no '. $Word2. '.';
}
else
{
if ($Plural == '1')
{
echo 'There are '. $Nam. ' through the Green Glass Door, but there is no '. $Word2. '.';
}}}}}
else
{
echo $Nam.' will go through the Green Glass Door.';
What I want this to do is when someone inputs a word through the green glass door it's checked to see if it's in the database, if it is it's displayed with its counterpart, if not it simply states whether it can go through or not. The problem is if I have more than one entry in the database it goes through them all, checking for a match, and each time there isn't a match it will say so. However if I tell it to stop after one loop it probably won't find the match and just go to the default statement. I'm fairly positive I'm structuring it wrong, but I've changed it over and over again and it won't work. Thanks in advance for any help on this matter.
Fumigator
12-11-2008, 08:58 PM
You don't need the first query. The second query filters the results with the WHERE clause, that's all you need. If no rows matching the conditions in your WHERE are found, the function mysql_num_rows() will return 0. So check that function and if it's 0, you know that condition doesn't exist. Otherwise it does.
kochier
12-11-2008, 09:24 PM
mysql_select_db('greendoor') or die('Cannot select database');
$result = mysql_query("SELECT Word1, Word2, Plural FROM greendoor WHERE Word1 = '$Nam'") or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (mysql_num_rows($result) != 0) {
$Word2 = $row['Word2'];
$Plural = $row['Plural'];
{
if ($Plural == '0')
{
echo 'There is a '. $Nam. ' through the Green Glass Door, but no '. $Word2. '.';
}
else
{
if ($Plural == '1')
{
echo 'There are '. $Nam. ' through the Green Glass Door, but no '. $Word2. '.';
}}}}}}
else
{
echo $Nam.' will go through the Green Glass Door.';
Is what I now have, the test words I am using are "trees", "hel" "helll" and "hell". Trees work fine, it says there's trees but no soil, same as hell which is also in the database, however nothing shows up for hell, and it says hel will go through when it shouldn't (only double lettered words should go through).
kochier
12-11-2008, 09:51 PM
$result = mysql_query("SELECT Word1, Word2, Plural FROM greendoor WHERE Word1 = '$Nam'") or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (mysql_num_rows($result) != 0) {
$Word2 = $row['Word2'];
$Plural = $row['Plural'];
{
if ($Plural == '0')
{
echo 'There is a '. $Nam. ' through the Green Glass Door, but no '. $Word2. '.';
}
else
{
if ($Plural == '1')
{
echo 'There are '. $Nam. ' through the Green Glass Door, but no '. $Word2. '.';
}}}}}}
else
{
if (mysql_num_rows($result) == 0) {
echo $Nam.' will go through the Green Glass Door.';
I have also tried this, with a check if it's equal and not equal, but nothing happens to words that are not in the database now.
Fumigator
12-11-2008, 09:53 PM
while($row = mysql_fetch_array($result)){
You need to understand that line of code before you move on.
http://us.php.net/manual/en/control-structures.while.php
The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.
Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.
http://us.php.net/manual/en/language.expressions.php
One last thing worth mentioning is the truth value of expressions. In many events, mainly in conditional execution and loops, you're not interested in the specific value of the expression, but only care about whether it means TRUE or FALSE. The constants TRUE and FALSE (case-insensitive) are the two possible boolean values. When necessary, an expression is automatically converted to boolean. See the section about type-casting for details about how.
kochier
12-11-2008, 10:12 PM
Okay so what you're saying is if the words don't match nothing in the while loop will run, which makes sense. So what I'm confused about is where to put my echo statement then, it can't be in the while statement since it won't run when I need it to, and if I put right after it will always run, as there will be no if statement to stop it.
Fumigator
12-11-2008, 11:19 PM
Maybe what you're missing is the fact that mysql_num_rows() does not need mysql_fetch_array() in order to work. You can call mysql_num_rows() immediately after the mysql_query() call to see if anything is in the resultset.
kochier
12-12-2008, 04:44 AM
Yes, thank you very much, it works fine now. Being able to use that outside of the while loop fixed everything up.
kochier
12-12-2008, 06:13 AM
else
{
include 'library/opendb.php';
mysql_select_db('greendoor') or die('Cannot select database');
$result2 = mysql_query("SELECT Word2, Plural FROM greendoor WHERE Word1 = '$Nam'") or die(mysql_error());
if (mysql_num_rows($result2) != 0) {
while($row = mysql_fetch_array($result)){
$Word2 = $row['Word2'];
$Plural = $row['Plural'];
{
if ($Plural == '0')
{
echo 'There is no '. $Nam. ' through the Green Glass Door, but there is a '. $Word2. '.';
}
else
{
if ($Plural == '1')
{
echo 'There are no '. $Nam. ' through the Green Glass Door, but there are '. $Word2. '.';
}}}}}
else
{
echo $Nam.' will not go through the Green Glass Door.';$fname = 'library/greendoor.php';
$fhandle = fopen($fname,"r");
Okay so this comes later in my code, for words that don't go through the door. Now what's happening is words that don't go through and are in the database don't show up at all, the while loop doesn't run. Words that don't go through still show up.
EDIT: fixed. Was using result instead of result2 in my while loop, spent an hour or so on it, just fixed it after posting this, strange how that happens. Thanks again for the help.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.