Help - Search - Members - Calendar
Full Version: Numbered Mysql results!
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
influct
Put in your average query:

Normal query here:
CODE
$sql = mysql_query("SELECT * FROM table");
$sql2 = mysql_query($sql);


Heres our starting number (0)
CODE
$newno = 0;


Heres our while statement (0)
CODE
While ($row = MySQL_fetch_array($sql)){

Heres what happenens!
The $newno variable for the first but is set to 0, and in the statement below we add one to it, so the total is 1.
When it repeats however it only repeats whats in the while statement, so our new math is 1+1, then it repeats to 2+1 and so on and so on - basically increments of 1

CODE
$newno = $newno + 1;
   echo"
$newno [b][color=#FF0000]And we echo the the number and whatever mysql result.[/color][b]
";
}



I used this on this page http://www.sfegaming.com/arcade/beta.php
(sorry if that counts as a plug:p)
If you look, top online games are numbered and I used it for layout purposes as well!
Look at the newest games box, see those smaller icons? Yup:)
Thats set up like this
CODE
if ($newno == 3){
echo"<br />";
}

so if its the third icon it prints a <br />

Handy eh?victory.gif
Hope this helped some people!
. Adam .
Bit basic...

But It's Good for beginners victory.gif

- Adam bigwink.gif
influct
Yeh I know:P
But its usefull.
Indigo
Not that bad, guess it might be useful for some.
Just one thing:

CODE
$newno = $newno + 1;


can be changed into

CODE
$newno = $newno++;


Or could it be done in another way, just write

CODE
$newno++;


Oh well, I don't remember, too long since I've worked with php.
Hayden
i always just have an 'id' field in all my tables that auto increments as information is entered blink.gif
Matthew.
QUOTE(SpatialVisionary @ Oct 13 2006, 06:21 AM) *
i always just have an 'id' field in all my tables that auto increments as information is entered blink.gif


Consider how p2l's latest tutorials are displayed "1, 2, 3, 4...20". Those numbers are not the IDs of the tutorials, they are generated within the loop like this smile.gif
Hayden
QUOTE(Matthew. @ Oct 13 2006, 02:33 PM) *
QUOTE(SpatialVisionary @ Oct 13 2006, 06:21 AM) *

i always just have an 'id' field in all my tables that auto increments as information is entered blink.gif


Consider how p2l's latest tutorials are displayed "1, 2, 3, 4...20". Those numbers are not the IDs of the tutorials, they are generated within the loop like this smile.gif


lol. oh yeah. okay. do it so that the numbers are sequential and don't skip around. blush.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.