Hi i have a sorting question to ask.
My database has images with "Photoname" as one of the columns in mySQL table.
Database:
Photoname
Apple
Bear
Cat
Orange
So in my catalog.php, I would display all the images from the database. Then, i have a navigation bar(div) to allow viewers to filter the images. So users can view by A | B | C... | Z|.
My question is with regards to the URL and approarch. I would create imagesort.php which would handle all the mySQL in a general way( so that i don't have to waste time creating for A - Z).
Should the imagesort.php be something like imagesort.php?sort=A
? Then in imagesort.php how can i get the value A
? For example: select photoname, date from image where photoname LIKE 'a%'
And also if the above way is correct, how can i parse this variable A
to the link in catalog.php? Here is what i've done so far in my catalog.php:
<ul>
<li><a href="imagesort.php?sort=A">A</a></li>
<li><a href="imagesort.php?sort=B">B</a></li>
<li><a href="imagesort.php?sort=C">C</a></li>
...
</ul>
Do i really have to do this for 26 entries? or is there a simpler method?
You can use something like this...
<ul>
<?php for($i=65;$i<90;$i++) { ?>
<li><a href="imagesort.php?sort=&#<php echo $i;?>;">&#<php echo $i;?>;</a></li>
<?php } ?>
</ul>
If the links are formatted like that then they will end up in the $_GET super global:
$sort = $_GET['sort'];
For the second question, you can easily use a loop in php to run through the letters of the alphabet, to generate the pagination content you need:
$link_base = 'imagesort.php';
$pagination_content = '<ul>';
for($i=65; $i<=90; $i++)
{
$pagination_content .= '<li><a href="'.$link_base.'?sort='.chr($i);.'">'.chr($i).'</a></li>';
}
You'll need to get the value of sort
from the $_GET array. Make sure you sanitize your input before using it in your SQL query.
$match = "";
if(isset($_GET['sort']))
{
// Escape the string to guard against SQL injection
$match = mysql_real_escape_string($_GET['sort']);
}
$query = "select photoname, date from image where photoname LIKE '$match%'";
This code has the effect of working for any value of sort
, including an empty string. If you want to limit the valid strings to a single uppercase alpha character, you'll have to check separately for that, like so:
if(strlen($match) != 1 || ctype_upper($match) == false)
{
$match = "";
// Maybe some other error condition
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.