Jump to content

[SOLVED] while loop does not function correctly why?


dsaba

Recommended Posts

i have a while loop that is supposed to loop based on the number of rows in a query

here is the code:

     while ($searchqueryrow = mysql_fetch_array($searchquery)) {
$filelink = formatSearch($searchqueryrow['filelink'], $trimmedsearchterm);
$filetotalmirrors = $searchqueryrow['filetotalmirrors'];
$filegeneraltype = $searchqueryrow['filegeneraltype_en'];
$filespecifictype = $searchqueryrow['filespecifictype_en'];
$filegeneraltypekey = $searchqueryrow['filegeneraltype_en'];
$filespecifictypekey = $searchqueryrow['filespecifictype_en'];
$filedateadded = $searchqueryrow['filedateadded'];
$filesubmitter = $searchqueryrow['filesubmitter'];
$fileid = $searchqueryrow['fileid'];
$keywordlink = <<<KEYWORDLINK
<a href="searchresults.php?searchterm=$keyword">$keyword</a>
KEYWORDLINK;


$keywordsquery = mysql_query("SELECT * FROM keywords WHERE keyword='$keyword' AND keywordfileid='$fileid' AND keywordip='$userip'");
$keywordsqueryrow = mysql_fetch_array($keywordsquery);

if ($keywordsqueryrow == NULL)
{
$insertkeywordsquery = "INSERT INTO `keywords` ( `keywordfileid`, `keyword`, `keywordspecifictype`, `keywordgeneraltype`, `keywordip`, `keyworduserid`, `keyworddatetime`, `keywordfilelink`, `keywordlink` )
VALUES ( '$fileid','$keyword','$filespecifictypekey','$filegeneraltypekey','$userip','$userid','$datetime', '$filelink', '$keywordlink' )";
$runinsertkeywordsquery = mysql_query($insertkeywordsquery);
echo "keywordquery was NULL so its a new keyword, it was inserted";
mysql_free_result($keywordsquery);
}
else
{
$updatekeywordsquery = mysql_query("UPDATE `keywords` SET `keyworddatetime`='$datetime', `keyworduserid`='$userid' WHERE `keyword`='$keyword' AND `keywordfileid`='$fileid' AND `keywordip`='$userip'");
echo "KEYWORDS TABLE WAS UPDATED ONLY";
mysql_free_result($updatekeywordsquery);
}


///count keywords in keywords table
$countkeywordquery = "SELECT keyword, COUNT(*) as howmany
          FROM keywords
          WHERE `keyword`='$keyword' AND `keywordfileid`='$fileid' AND `keywordip`='$userip'";
$runcountkeywordquery = mysql_query ($countkeywordquery);
$countkeywordrow = mysql_fetch_array($runcountkeywordquery);
$howmany = $countkeywordrow['howmany'];

//make $keydisplay variables
if ($howmany < 10)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#000000" font size="12">$keyword</font></a>
KEYDISPLAY;
}
elseif ($keycountplusone > 09)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#006600" font size="16">$keyword</font></a>
KEYDISPLAY;
}
elseif ($keycountplusone > 24)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#CC3300" font size="20">$keyword</font></a>
KEYDISPLAY;
}
elseif ($keycountplusone > 49)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#FF6600" font size="25">$keyword</font></a>
KEYDISPLAY;
}
elseif ($keycountplusone > 99)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#0000CC" font size="35">$keyword</font></a>
KEYDISPLAY;
}
elseif ($keycountplusone > 999)
{
$keydisplay = <<<KEYDISPLAY
<a href="searchresults.php?searchterm=$keyword"><font color="#000033" font size="45">$keyword</font></a> 
KEYDISPLAY;
}
$updatekeyquery = mysql_query("UPDATE `keywords` SET `keydisplay`='$keydisplay'");
echo "keywords table has been UPDATED with keydisplay";
mysql_free_result($runcountkeywordquery);

//now displaying search results in a table
$searchtableinside = <<<EOT
        <tr>
          <td class="nombre">$filelink</td>
          <td class="nombre">$filetotalmirrors</td>
          <td class="fecha">$filedateadded</td>
          <td class="descargas">$filespecifictype</td>
          <td class="descargas">$filesubmitter</td>
        </tr>
EOT;
echo $searchtableinside;
}
//end while statement

 

 

 

$searchqueryrow = mysql_fetch_array($searchquery)

in my test run I know that this will yield 4 rows, so everything inside while should repeat 4 times

 

then in my if/else statement concerning:

$keywordsqueryrow == NULL

 

i know that all the 4 times $searchqueryrow repeats it should have 4 different fileids

so then it should insert 4 different entries into keywords table

 

it only inserts 1 ! the first fileid, the first row from the main while statement

why?

 

someone told me i had to free results so I did that, but it still happens

-thank u (if u need more explanation i'm happy to do so)

Simple, you indent by using the "Tab" key at the start of the line.

 

$keywordsqueryrow = mysql_fetch_array($keywordsquery);

if ($keywordsqueryrow == NULL)

 

If no rows are found, mysql_fetch_array will return false, otherwise it will return an array of data. It will not be NULL.

 

 

I will start trying to indent to help me out and those who try to help me :)

 

the funny thing barant is that it does read the row as "NULL" it does the correctly 4 times

and it supposedly is supposed to insert 4 times when it does, but it only actually inserts 1 time

 

so maybe the problem is not because it cannot find it "NULL" it seems to do that well

 

however, i will try what you said and change it anyways since i am desperate to get this to work

its been 2 days trying to make it work

i recommend PSpad... it has syntax alerts for several languages, including php... also sql, html, css, etc.  and it tabs documents too, so if you need to switch back and forth between files quickly it helps a lot.

 

and the best part is, you can't beat the price :P  FREE  http://www.pspad.com/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.