Closed Thread
Results 1 to 6 of 6

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

  1. #1
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

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

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

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

    Sorry to bump my own thread... Anyone got any ideas please?

  4. #3
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

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

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

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

    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?

  6. #5
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

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

    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.

  7. #6
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

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

    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!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [category] - [sub category]
    By lol33d in forum PHP Development
    Replies: 4
    Last Post: 04-20-2011, 09:04 AM
  2. Link to specific category
    By ly0ha in forum ionFiles
    Replies: 3
    Last Post: 10-05-2008, 05:05 PM
  3. Posting a link to a text field
    By holla22 in forum PHP Development
    Replies: 3
    Last Post: 07-19-2007, 04:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts