php/html ordered lists?

Discussion in 'Web Design and Development' started by twoodcc, Mar 27, 2009.

  1. twoodcc macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    ok, so i made a php flash video type site. anyways, the index has a link to each video and it pulls from the database how many "views" each video has also. example:

    video 1(link) Views: 20
    video 2(link) Views: 10
    etc

    my question: is there a way to sort this list by views?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Depends what your database tables look like, but would be something like,
    Code:
    SELECT * FROM videos ORDER BY views DESC;
    if you want videos with the most views first.
     
  3. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    well, i understand how to do it in sql, but that won't change the order of the html links/titles of the movies.

    in other words, i make a query to the database for the views, i have a <p> for the link to the video and title, display the views, and go to the next <p> for the next one, and start over again.

    i know this isn't the best way to go about this, but this is just for myself anyways.
     
  4. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #4
    So what you want to do is query your list, store the info in a PHP array, and then sort the array by title. Then iterate through the array and output the appropriate HTML.

    Edit: err, sorry, sort by views, like the OP said.
     
  5. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #5
    that might work. but this site isn't anything fancy. just something i threw together for the heck of it.

    i was looking at ordered lists in html, but it doesn't actually reorder them.
     
  6. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #6
    Yeah, HTML is just a mark-up language, meaning it only specifies to the browser how to format a block of text. An "ordered list" just means a list that is assumed to have some sort of order (like a list of instructions, hence the typical rendering with sequential numbers 1, 2, 3, ...) HTML markup can't actually change the physical order of a list of items.

    Anything where the block of text is dynamically generated or altered (and that includes changing the sort order of a list) must be done using some other program which does the change and then generates the appropriate HTML code, whether it's data pulled from an SQL query, or a PHP script, or a Javascript, or whatever.

    That's another option too, you could probably find some Javascript code that could read the contents of a table and dynamically sort them.
     
  7. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    yeah, that's what i thought.

    i really don't know javascript at all though. here is my code (just remember i just threw this together):
    Code:
    some html headers and such
    
    db connection and all...
    
    $num1 = mysql_fetch_row(mysql_query("select views from views where id = 1"));
    	$num2 = mysql_fetch_row(mysql_query("select views from views where id = 2"));
    	$num3 = mysql_fetch_row(mysql_query("select views from views where id = 3"));
    	$num4 = mysql_fetch_row(mysql_query("select views from views where id = 4"));
    	$num5 = mysql_fetch_row(mysql_query("select views from views where id = 5"));
    			
    	?>
    <p> <a href="./falconssaints.php">Falcons @ Saints</a> <?php echo "Total Views: ".number_format($num1[0]);?></p>
    		
    <p><a href="./heaven.php">Heaven</a> <?php echo "Total Views: ".number_format($num2[0]);?></p>
    		
    <p><a href="./NoCountryForOldMen.php">No Country For Old Men</a> <?php echo "Total Views: ".number_format($num3[0]);?></p>
    
    <p><a href="./valentinesday2008.php">Valentine's Day 2008</a> <?php echo "Total Views: ".number_format($num4[0]);?></p>
    
    <p><a href="./underworld.php">Underworld</a> <?php echo "Total Views: ".number_format($num5[0]);?></p>
    		
    	</div>
     
  8. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #8
    So what I would do there is put each filename in an array associated with the file's ID number, link description, and view count (initialized to 0). (Look up PHP's multidimensional arrays, or use strings and delimiters).

    Then put your SQL queries in a loop: Walk through every element in the array, build the query from the ID number, store the view count.

    Then sort the array by view count.

    Then walk through the array elements again, this time generating the appropriate HTML code including the filename link, description, and view count.

    This code will have the added advantage of reducing a lot of repetition (where you generate the links and do the SQL queries) and it will be that much easier to add more links in the future. Just add to the array and let the code do the rest. You could even take it a step further and store the array in another file and read them in.
     
  9. Jas123 macrumors member

    Joined:
    Apr 1, 2008
    #9
    If I'm not mistaken, couldn't you create another table with that information, join it, then add it to your variables when you add the views to it.
     
  10. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #10
    thanks for the reply. now all that sounds like a lot of work, especially since i don't hardly know where to start. could you give me an example, if you have time? or something to help me get started in the right direction?

    i know some sql. i think i could do that, but since this isn't a lot of info, would it really be necessary?
     
  11. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #11
    There's probably quite a few ways to get this done. By no means am I suggesting my approach is the "best" one.

    I can try to come up with some simple examples later. It's not as much work as it sounds. And the payoff will be big if you expect to keep updating this page of links. You'll spend a bit of extra time getting the PHP code going, but right now if you want to add more links you need to change your code in two different places.
     
  12. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #12
    thanks :)

    i agree that what i have isn't the best coding for adding more links and keeping them in order. that's why i'm here. always open to suggestions.

    i really appreciate the help! and i get to learn in the process as well (which is really the whole point of the thing)
     

Share This Page