codeigniter pass data from view to model and retrieve info
Here is the thing: I have controller that retrieve data from database and pass info to view. In the view I have a loop like this:
foreach ($myphotos->result() as $myphoto){
echo $myphoto->p_id
}
But in that loop I need to pick up $myphoto->p_id and ask database to retrieve me users with this p_id.
SELECT * FROM users WHERE u_id IN (SELECT u_id FROM p_votes WHERE p_id = 268);
and in:
foreach ($myphotos->result() as $myphoto){
echo $myphoto->p_id
$this->load->model('m_member');
$users_voted = $this->m_member->getUsersVotedOnImage($myphoto->p_id);
foreach ($users_voted->result() as $users){
echo '<div id="voterfooter">voted:<a href="$users->i_id">'.$users->name.</a>
}
}
Ok that was one option that I come up, but there is also another option, but select statement is so complicated!
here is how: I already have this one:
SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$segment_url.", ".$config['per_page'];
But how to pick up also from users people that voted on that picture and print it in view?
Here is database scheme:
CREATE TABLE IF NOT EXISTS `users` (
`u_id` int(10) unsigned NOT NULL auto_increment,
`fb_id` varchar(255),
`fb_url` varchar(255),
`email` varchar(100),
`username` varchar(100),
`name` varchar(100),
`lastname` varchar(100),
`mini_pic_fb` varchar(255),
`mini_pic`开发者_开发问答 varchar(255),
`country` varchar(100),
`place` varchar(100),
`address` varchar(100),
`nr` int(10),
`postcode` varchar(10),
`pass` varchar(35),
`active` varchar(35),
`activation_code` varchar(42),
PRIMARY KEY (`u_id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `fb_id` (`fb_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=229 ;
CREATE TABLE IF NOT EXISTS `photos` (
`p_id` bigint(20) unsigned NOT NULL auto_increment,
`u_id` bigint(20) unsigned NOT NULL default '0',
`p_date` datetime NOT NULL default '0000-00-00 00:00:00',
`p_content` longtext NOT NULL,
`p_title` text NOT NULL,
`p_photo` text NOT NULL,
`p_small` text NOT NULL,
`p_thumb` text NOT NULL,
`p_up` bigint(20),
`p_down` bigint(20),
PRIMARY KEY (`p_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=229 ;
CREATE TABLE IF NOT EXISTS `p_votes` (
`pv_id` bigint(20) unsigned NOT NULL auto_increment,
`u_id` bigint(20) unsigned NOT NULL,
`p_id` bigint(20) unsigned NOT NULL,
`pv_date` datetime NOT NULL default '0000-00-00 00:00:00',
`pv_ip` varchar(200) NOT NULL,
PRIMARY KEY (`pv_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=229 ;
I braked MVC pattern but I didn't know how to do this other way! If someone knows how to do this without breaking MVC please respond! In the view:
foreach ($myphotos->result() as $myphoto){
$query = "SELECT * FROM users WHERE u_id IN (SELECT u_id FROM p_votes WHERE p_id = ".$myphoto->p_id.")";
$voters = $this->db->query($query)->result();
echo $myphoto->p_title;
foreach($voters as $row){
echo '<div id="voterfooter">';
echo '<a href="'.base_url().'clanovi/clan/'.$row->u_id.'">';
echo $row->name.' '.$row->lastname.'</a>';
echo '</div>';
}
}
精彩评论