Page 1 of 2

Changes to loop do nothing

Posted: Sun Sep 17, 2006 1:31 pm
by Citizen
I had a loop like..


i=0, i<5, i++

and it works fine

but when i change it to

i=2, i<7, i++

the results on the page stay the same

any ideas?

Posted: Sun Sep 17, 2006 1:35 pm
by feyd
post. your. code.

Posted: Sun Sep 17, 2006 6:39 pm
by Citizen
I get the same results no matter what I set $begin and $end to.

Code: Select all

if($cat == 'random'){
	$sql1="SELECT * FROM `arcade_games` ORDER BY `title`";
}
else{
$sql="SELECT * FROM `arcade_categories` WHERE `catname` = '$cat' LIMIT 1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$maincat = $row["catid"];

$sql1="SELECT * FROM `arcade_games` WHERE `categoryid` = '$maincat' ORDER BY `shortname`";
}
$result1=mysql_query($sql1);
$gamenum = mysql_num_rows($result1);
if ($gamenum < 20){
	$begin = 0;
	$end = $gamenum;
}
else if ( isset($_GET['begin']) ) {
	$begin = $_GET['begin'];
	$end = $begin + 20;
}
else {
	$begin = 0;
	$end = 20;
}
for($i = $begin; $i < $end; $i++) {
	$row = mysql_fetch_array($result1);
	$shortname = $row["shortname"];
	$title = $row["title"];
	$gameid = $row["gameid"];
	$stdimage = $row["stdimage"];
	$description = $row["description"];
	$thiscat = $row["categoryid"];
	$isreverse = $row["isreverse"];

	$sql2="SELECT * FROM `arcade_categories` WHERE `catid` = '$thiscat' LIMIT 1";
	$result2=mysql_query($sql2);
	$row = mysql_fetch_array($result2);
	$tempcat = $row["catname"];

	if($isreverse == 1){
		$sql3="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid' ORDER BY `score` LIMIT 1";
	}
	else{
		$sql3="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid' ORDER BY `score` DESC LIMIT 1";
	}
	$result3=mysql_query($sql3);
	$champnum = mysql_num_rows($result3);
	if($champnum == 1){
		$row = mysql_fetch_array($result3);
		$champname = $row["username"];
		$champscore = $row["score"];
	}
	else {
		$champname = "None";
		$champscore = 0;
	}
	
	$user = $VAR[4];
	$sql4="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid' AND `username` = '$user' ORDER BY `score` DESC LIMIT 1";
	$result4=mysql_query($sql4);
	$row4 = mysql_fetch_array($result4);
	$scorenum = mysql_num_rows($result4);
	if($scorenum == 1){
		$yourscore = $row4["score"];
	}
	else {
		$yourscore = "None";
	}	
	echo "
			<table style='width:100%;'>
				<tr>
				<td style='width:15%;text-align:center;font-size:11pt;' class='color'>
				<a href='$VAR[0]/arcade.php?play=$gameid'><img src='/arcade/images/$stdimage' border='2'></a>
				</td>
				<td style='width:30%;vertical-align:top;font-size:11pt;' class='color'>
				<b><a href='$VAR[0]/arcade.php?play=$gameid'>$title</a></b>
				<div style='font-size:10pt;'>
				$description
				</div>
				</td>
				<td style='width:15%;text-align:center;font-size:11pt;' class='color'>
				<b>$tempcat</b><br />
				(<a href='$VAR[0]/arcade.php?cat=$tempcat'>More</a>)</td>
				<td style='width:20%;text-align:center;font-size:11pt;' class='color'>
				<b><a href='$VAR[0]/$champname'>$champname</a></b>
				<div style='font-size:10pt;'>
				With a high score of:<br />
				<b>$champscore</b>				</div>
				</td>
				<td style='width:20%;text-align:center;font-size:11pt;' class='color'>
				Your high score:
				<br />
				<b>$yourscore</b>
				<br />
				[<a href='$VAR[0]/arcade.php?highscore=$gameid'>High Scores</a>]
				</td>
				</tr>
			</table>
		";
	}
}

Posted: Sun Sep 17, 2006 6:53 pm
by feyd
Your code doesn't use $i, therefore there is nothing that could change as a result.

