Hi Guys,
I hope you can help me on this: I have a table with a field called 'category' and 'catslug' (stands for category slug)
The slug is because I have categories called " Sound & Isolating" where if I was to use it in a URL it would be ugly because it would read something like /Sound%20&%20Isolating/
So I would make an entry for the catslug to be 'sound-and-isolating' so that it becomes URL friendly like /sound-and-isolating/
Now my problem becomes linking the category with the slug. For instance, I have this code in my sidebar which lists out the categories:
But when I change the href to $row['catslug'] like in this:Code:<?php $result = mysql_query("SELECT DISTINCT category FROM tbprofo"); echo "<ul>"; while($row=mysql_fetch_array($result)) { echo "<li><a href='categories.php?cat=".strtolower($row['category'])."'>".$row['category']."</a></li>"; } echo "</ul>"; ?>
It doesnt echo out my catslug field (i.e. it echos out only the domainname.c0m/categories.php?cat=). Its like it just doesnt know what record its coming from or something.Code:<?php $result = mysql_query("SELECT DISTINCT category FROM tbprofo"); echo "<ul>"; while($row=mysql_fetch_array($result)) { echo "<li><a href='categories.php?cat=".strtolower($row['catslug'])."'>".$row['category']."</a></li>"; } echo "</ul>"; ?>
But can you see what I am trying to do with this code? Im trying to display the text of the category (so for a user it reads "Sound & Isolating") but the href link reads categories.php?cat=sound-and-isolating
Any ideas what I am doing wrong please?
Thanks so much for all your help, this forum is a great help to me, and I promise Ill contribute when I become knowledgeable!
Sorry to bump my own thread... Anyone got any ideas please?
In the second one, you aren't selecting catslug, and you haven't done any limits on your query. I'm not clear on what your database structure is, either.
Thanks very much for your reply WingedPanther.
In the first one I am echoing only the category for the URL:
echo "<li><a href='categories.php?cat=".strtolower($row['category'])."'>".$row['category']."</a></li>";
In the second one I am attempting to echo the category slug:
echo "<li><a href='categories.php?cat=".strtolower($row['catslug'])."'>".$row['category']."</a></li>";
The problem is that it doesnt seem to pull the catslug field (yes there is valid data in this field)
My sql query is deliberately:
"SELECT DISTINCT category FROM tbprofo"
Because I want to pull all the categories only once to list them in my sidebar.
So I can pull data from my database when I use row['category'] but I cant pull it when I use $row['catslug'].
Any ideas?
I think there is an easier way to ask my question...
Is there a way to write something like this:
1 -> mysql_query("SELECT DISTINCT (category + catslug) FROM tbprofo")
Instead of just
2 -> mysql_query("SELECT DISTINCT category FROM tbprofo")
because in 2 I am just selecting the category fields to put into my array, but in 1 I also want to select the catslug field.
I figured it out...
If anybody wants the answer it is this:
$result = mysql_query("SELECT catslug, category FROM tbprofo GROUP BY catslug ORDER BY category");
cheers guys!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks