SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Alphabetical Order

    Adding a row of categories for the site. When adding it's not in alphabetical order. In the code below, what do I need to change to have the channel_name in order?

    Code:
    //get all categories
    $all_categories = array();
    $sql = "SELECT * FROM channels";
    $query = @mysql_query($sql);
    while ($result = @mysql_fetch_array($query)) {
    
        //get latest video information
    
        $channel_id = $result['channel_id'];
        $channel_name = $result['channel_name'];
        $channel_name = mysql_real_escape_string($channel_name);
        $sql0 = "SELECT * FROM videos WHERE channel = '$channel_name' AND approved = 'yes' ORDER BY indexer DESC";
        $query0 = @mysql_query($sql0);
        $result0 = @mysql_fetch_array($query0);
        if (empty($result0)) {
            $video_picture = 'default_no_group_video';//show place holder image2wbmp
            $indexer = "";
            $result0 = array('video_id' => $video_picture, 'indexer' => $indexer);
        }
        $new_array = @array_merge($result, $result0);
    
        $all_categories[] = $new_array;
    }
    Thank you!

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing the 3rd line, From :

    PHP Code:
    $sql "SELECT * FROM channels"
    To

    PHP Code:
    $sql "SELECT * FROM channels order by channel_name"

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skaykay View Post
    Try changing the 3rd line, From :

    PHP Code:
    $sql "SELECT * FROM channels"
    To

    PHP Code:
    $sql "SELECT * FROM channels order by channel_name"
    OMG! You are the MAN/WOMAN! I asked everywhere, and I can't believe I finally got the solution. Thank you so much skaykay, your the best!

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    just curious, where were you asking that they had never heard of the ORDER BY clause?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a larger problem than not knowing how to use an order by clause. You are causing excessive load on your server because you are getting some of your values you need and then running another sql query inside the loop while getting those results.

    What you should be doing is a join and then output your rows as necessary.


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
  •