SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help sorting php/mysql array

    Hello:

    I need help sorting an array. I have my data in several mysql databases. I want to list the most popular item from each database on my home page.
    I can query each database and assign the items retrieved in a php array as follows:

    $result = mysql_query("SELECT * FROM `Productlist1` ORDER BY `Productlist1`.`Hits` DESC LIMIT 1") or die(mysql_error());

    while($row = mysql_fetch_array($result)){
    $name=$row['Name'];
    $url=$row['Link'];
    $hits=$row['Hits'];

    $result2 = mysql_query("SELECT * FROM `Productlist2` ORDER BY `Productlist1`.`Hits` DESC LIMIT 1") or die(mysql_error());

    while($row2 = mysql_fetch_array($result2)){
    $name2=$row2['Name'];
    $url2=$row2['Link'];
    $hits2=$row2['Hits'];

    etc.

    I want to sort and list the results by the number of 'Hits'
    Richard DeJong

    Admin
    http://www.soldquick.ca

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can use a UNION to query all the tables at once and sort the entire result set.

    http://dev.mysql.com/doc/refman/5.0/en/union.html


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
  •