I have an array of 'search tags' submitted by my users, such as apples, oranges, anger, love etc. Each set of these tags are put into an array. The array represents a set of tags submitted by a unique user.
I want to take these tags, submit them into the database, and fetch the corresponding books in the same row as these tags. The books must be in the same kind of array structure as the tags so that they can be assigned back to each user in the html script.
Example: tagsarray[0][1] = 'love'
, booksarray[0][1] = 'the book corresponding to 'love''
I had an idea of how I could make this work, using loops and push arrays. I wrote this script below, but it simply isn't working.
<?php
$tags = array(array("apples","oranges", "pears"),
array("sun", "blood"),
array("anger", "love"));
$con = mysql_connect("Stories.mysql.anyhost.com", "user", "password");
mysql_select_db("Stories");
foreach ($tags as $tag) {
$one = array();
$two = array();
for ($i = 0; $i < count($tag); $i++) {
$tag_value = $tag[$i];
$selectbooks = "SELECT * FROM Journeys
WHERE book_tags = '$tag_value'";
$query = mysql_query($selectbooks);
while ($row = mysql_fetch_assoc($query)) {
$one[] = $row['books'];
}
$two[] = $one;
}
}
echo json_encode($two);
// should echo out [ [books1, books2], [books3, books4], [books5, books6] ] - but instead it returns [[]]
mysql_close($con);
?>
It would be great if someone could pinpoint the fault with my script and/or suggest a better alternative.