SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why won't this sorting work?

    Hi. I have problems sorting. Can anyone help me? My output is correct just that the sorting dosen't work, which i want it to be in alphabetical order by the name of Game (column - Game). I'm using this with PHPLib Template and PHPLib dB class

    Code:


    $tables = array("PlaystationAction", "PlaystationAdventure", "PlaystationFighting", "PlaystationPlatform", "PlaystationPuzzle", "PlaystationRacing", "PlaystationRpg", "PlaystationSimulation", "PlaystationSports", "PlaystationStrategy");

    foreach ($tables AS $table) {

    $sql = "SELECT Alpha, Game, Url, Date, Publisher, Developer FROM $table
    WHERE Alpha='a' ORDER BY Game ASC";

    $db->query($sql);

    while ($db->next_record()) {

    $Alpha = $db->f(Alpha);
    $Game = $db->f(Game);
    $Url = $db->f(Url);
    $Date = $db->f(Date);
    $Publisher = $db->f(Publisher);
    $Developer = $db->f(Developer);

    $color = ($i++ % 2 ) ? '#000000' : '#333333';

    $tmp3 = "<tr bgcolor='$color'><td><a href='/$Alpha/$Url/index.phtml'>$Game</a></td><td>$Publisher</td><td>$Developer</td><td>$Date</td></tr>";
    $t->set_var('data', $tmp3);
    $t->parse('DBlock', 'DataBlock', true);
    }

    }
    Last edited by XploreR; May 27, 2001 at 11:03.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It looks to me like it is sorting each table alphabetically, but since you are outputing more than one table, it starts sorting again for the next table. Is that what the problem is?

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with this is your database design. What you should have done is made one table for all of the games, with an additional field called typeId, which links to a table called gameTypes with fields id and name.

  4. #4
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi 7stud ,

    yes, that's exactly my problem. Any Help? Someone told me to use left join tho.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    There is no easy way to do that. You are going to have to read everything into an array and then sort the array.

  6. #6
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know how? Please help

  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while ($db->next_record()) {

    $array[] = ('$Alpha => $db->f(Alpha)',
    '$Game => $db->f(Game)',
    '$Url => $db->f(Url)',
    '$Date => $db->f(Date)',
    '$Publisher => $db->f(Publisher)',
    '$Developer => $db->f(Developer)');

    }



    Thats how you get it into an array, although I'm not sure how to sort it depending on a field of choice. You can use asort() or array_mutlisort but I don't know which.

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this helps, I found this code on php.net for you:

    function cmp ($a, $b) {
    return strcmp($a["fruit"],$b["fruit"]);
    }

    $fruits[0]["fruit"] = "lemons";
    $fruits[1]["fruit"] = "apples";
    $fruits[2]["fruit"] = "grapes";

    usort($fruits, "cmp");

    while (list ($key, $value) = each ($fruits)) {
    echo "\$fruits[$key]: " . $value["fruit"] . "\n";
    }


    That would print out
    0: Apples
    1: Lemonds
    2: Grapes

  9. #9
    SitePoint Enthusiast XploreR's Avatar
    Join Date
    May 2001
    Location
    Singapore
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, petesmc

    I'll have a go and let u know


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •