Ok, I’m having difficult trying to explain this in good words, but I’m selecting ALL default avatars from a table in my database that holds ALL avatars whether hidden or not..
The hidden field in forum_title is default to 0 (available to every user) and some are a value of 1 (the user will have to unlock it, it’s not automatically available).
Anyway, when they find an avatar, it gets inserted in user_title.
So user_title keeps track of the user_is and title_id…
But I want to show the user ALL default avatars ALONG with their hidden ones they unlocked..
The SQL works fine UNTIL I add the WHERE clause since the first table doesn’t have the user_id column… I only want to say WHERE user_title.user_id = $_SESSION[‘userid’] IF they have unlocked avatars before (obviously or there would be NO data in the second table hence the reason I left joined it)
This is using CodeIgniter, but I’ll post both MySQL and CodeIgniter versions so people can help me!
$this->db->select('id, phrase');
$this->db->from('forum_title');
$this->db->join('user_title', 'title_id = id', 'left');
$this->db->where('user_title.user_id', $this->session->userdata('user_id'));
$this->db->order_by('hidden');
And then here is the MySQL:
SELECT id, phrase
FROM forum_title
LEFT JOIN user_title ON title_id = id
WHERE user_title.user_id = $_SESSION['user_id']
ORDER BY hidden;
Thank you!
SQL for forum_title Table:
CREATE TABLE IF NOT EXISTS `forum_title` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`phrase` varchar(50) NOT NULL,
`hidden` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `forum_title` (`id`, `phrase`, `hidden`) VALUES
(1, 'OMGITSATITLE', 0),
(2, 'DStable Addict', 0),
(3, 'Randomnezz', 0),
(4, 'I Eat Hair...', 0),
(5, 'IMMA STAFFY', 1);
SQL for user_title Table:
CREATE TABLE IF NOT EXISTS `user_title` (
`user_id` bigint(20) NOT NULL,
`title_id` tinyint(3) NOT NULL,
PRIMARY KEY (`user_id`,`title_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
NOW my desired results:
ALWAYS show the titles that are default (hidden = 0), but show the hidden = 1 as well IF they have it unlocked which it will be in the user_title table if they have unlocked it.
You’re where statement is limiting your results to the ones that match the user ID, you need to specify the other ones you want too. Add the following to your query and if I understand you right that should solve the problem.