1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Any Ideas How To Link A Category And Slug Field Together?

Discussion in 'PHP & Perl' started by back2black, Jul 29, 2009.

  1. back2black

    back2black Junior Member

    Joined:
    Dec 20, 2008
    Messages:
    116
    Likes Received:
    28
    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:

    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>";
    	?> 
    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['catslug'])."'>".$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.

    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!
     
  2. lord

    lord Junior Member

    Joined:
    Jan 21, 2009
    Messages:
    176
    Likes Received:
    251
    use a print_r($row) before and see what does that contains (if your slug is printed then you have the name right there)

    if you don't get any errors, there is a slight possibility that you don't have the slug defined in your database so it's blank or null
     
  3. Killswitch

    Killswitch BANNED BANNED

    Joined:
    Aug 9, 2009
    Messages:
    14
    Likes Received:
    5
    SELECT DISTINCT(category), catslug FROM tbprofo