dsaba Posted March 4, 2007 Share Posted March 4, 2007 can I put a while inside of another while? which looping method is the best to put inside each other? foreach? this is what i'm trying to do, will it work? so far it hasn't while (condition a) { echo "bla bla bla"; while (condition b) { echo "more bla blas"; } } Quote Link to comment https://forums.phpfreaks.com/topic/41079-can-i-put-several-whiles-inside-of-whiles/ Share on other sites More sharing options...
doni49 Posted March 4, 2007 Share Posted March 4, 2007 Sure it'll work--if you code it right. Try posting your code. In the meantime, I'll give you an example. $test = 1; while ($test){ echo "Line" . $test . ": "; $c = 1; while($c){ echo $c . "-"; if ($c == 10){ $c = false; echo "<br>"; } } if ($test == 10){$test = 0;} } I haven't tested this to see if there are any typos (hopefully not) but you should be able to get the gist of it. It should output the following: Line1: 1-2-3-4-5-6-7-8-9-10- Line2: 1-2-3-4-5-6-7-8-9-10- Line3: 1-2-3-4-5-6-7-8-9-10- Line4: 1-2-3-4-5-6-7-8-9-10- Line5: 1-2-3-4-5-6-7-8-9-10- Line6: 1-2-3-4-5-6-7-8-9-10- Line7: 1-2-3-4-5-6-7-8-9-10- Line8: 1-2-3-4-5-6-7-8-9-10- Line9: 1-2-3-4-5-6-7-8-9-10- Line10: 1-2-3-4-5-6-7-8-9-10- Quote Link to comment https://forums.phpfreaks.com/topic/41079-can-i-put-several-whiles-inside-of-whiles/#findComment-198929 Share on other sites More sharing options...
dsaba Posted March 4, 2007 Author Share Posted March 4, 2007 ok what i'm trying to do is this: 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']; $keywordtablequery = mysql_query("SELECT * FROM keywords WHERE keyword='$keyword'"); //--------------------------------------------------------------begin keyword table shit while ($keywordtablerow = mysql_fetch_array($keywordtablequery)) { $keywordtablequery = mysql_query("SELECT * FROM keywords WHERE keyword='$keyword'"); $keywordtablerow = mysql_fetch_array($keywordtablequery); $keywordfileid = $keywordtablerow['keywordfileid']; $matchedkeyword = $keywordtablerow['keyword']; echo $matchedkeyword; $keywordip = $keywordtablerow['keywordip']; $keywordentryid = $keywordtablerow['keywordentryid']; $keywordlink = <<<KEYWORDLINK <a href="searchresults.php?searchterm=$keyword">$keyword</a> KEYWORDLINK; if ($keywordfileid == $fileid && $keywordip == $userip && $matchedkeyword == $keyword) { $updatekeywordtablequery = mysql_query("UPDATE `keywords` SET `keywordtimestamp`='$datetime', `keywordip`='$userip', `keyworduserid`='$userid' WHERE `keywordentryid`='$keywordentryid'"); echo "KEYWORD TABLE WAS UPDATED ONLY should be no new ids if same search"; } else { $insertkeywordtablequery = "INSERT INTO `keywords` ( `keywordfileid`, `keyword`, `keywordspecifictype`, `keywordgeneraltype`, `keywordip`, `keyworduserid`, `keywordtimestamp`, `keywordfilelink`, `keywordlink` ) VALUES ( '$fileid','$keyword','$filespecifictypekey','$filegeneraltypekey','$userip','$userid','$datetime', '$filelink', '$keywordlink' )"; $runinsertkeywordtablequery = mysql_query($insertkeywordtablequery); echo "KEYWORD TABLE WAS INSERTED ONLY"; } } //-----------------------------------------------------------------------------------------------end keyword table shit $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 function and display searchtableinside break; } //end switch statement thats the real code i'm using here is the watered down version: while ($searchqueryrow = mysql_fetch_array($searchquery)) { i know $searchqueryrow will have 5 rows so all this shit should repeat 5 times while ($keywordtablerow = mysql_fetch_array($keywordtablequery)) { I know $keywordtablerow has 2 rows so this should repeat 10 times } } the result however is $searchqueryrow shit does loop 5 times, but $keywordtablerow doesn't loop at all the real code i'm using is pasted in the first code post Quote Link to comment https://forums.phpfreaks.com/topic/41079-can-i-put-several-whiles-inside-of-whiles/#findComment-198957 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 You are duplicating the query inside the loop that is processing the query $keywordtablequery = mysql_query("SELECT * FROM keywords WHERE keyword='$keyword'"); //--------------------------------------------------------------begin keyword table shit while ($keywordtablerow = mysql_fetch_array($keywordtablequery)) { $keywordtablequery = mysql_query("SELECT * FROM keywords WHERE keyword='$keyword'"); $keywordtablerow = mysql_fetch_array($keywordtablequery); $keywordfileid = $keywordtablerow['keywordfileid']; ... ... } Quote Link to comment https://forums.phpfreaks.com/topic/41079-can-i-put-several-whiles-inside-of-whiles/#findComment-199003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.