Hi all,
I have been confusing myself for days over this so bear with me if it gets a bit confusing.
I have a table called tags with columns id, name which list all unique tags (linux, php, mysql, etc.). And I have an array that includes some tags that have been entered:
$tags = array('linux,php,mysql');
Now when the array has just one tag I can search the tags table and return whether the tag already exists in the table or not. But I am unsure how I can go about checking for each array item. My thinking on how to go about this (and I could be wrong) is:
- Explode the array.
- Check the first item in the array to the table.
- If it does not exist, put it into a separate array.
- Once all items have been iterated, store these unique tags in the table.
For reference, currently I have:
function tagExists($pdo, $tag) {
$stmtTagExists = $pdo->prepare('SELECT * FROM tags WHERE name=?');
$stmtTagExists->execute([$tag]);
return $stmtTagExists->fetch(PDO::FETCH_ASSOC);
}
$tags = array('linux');
foreach($tags as $item) {
if (tagExists($pdo, $item)) {
echo 'The tag ' . $item . ' exists! <br>';
} else {
echo 'The tag ' . $item . ' does not exist! <br>';
}
}
If you need any clarification on anything, just ask.
TIA