dsaba Posted March 5, 2007 Share Posted March 5, 2007 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) Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/ Share on other sites More sharing options...
trq Posted March 5, 2007 Share Posted March 5, 2007 Sorry, but you really need to learn to indent your code. I cannot read that at all. Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/#findComment-200113 Share on other sites More sharing options...
dsaba Posted March 5, 2007 Author Share Posted March 5, 2007 damn please try to read it, i don't know how to indent where it would please you but indent or no indent i still only inserts 1 value into keywords table instead of 4 Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/#findComment-200121 Share on other sites More sharing options...
Barand Posted March 5, 2007 Share Posted March 5, 2007 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. Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/#findComment-200239 Share on other sites More sharing options...
dsaba Posted March 5, 2007 Author Share Posted March 5, 2007 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 Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/#findComment-200303 Share on other sites More sharing options...
interpim Posted March 5, 2007 Share Posted March 5, 2007 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 FREE http://www.pspad.com/ Link to comment https://forums.phpfreaks.com/topic/41296-solved-while-loop-does-not-function-correctly-why/#findComment-200305 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.