Complex While Loop

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

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Complex While Loop

Post by oscardog »

So i have a games website in the making, its getting there... now i want a page that displays 'Full Games List' basically of all the games in my database, now the only way is a while loop, and i tried to first of all use a table - but i wanted it in alphabetical order and i couldnt work out how to go down the columns alphabetically.

So i went for 4 DIVs using a UL. Here is my code, i had to use task manager to close IE as it was stuck in a loop XD

Code: Select all

<?php $result = mysql_query("SELECT * FROM games");
                $gamecount = mysql_num_rows($result);
                
                $result2 = mysql_query("SELECT * FROM games ORDER BY title ASC");
                $row = mysql_fetch_array($result2);
                $gamesperrow = $gamecount / 5; 
                $count1 = 0;
                $count2 = 0;
                $count3 = 0;
                $count4 = 0;
                ?>
                
                <div style="float:left; width: 146px; font-size: 12px;">
                  <ul>
                    <?php while($count1 <= $gamesperrow) {
                    echo "<li><a href=\"playgame.php?game=" . $row['title'] . "\">" . $row['title'] . "</a></li>";
                    $count1++;
                    } ?>
                 </ul>
                </div>
                <div style="float:left; width: 146px; font-size: 12px;">
                <ul>
                <?php while($count2 <= $gamesperrow) {
                    echo "<li><a href=\"playgame.php?game=" . $row['title'] . "\">" . $row['title'] . "</a></li>";
                    $count2++;
                    } ?>
                </ul>
                </div>
                <div style="float:right; width: 146px; font-size: 12px;">
                <ul>
                <?php while($count3 <= $gamesperrow) {
                    echo "<li><a href=\"playgame.php?game=" . $row['title'] . "\">" . $row['title'] . "</a></li>";
                    $count3++;
                    } ?>
                </ul>
                </div>
                <div style="float:right; width: 146px; font-size: 12px;">
                <ul>
                <?php while($count4<= $gamesperrow) {
                    echo "<li><a href=\"playgame.php?game=" . $row['title'] . "\">" . $row['title'] . "</a></li>";
                    $count4++;
                    } ?>
                </ul>
                </div>
It's quite 'ugly' imo but i had no idea of how else to do it, and i have a feeling that it will just echo the first game in my DB over and over... Any solutions are welcome, mainly i would like the while loop sorted and then if anyone has any suggestions on how to lay it out, would be greatly appreciated :)

EDIT: Just realised i was incrementing the count1, corrected, now it displays the first game in my database over and over, any help? Also, the way im doing it to decide games per 'row' there are 14 games in my DB atm, and its displaying 3 per 'row' so i have 12 games displaying(Well i would, if the links wern't all the same... So should i like round the number? Just looking at php.net at the rounding thing, how would i round the number upwards, because i dont want it to ever round down...?

Oscardog
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Complex While Loop

Post by papa »

There are some nice things you can do with array_multisort().

Example from a file explorer I did:

Code: Select all

 
//File array
$files[] = array("Name"=>$file, "Size"=>filesize($cur_path.$file), "Type"=>filetype($cur_path.$file), "Date_modified"=>date("Y-m-d H:i", fileatime($cur_path.$file)));
 
// Obtain a list of columns
foreach ($files as $key => $row) {
    $Name[$key]  = strtolower($row['Name']);
    $Size[$key] = $row['Size'];
    $Type[$key]  = $row['Type'];
    $Date_modified[$key] = $row['Date_modified'];
}
 
array_multisort($Type, SORT_ASC, $Name, SORT_ASC, $files);
Hope it helps
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Complex While Loop

Post by oscardog »

Thanks :) Looks good, but could you explain how it works in English? Haha Like how i would then output the information?

And will that fit into my current layout, with the <UL>'s?

Oscardog
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Complex While Loop

Post by papa »

In this case, multisort might be redundant.

Using a table would make it only one loop.

Code: Select all

 
$result2 = mysql_query("SELECT * FROM games ORDER BY title ASC");
$i=1;
?>
<table border=1>
<tr>
<?php
while($row = mysql_fetch_array($result2)) {
 
 echo "<td><a href=\"playgame.php?game=" . $row['title'] . "\">" . $row['title'] . "</a></td>\n";
 if($i%5 == 0) echo "</tr><tr>\n";
 $i++;
}
echo "</tr><tr>\n";
?>
</table>
 
 
Haven't tried the code though...

And the tr's won't be all that correct.. I can have a look at it again, but must go back to work right now. :)

edit: slighty updated.
Last edited by papa on Mon Jan 19, 2009 9:48 am, edited 1 time in total.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Complex While Loop

Post by oscardog »

Yeh doesn't quite work, as there is no closing TR tag at some point, and im not entirely sure it will work..

For example:

Code: Select all

if($i%5 == 0) echo "</tr><tr>\n";
$i starts off as 0...surely when its done 5 runthroughs it will make a new row. I want it so that the number of games i have(so the rowcount) when divided by 4 it will give a number, so say i have 100games, and i divide that by 4, it means the answer is 25. Therefore it would put 25 bullet points then go to the next set of bullet points... As an example, i want it to work basically like this:

http://www.miniclip.com/games/en/ (Scroll to bottom of page, you'll get a long long list of games)

But i have no idea how on earth they did that, so it goes in alphabetical order like they did, any ideas? :S

Oscardog
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Complex While Loop

Post by papa »

Aha ok, my bad... :roll:

Someone will probably post before me, but I can show you tomorrow otherwise.

Read up on group by in mysql meanwhile:
http://www.w3schools.com/sql/sql_groupby.asp
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Complex While Loop

Post by oscardog »

Thankyou very much, i'm happy to wait, but if anyone else has ideas/suggestions im all ears.

Look forward to your help tomorrow! :)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Complex While Loop

Post by papa »

Code: Select all

 
<?php
$alphabet = range('A','Z');
$i=1;
?>
<table border=1>
<tr>
<?php
foreach($alphabet as $letter) {
    //Fetch games by letter
    $result2 = mysql_query("SELECT * FROM games WHERE title LIKE '$letter%' ORDER BY title ASC");
    echo "<td>\n";
    echo $letter."<br />\n";
    echo "<ul>\n";
        while($row = mysql_fetch_array($result2)) {
        echo "<li><a href=\"playgame.php?game=" .$row['title'] . "\">" . $row['title'] . "</a></li>\n";
    }
    echo "</ul>\n";
    
    //Break every 5th row
    if($i%5 == 0) echo "</tr><tr>\n";
    $i++;
}
echo "</tr><tr>\n";
?>
</table>
 
 
Try this for fun.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Complex While Loop

Post by oscardog »

You sir deserve a medal :D

Thanks alot :)

Oscardog
Post Reply