Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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?
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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.
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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.
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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>
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

Jas123

macrumors member
Apr 1, 2008
97
0
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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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?

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.

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

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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.

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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.