Basically my while loop is making my page super slow even though I only want 4 rows max, and it is displaying the first row twice. Any help <? $fnm = 1; if($fbannershow == 1) { $sql=$Db1->query("SELECT * FROM fbanners WHERE credits>=1 and (daily_limit>views_today or daily_limit=0) ".iif($LOGGED_IN==true," and username!='$username'")." order by rand() limit 4"); $pagebanner180=$Db1->fetch_array($sql); $sql=$Db1->query("UPDATE fbanners SET credits=credits-1, views=views+1, views_today=views_today+1 WHERE id='$pagebanner180[id]'"); if($pagebanner180[id] != "") { $fbannerdisp.="<a href=\"fbannerclick.php?id=$pagebanner180[id]\" target=\"_blank\"><img src=\"$pagebanner180[banner]\" border=0\" width=\"180\" height=\"100\"></a> "; } } while ($fnm <= 2) { echo $fbannerdisp; $fnm = $fnm++; } ?> Thanks all
Should use $fnm++; instead of $fnm = $fnm++; $fnm++ is the same thing as saying $fnm = $fnm + 1; so there's no need to do the long format in combination with it. Can't really tell you bout the speed since it seems like there's an error in your code anyways, for example your SQL string isn't properly escaped for the strings and such. Also not even sure why you're using the loop when $fbannerdisp at that point of time is always the same value during each loop. look into ob_flush to have data sent to the browser in real time, but to do so smoothly, you have to output it as you get it, not after the data retrieval.
Im newish to php, Im trying to get a different banner to display from the database, and I never want more or less than 4 to display, with the code I have I can either get 3 or 6 to display but not for and the first two are always the same. I want 4 no matter what and I dont want any of them to be the same, I just cant figure it out, been working on it for hours.
What database are you using, your queries seem a bit complex? Anyhow if you query is returning 4 rows, then you just loop through them to display. I see your using an OOP style and i'm not too sure how to do this but to loop through you'd just do: in place of: $pagebanner180=$Db1->fetch_array($sql); $sql=$Db1->query("UPDATE fbanners SET credits=credits-1, views=views+1, views_today=views_today+1 WHERE id='$pagebanner180[id]'"); if($pagebanner180[id] != "") { $fbannerdisp.="<a href=\"fbannerclick.php?id=$pagebanner180[id]\" target=\"_blank\"><img src=\"$pagebanner180[banner]\" border=0\" width=\"180\" height=\"100\"></a> "; } PHP: You'd do: //Loop through the 4 banners while($row = mysql_fetch_array($sql)){ //Show Banner echo "<a href=\"fbannerclick.php?id=$row[id]\" target=\"_blank\"><img src=\"$row[banner]\" border=0\" width=\"180\" height=\"100\"></a> "; //Update views counter $sql=$Db1->query("UPDATE fbanners SET credits=credits-1, views=views+1, views_today=views_today+1 WHERE id='$row[id]'"); } PHP:
Well course they'd be the same all the time, by the time you get down to the while loop, the $fbannerdisp variable had already been filled with a value (being the last one). Unless you make it an array, the variable will only contain the last value inserted into it. Try this code if you're trying to do multiple banners. /* Not sure what fbannershow varible is for, but as long as you got it set before getting to this point you're good to go, 1 is interpreted the same as 'true', likewise 0 could be interpreted as such too. So in this case you don't need to check against == 1 */ if($fbannershow) { $result = mysql_query("SELECT * FROM fbanners WHERE credits >= 1 AND (daily_limi > views_today OR daily_limi = 0) ".iif($LOGGED_IN, " AND username != '".$username."'")." ORDER BY rand() limit 4"); if($result) { $num_results = mysql_num_rows($result); if($num_results > 0) { $banners = array(); // sets $banners up as an array //Your previous code would only set views for a specific ID, so 3 guys got freebies :P, this fixes that by looping thru the results. while ($row = mysql_fetch_assoc($result)) { //Only set views and such if results came back. mysql_query("UPDATE fbanners SET credits=credits-1, views=views+1, views_today=views_today+1 WHERE id='".$row['id']."'"); $banners[] = "<a href=\"fbannerclick.php?id=".$row['id']."\" target=\"_blank\"><img src=\"".$row['banner']."\" border=0\" width=\"180\" height=\"100\"></a> "; } } } } foreach($banners as $banner) { echo $banner; } PHP: not sure whats up with the database retrieval method you were using, but I generally use MySQL. Also when you are accessing associative arrays with keys you want to make sure to put quotes around the key, for example $row[id] is not right, where as $row['id'] is the correct way to do it.
if you're referring to the iif() , thats kind of a single statement (tertiary) form of if-then-else, meaning iif(condition, value-if-matched, value-if-not)
Yes the inclusion of iif and non use of caps for keywords couple with poor spacing made it seem complex to me