Hello,
I want to create a favorites option for something I am working on. I have two tables. The main table where the posts are kept called "place".
+---------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| userid | int(3) | YES | | NULL | |
| address | varchar(300) | YES | | NULL | |
| desc | varchar(550) | YES | | NULL | |
| phone | int(15) | YES | | NULL | |
| image | varchar(50) | YES | | NULL | |
| website | varchar(100) | YES | | NULL | |
| cat | varchar(25) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------+--------------+------+-----+-------------------+----------------+
and the favorite table called "wait"
+---------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| userid | int(11) | YES | | NULL | |
| place_id | int(11) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------+-----------+------+-----+-------------------+----------------+
I am using codeigniter and displaying the data on the home page like this.
<? foreach ($places as $place): ?>
<? echo $place->title; ?>
<a href="#" id="addfav">Add to favorites</a> <----- This sends the user's id and place id to wait table. I want to display this if the current user hasn't clicked on it already.
<? endforeach; ?>
I tried something like this:
$query_str ="select place.id, place.name, place.desc, wait.userid, users.username
from place left join wait on place.id=wait.chat_id
left join users on place.userid=users.id";
I am also querying the users table to get the user who created the post. Not too familiar with joins so any help is appreciated. I only have two posts and two users at the moment and if both users favorite each post, the foreach displays four posts instead of two.