Posted: Sun Sep 17, 2006 7:00 pm
by Citizen
I'm not sure what you mean.

No matter what I put in for $begin, my page only shows the first 20 results.

What do I need to change in order to only display results 20-40 instead of always the 1-20?

Posted: Sun Sep 17, 2006 7:14 pm
by feyd
Your code doesn't really use $begin or $end either.

Posted: Mon Sep 18, 2006 12:14 am
by Citizen

Code: Select all

if ($gamenum < 20){
        $begin = 0;
        $end = $gamenum;
}
else if ( isset($_GET['begin']) ) {
        $begin = $_GET['begin'];
        $end = $begin + 20;
}
else {
        $begin = 0;
        $end = 20;
}
for($i = $begin; $i < $end; $i++) {
the variable $begin is set by url

if they click page 2, begin is set to 20 and should show results 20-40

instead, even when begin is set, it displays results 1-20

Here is an example:

go to the bottom of the page and click 2

begin should be set to 20 but the results dont change

Posted: Mon Sep 18, 2006 12:19 am
by arkady
Citizen wrote:

Code: Select all

if ($gamenum < 20){
        $begin = 0;
        $end = $gamenum;
}
else if ( isset($_GET['begin']) ) {
        $begin = $_GET['begin'];
        $end = $begin + 20;
}
else {
        $begin = 0;
        $end = 20;
}
for($i = $begin; $i < $end; $i++) {
the variable $begin is set by url

if they click page 2, begin is set to 20 and should show results 20-40

instead, even when begin is set, it displays results 1-20

what am i doing wrong?
This may sound silly, but are you incrementing the $_GET['begin'] field when writing the generated page?

I couldn't see any form code is all...

Posted: Mon Sep 18, 2006 12:34 am
by Citizen
right

you can see it here:

http://gamerbio.com/arcade.php

just scroll to the bottom and click the "Page 1, 2, 3 " and note that no changes are made to the selection of which games are shown

Posted: Mon Sep 18, 2006 1:01 am
by arkady
Citizen wrote:right

you can see it here:

http://gamerbio.com/arcade.php

just scroll to the bottom and click the "Page 1, 2, 3 " and note that no changes are made to the selection of which games are shown
one tiny thing, at the end of your main table you need to close the </table> tag (missing >)

Also.. why not try spitting some debug info out from the query result to confirm that the number of rows it's detecting is in fact higher than 20? it seems to be triggering the condition of less than 20 each time.

Nice website design btw

Posted: Mon Sep 18, 2006 12:23 pm
by Citizen
ok, the script now shows the results before allowing page slecection.

http://www.gamerbio.com/arcade.php

and

http://www.gamerbio.com/arcade.php?begin=21

display the same results

any ideas?

Posted: Mon Sep 18, 2006 12:52 pm
by volka
Citizen wrote:any ideas?
Did you try
arkady wrote:Also.. why not try spitting some debug info out from the query result to confirm that the number of rows it's detecting is in fact higher than 20?
?

You can add the function

Code: Select all

function dbg_mysql_query($query) {
	echo '<fieldset><legend>sql query</legend>', htmlentities($query), "<br />\n";
	$r = mysql_query($query);
	if (!$r) {
		echo mysql_error();
	}
	else {
		echo 'num_rows: ', mysql_num_rows($r);
	}
	echo "</fieldset>\n";
	return $r;
}
and replace all calls of mysql_query by dbg_mysql_query. Do the querries look like you have expected?

Posted: Mon Sep 18, 2006 1:37 pm
by Citizen
The query is being executed fine... it comes up with 40+ queries and i only list 20 at a time. The thing is, no matter what i set $begin and $end to, it always displays the same 20 instead of showing 21-40 or anything.

Posted: Mon Sep 18, 2006 2:39 pm
by volka
Can you show us the two different querries (for 1-20 and 21-40) from the output of dbg_mysql_query?

Posted: Mon Sep 18, 2006 9:50 pm
by Citizen
volka wrote:Can you show us the two different querries (for 1-20 and 21-40) from the output of dbg_mysql_query?
I'm not sure what the dbg thing is. Could you explain what it is?