1

Not sure if there a way of doing this, but I can insert multiple categories into MYSQL, using :

GetSQLValueString(implode($_POST['r_category'],", "), "text"),

So then when I echo:

<?php echo $row_Recordset1['r_category']; ?>

It is fine, like this: Cat 1, Cat 2, Cat 3

I am trying to find a way to link each category for easy navigation purposes!!

Like this:

<a href="/page/results.php?r_category=<?php echo $row_Recordset1['r_category']; ?>"><?php echo $row_Recordset1['r_category']; ?></a>

This works great for One Cat, but if I have multiple Cats, then it is one big link.... not what I want.

I need to use implode or explode, but not sure how ? Thanks in advance!!!

This is what I would love:

Cat 1, Cat 2, Cat 3 (these are all separated links pulling from one row!)

1 Answer 1

2

If $row_Recordset1['r_category'] is the string "Cat 1, Cat 2, Cat 3", then you can explode that into an array like this:

$arr = explode(",", $row_Recordset1['r_category']);

then step through your array to create the links:

$links = array();
foreach ($arr as $value)
{
    $links[] = "<a href='/page/results.php?r_category=". trim($value) ."'>". trim($value) ."</a>";
}
$links_str = implode(", ", $links);
echo $links_str;
Sign up to request clarification or add additional context in comments.

3 Comments

hmm.. great let me try. Does this work if i just have one Cat in the string, like Cat 1 only.. ?
EXCELLENT!! It worked, but now I am having problems echoing the result with a comma. If there is just one Cat, then their shouldn't be any comma, but if multiple, like Cat 1 and Cat 2, then there should be a comma separating them?? 'echo "<a href='/page/results.php?r_category=". trim($value) ."'>". trim($value) ."</a>, ";'
@eberswine see my edit for how to handle that properly. We'll store each link in an array and re-implode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.