Changes to loop do nothing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Changes to loop do nothing

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post. your. code.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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>
		";
	}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your code doesn't use $i, therefore there is nothing that could change as a result.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your code doesn't really use $begin or $end either.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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
Last edited by Citizen on Mon Sep 25, 2006 7:06 pm, edited 1 time in total.
arkady
Forum Newbie
Posts: 23
Joined: Sun Sep 17, 2006 9:34 pm

Post 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...
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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
arkady
Forum Newbie
Posts: 23
Joined: Sun Sep 17, 2006 9:34 pm

Post 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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Can you show us the two different querries (for 1-20 and 21-40) from the output of dbg_mysql_query?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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?
Post Reply