Reputation: 777
on my website I currently have a save to 'favorites' button which saves a recipe to the users favorites and displays their favorites on their dashboard. I have managed to display the favorites on their dashboard with hyperlinks surrounding them. However I can't quite figure out how to actually connect each favorite to its link. Below is the code to retrieve the favorites stored on the database.
$favs = array();
$links = array();
$sql = "SELECT * FROM recipe WHERE fav='yes'";
$records = mysql_query($sql);
while($result =mysql_fetch_assoc($records)){
$favs[] = $result['recipeName'];
$links[] = $result['url'];
}
I have also saved the url of each recipe on my database, so just need to print out each favorite with the link to their url. below is the code to display the favourites surrounded by a tags. I have also managed to return the url links from the database but just need to conect them to each recipe.
foreach ($favs as $fav) {
echo '<a href=''>'.$fav.'</a>'.' ';
}
Upvotes: 0
Views: 50
Reputation: 3665
You don't need foreach loop and opening blank arrays here:
while($result =mysql_fetch_assoc($records)){
echo '<a href="'.$result['url'].'">Go to '.$result['recipeName'].'</a>';
}
Upvotes: 1
Reputation: 13699
I suggest you put the recipeName and the URL in a single array.
e.g.
$i = 0;
while($result =mysql_fetch_assoc($records)) {
$fav_links[i] = array('recipeName' => $result['recipeName'], 'url' => $result['url']);
}
Then in your foreach:
foreach ($fav_links as $fav) {
echo '<a href="'.$fav["url"].'">'.$fav["recipeName"].'</a>'.' ';
}
Upvotes: 1