I am trying to retrieve tags that are associated with a society. The code below works, but it is only retrieving the first tag and not the rest in the table.
$user = $_SESSION['user'];
$society = $_SESSION['society'];
$soc_q = mysql_query("SELECT socID, creator, socName, type, datetime
FROM societies.society WHERE socName = '$society'");
$soc_row = mysql_fetch_array($soc_q, MYSQL_ASSOC);
$tag_q = mysql_query("SELECT society.socID, tagID, name
FROM societies.society
INNER JOIN societies.tags ON society.socID = tags.socID
WHERE society.socID = '$soc_row[socID]'");
$tag_row = mysql_fetch_array($tag_q, MYSQL_ASSOC);
$the_tags = $tag_row['name'];
if (!@$_SESSION['existing_tags']) {
$_SESSION['existing_tags'] = $the_tags;
}
$existing_tags = $_SESSION['existing_tags'];
$tags = explode(' ', $the_tags);
// ajax
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && @$_GET['tag']) {
$match = array();
foreach ($tags as $tag) {
if (stripos($tag, $_GET['tag']) === 0) {
$match[] = $tag;
}
}
echo json_encode($match);
exit;
You have to load tags in loop like this (get each row):
Edit: using GROUP BY
If you are willing to play with mySQL and element grouping you may use
GROUP_CONCAT(if you need only tag names):The code above isn’t that useful, but when you’ll combine it with your first query, you’ll get:
Note: prefixing tables with database names makes your code hard to read because once you’re doing that, other time not.