Hello!

I'd like to ask a question, and I guess the simplest way to explain what I want is to simply show you the page.

See the sort by list? I simply want to sort the entries by year, which I did succeed with, in a way. The problem is that I'm being forced to make a new identical PHP page for each year, where I change the query to sort by the chosen year. The pages are linked from the dropdown list. Which is an awful way of doing it, since if I ever want to edit the page, I have to edit all the year copy pages too. :/

Is there a way to sort entries that doesn't require me to make a new php page, and just keeps it all on page one, so to speak? And if this is possible, how do you make the pagination adapt to it? Since the way I have it now, it'd break if I didn't change the URL's in the code.

Thanks a lot in advance for any replies!

Note: I'm a beginner at this, so I hope the question isn't too silly. I've searched the net a lot, but I couldn't find anything directly related to my question. :/

Recommended Answers

All 7 Replies

Member Avatar for diafol

The dropdown should send a value to the page, e.g. 2009.
The page searches for this - by looking for $_POST (or whatever you've called the dropdown select field).
You now 'clean' this value and pass it to your sql statement:

$year = mysql_real_escape_string($_POST['year']);
$sql = "SELECT * FROM table WHERE year = '$year'";

Straightforward.

The dropdown should send a value to the page, e.g. 2009.
The page searches for this - by looking for $_POST (or whatever you've called the dropdown select field).
You now 'clean' this value and pass it to your sql statement:

$year = mysql_real_escape_string($_POST['year']);
$sql = "SELECT * FROM table WHERE year = '$year'";

Straightforward.

I have to say I apparently have no idea how to actually implement that on my page. I can't get my list to work at all, heh. I'm not sure how that works with multiple years either. :/

Member Avatar for diafol

Do you have any code to share with us? I'm loathe to provide a complete solution if you haven't got anything yourself. I'm willing to help, but not do ALL the work.

Uhm, like what code? The code for the entries to show up? Well, since I'm not sure how to implement the slice of code you gave me, I've just been experimenting a bit (like of which part of the page it goes, if I was supposed to replace my current query, make a second, mess with my list etc)... I guess I don't really understand how the list is supposed to pick up the information and make anything happen?

Member Avatar for diafol

Can you show your html then?

For my dropdown, I guess?

Sort by year:
<form name="jump" action="http://lace.forthesky.org/wardrobe/angelicpretty/op" method='post'>
<fieldset>
<select name="x">
<option value="2010">2010</option>
<option value="2004">2004</option>
</select>
<input type="button" onclick="location=document.jump.x.options[document.jump.x.selectedIndex].value;" value="GO" />
</fieldset>
</form>

Or nevermind. I got the list to work, but then all default entries disappeared. Oh well. Thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.