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!
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?
and replace all calls of mysql_query by dbg_mysql_query. Do the querries look like you have expected?
and after
Citizen wrote: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.
You know, feyd told you, twice, that you are not using $begin or $end, and you are not using $i. What that means is that your are looping the exact same dataset regardless of what $i, $begin or $end are. Look at this little piece of example and tell me if you can spot the error in it.
<?php
$begin = 0;
$end = 35;
for ($i = $begin; $i < $end; $i++)
{
echo '<p>I can fit inside a loop!</p>';
}
?>
You see how the vars $i, $begin and $end have absolutely nothing to do with what is displayed here? It will always be the same. Now, what if you did this...
Everah wrote:You know, feyd told you, twice, that you are not using $begin or $end, and you are not using $i. What that means is that your are looping the exact same dataset regardless of what $i, $begin or $end are. Look at this little piece of example and tell me if you can spot the error in it.
<?php
$begin = 0;
$end = 35;
for ($i = $begin; $i < $end; $i++)
{
echo '<p>I can fit inside a loop!</p>';
}
?>
You see how the vars $i, $begin and $end have absolutely nothing to do with what is displayed here? It will always be the same. Now, what if you did this...
<?php
$begin = 0;
$end = 35;
for ($i = $begin; $i < $end; $i++)
{
echo '<p>The number ' . $i . ' can fit inside a loop!</p>';
}
?>
You see how the display changes now depending on $i. See if you can use this to your advantage when fixing your code problem.
I dont see how I can use $i since all I am doing is showing the results. I've read everythign i've found about loops, maybe I'm just missing something.
Last edited by Citizen on Tue Sep 19, 2006 11:17 am, edited 1 time in total.
The loop controls the number of things shown. What controls what is shown is the use of $i inside the array that is being looped. If all you are doing is looping through a certain number of times, that by itself does nothing for your. But if, inside that loop, you tell the output to use the loop index (the $i) then you can control what is being displayed. Post your array and the loop construct again.
Ok, I changed a little bit of your code around for you. You did no error checking, so I added some die's in there (though I'd recommend a customer error handler to do that) and changed some things related to your loop. See how it runs. Then toy around with it. Then post back with questions.
$result1 = mysql_query($sql1) or die ('Could not process sql1 query: ' . mysql_error());
$gamenum = mysql_num_rows($result1);
// This is for the looping construct
$game_array = array();
if ($gamenum > 0)
{
while ($row = mysql_fetch_array($result1))
{
$game_array[] = $row;
}
}
$game_count = count($game_array);
What exactly does this do? Why do we use a 'while' to setup a variable's value?
Citizen wrote:Wow! It worked... ok so I guess its question time
I would venture to say question time was a little while ago .
Citizen wrote:What exactly does this do? Why do we use a 'while' to setup a variable's value?
You loop through the mysql_fetch_array() call and while you are looping, you assign that array to a multi-dimensional, numerically indexed array (those are a beauty for looping with for)
Same thing here, I've never seen code used this way before. How can we use inequalities when setting a variable's value?
This is the ternary operator. It is a shortened form of an if-else statement. Basically this checks to see of the total number of games is lower than $begin + 20 (because you don't want to loop farther than your array count) and sets the end value to the appropriate amount.
What is [$i] doing in this code? How does the script know to pull out only the results that we want and not the same first 20 like it did last time?
Because you made a numerical index in your array with the while loop above. So you are saying in your for loop, when you are on index number 4, show the 4th row of the games array, when we get to 13 show the 13th row, etc. That is how you can tell your script to start at 30 and end at 50 and it will show rows 30 to 50.