djfenom
asked on
Alphabetise after explode
I have a blog page which lists tags relating to particular blog articles.
The tags are displayed on the page and are populated from a database.
Tags are allocated to a particular blog and are then held in one field separated by a comma.
The tags are written to the page using the following code:
This works great, but my question is, how do I get the tags to come out in alphabetical order? I've looked at Sort, but can't work out where I would use it?
Thanks,
Chris
The tags are displayed on the page and are populated from a database.
Tags are allocated to a particular blog and are then held in one field separated by a comma.
The tags are written to the page using the following code:
$query_RStags = "SELECT DISTINCT blogTags FROM news";
$RStags = mysql_query($query_RStags, $conn) or die(mysql_error());
$row_RStags = mysql_fetch_assoc($RStags);
$totalRows_RStags = mysql_num_rows($RStags);
$alltags = array();
do {
foreach(explode(',', $row_RStags['blogTags']) as $tag) {
$taglink = strtolower(str_replace(' ','_',$tag));
if (!in_array($tag, $alltags)) {
if ($tag !="") {
if ($_GET['cat'] == $taglink) { $state = ' class="onstate"'; } else { $state = ''; }
array_push($alltags, $tag);
$catcount = mysql_query("SELECT * FROM news WHERE blogTags LIKE '%" . $tag . "%'", $conn);
$catrows = mysql_num_rows($catcount);
echo "<li><a href=\"/news/category/{$taglink}\"{$state}>{$tag}</a> ({$catrows})</li>\n";
}
}
}
This works great, but my question is, how do I get the tags to come out in alphabetical order? I've looked at Sort, but can't work out where I would use it?
Thanks,
Chris
btw, u don't need array $alltags anymore, cause u have it in the $alltags_echos dictionary as the keys.
ASKER
Thanks sedgwick, before I had:
c tag (1)
b tag (1)
a tag (1)
After adding your code it now comes out as:
c tag (1)
b tag (1)
c tag (1)
a tag (1)
b tag (1)
c tag (1)
Any ideas?
Chris
c tag (1)
b tag (1)
a tag (1)
After adding your code it now comes out as:
c tag (1)
b tag (1)
c tag (1)
a tag (1)
b tag (1)
c tag (1)
Any ideas?
Chris
the $tag is unique ?
ASKER
Not necessarily, more than one article could use the same tag, that's why I used DISTINCT in my query.
but the result of your query are distinct (as you say), which means no multiple $tags should be queried, right?
ASKER
No, it should just show them once.
Suggest you try ordering the query results set like this:
$query_RStags = "SELECT DISTINCT blogTags FROM news ORDER BY blogTags";
$query_RStags = "SELECT DISTINCT blogTags FROM news ORDER BY blogTags";
ASKER
Sorry for the delay.
The order by won't work as the field blogTags can have multiple tags in it separated by a comma.
The order by won't work as the field blogTags can have multiple tags in it separated by a comma.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Ray, unfortunately it's not an option to change the database design at this stage.
Where would I put var_dump() in my code?
Where would I put var_dump() in my code?
I have no idea where to put var_dump()! I cannot understand the code well enough because of the function overloading. You might try deconstructing this statement to get an intermediate array that you could visualize with var_dump()
foreach(explode(',', $row_RStags['blogTags']) as $tag) {
foreach(explode(',', $row_RStags['blogTags']) as $tag) {
...unfortunately it's not an option to change the database design at this stage.And it's not an option to get correct output from a misdesigned data base!
A professional would make a copy of the data base, modify the structure, revise the code, test the new data base, and once satisfied that things were working correctly, push the build to enable the new functionality. It could reasonably be expected to take the better part of a day. And you would probably need to take the site down for a few minutes during the switchover We all make mistakes, and the design of this data base is a mistake. It should be corrected
And a professional would not try to learn PHP by copying Dreamweaver code, either. If you want to get a foothold in how to write PHP, this book will give you a head start. By the time you get to page 325, you'll never need to look to Dreamweaver again!
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/
ASKER
Don't have a cow man!
So I'm clearly not a 'professional' like you, you've ascertained that, I've given you the points, now move on and don't shoot me down.
So I'm clearly not a 'professional' like you, you've ascertained that, I've given you the points, now move on and don't shoot me down.
Open in new window