Codeigniter: Display an image stored in DB - image

How do I get an image stored in a field of my database, and put it into a variable for using it in an email message like a signature?
Here is my code:
//Model user_model
function getImage(){
$this->db->select("image");
$this->db->where("$id", user_id);
$query = $this->db->get("users");
if($query->num_rows()>0){
return result();
}else{
return false;
}
}
//Controller user_controller
function image(){
this->load->model("user_model");
$id = $this->session->userdata('id');
$image = $this->user_model->getImage($id);
echo $image;
}

Here is a example
Model user_model
// Pass the id function
function getImage($id){
$this->db->where($id, 'user_id');
$query = $this->db->get("users");
if($query->num_rows()>0){
return $query->row_array();
}else{
return false;
}
}
Controller user_controller
function image(){
$this->load->model("user_model");
$somedata = $this->user_model->getImage($this->session->userdata('id'));
$data['image'] = $somedata['image'];
$this->load->view('someview', $data);
}
On view
<?php echo $image;?>

Related

How to Share functions in same controller in one view

Hi i have a Controller called homeController and a view called home.blade.php (home). I have 2 functions in my homeController that i want to use in my view 'home'.
The function called index() , works fine, but how do i get the values from function userchart() to my view 'home'.
public function index(){
$value1 = '';
$value2 = '';
return view('home', compact('$value1', 'value2'));
}
public function userchart(){
$value3 = '';
$value4 = '';
return view('home', compact('$value3', 'value4'));
}
Now in my VIEW i can only access $value1 and $value2
foreach($value1 as $key){
echo $key->value;
}
How do i get access to the data '$value3 and $value4'
public function index(){
$data['value1'] = 'value1';
$data['value2'] = 'value2';
$data = array_merge($data, $this->userchart());
return view('home', $data);
}
public function userchart(){
$data['value3'] = 'value3';
$data['value4'] = 'value4';
return $data;
}

Explain the functionality or meaning of this : $data['row']

I have this doubt.
// This is my controller
public function profile()
{
$session_data = $this->session->userdata('logged_in');
$uid= $session_data['id'];
$pic = $this->user_model->getImage($uid);
$data['profile_pic']= $pic ;
$data['row'] = $this->user_model->get_user_data($uid);
$test = $this->load->view('profile_view',$data,TRUE);
echo $test;exit;
}
Based on name it seems to be:
public function profile()
{
$session_data = $this->session->userdata('logged_in');//checking user login or not
$uid= $session_data['id'];//getting id from session
$pic = $this->user_model->getImage($uid);//geting user image
$data['profile_pic']= $pic ;//storing image to data array
$data['row'] = $this->user_model->get_user_data($uid);//passing $uid to model and store in data array what row model returns
$test = $this->load->view('profile_view',$data,TRUE);//passing data array to view
echo $test;exit;
}
//model
i am passing user id only
public function get_user_data($uid){
//echo $uid;exit;
$data = array();
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$uid);
$query = $this->db->get();//checking that $uid data in database
if($query->num_rows() > 0){
$get_user_data=$query->result();
//print_r($get_user_data);exit;
//print_r($items);exit;
return $get_user_data; // if find, return data to controller
}
else{
return FALSE;
}
//return $query->row();
}

How to send value as parameter to model in codeigniter to get data?

I am new in codeigniter.
Currently working in e-commerce site I want to send the category_id as parameter to bring product data.
I want to know the steps to detail and Thanks
This is my model:
( function get_category(){ $query = $this->db->query("SELECT a.category_id,a.category_name from ci_intro.categories a order by a.category_id"); return $query->result(); } function get_product($id){ $q = $this->db->query('select * from products where category_id = $id'); if($q->num_rows() > 0){ foreach ($q->result() as $row) { $data[] = $row; } return $data; } } )
and this is my controller
( public function services() { $this->load->model("get_db"); $data['results'] = $this->get_db->get_category(); $id=$this->input->post('id'); $data['cat_id'] = $this->get_db->get_product($id); $this->load->view("view_header"); $this->load->view("view_nav"); $this->load->view("content_portfolio",$data); $this->load->view("site_footer"); } )
You get category_id in model as $_GET['category_id'] or $_REQUEST['category_id'] in model also .
class Pages extends CI_Controller {
public function view($page = 'home')
{
$this->load->model('services_model');
$id=$this->input->post('id');
$data['records']= $this->services_model->getData($id);
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
this is your controller example
and this is mlodel for you
class Services_model extends CI_Model {
function getUser($id) {
$q = $this->db->query('YOUR QUERY HERE');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
you can see various crud example and their website for further details
Your category should be the drop downlist. on change event the id will be pass through ajax like below
$(document).ready(function() {
$("#category").change(function(){
/*dropdown post */
$.ajax({
url:"<?php echo base_url(); ?>index.php/services/getproducts",
data: {catid: $(this).val()},
type: "POST",
success: function(data){
$("#product").html(data);
}
});
});
}
get prducts will be the function in services controller class you can get the category id using the
$this->input->post('catid');

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

Codeigniter database check

i am currently working on a project where users can save note snippets in there own little user area.
I was wondering how would i check if the id of an item exists for example if a user visits
http://mysite.com/view/1 and there is a note snippet of 1 it will display all the data relating to the id of one. Now if the user was to change the url to lets say 1000, that id doesnt exist and the view just errors.
i want to be able to redirect them back to a certain page with a error message "snippet does not exist" etc.
heres what i have so far ( i currently already have a conditional statement in here to check if the snippet is private, then if it is redirect back to /publicsnippets)
Controller:
class Publicsnippets extends CI_Controller {
function __construct()
{
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
redirect('/login/');
}
$this->load->model('dashboard_model');
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username']= $this->tank_auth->get_username();
}
public function index()
{
$this->data['public_snippets'] = $this->dashboard_model->public_snippets();
$this->load->view('dashboard/public_snippets', $this->data);
}
public function view($snippet_id)
{
$snippet = $this->dashboard_model->get_snippet($snippet_id);
if ($snippet['state'] === 'private')
{
$this->session->set_flashdata('message', "<b style=\"color:red;\">You are not allowed to view this snippet!</b>");
redirect('/publicsnippets');
} else {
$this->data['snippet'] = $snippet;
}
$this->load->view('dashboard/view_public_snippet', $this->data);
}
}
Model:
class Dashboard_model extends CI_Model {
public function public_snippets()
{
$this->db->select('id, title, description, tags, author, date_submitted');
$query = $this->db->get_where('snippets', array('state' => 'public'));
return $query->result_array();
}
public function private_snippets()
{
$this->db->select('id, title, description, tags, author, date_submitted');
$query = $this->db->get_where('snippets', array('user_id' => $this->tank_auth->get_user_id()));
return $query->result_array();
}
public function add_snippet($data)
{
$this->db->insert('snippets', $data);
$id = $this->db->insert_id();
return (isset($id)) ? $id : FALSE;
}
public function get_snippet($snippet_id) {
$query = $this->db->get_where('snippets', array('id' => $snippet_id));
return $query->row_array();
}
public function update_snippet($snippet_id, $data)
{
$this->db->where('id', $snippet_id);
$this->db->update('snippets', $data);
}
public function delete_snippet($snippet_id)
{
$this->db->where('id', $snippet_id);
$this->db->delete('snippets');
}
}
View:
<h3>Description</h3>
<p><?php echo $snippet['description']; ?></p>
<h3>Tags</h3>
<p><?php echo $snippet['tags']; ?></p>
<h3>Date Submitted</h3>
<p><?php echo $snippet['date_submitted']; ?></p>
<h3>Snippet</h3></pre>
<pre class="prettyprint"><?php echo $snippet['code_snippet']; ?></pre>
You work is fine just add a check like this in your view method
Controller
public function view($snippet_id)
{
$snippet = $this->dashboard_model->get_snippet($snippet_id);
if($snippet){
if ($snippet['state'] === 'private')
{
$this->session->set_flashdata('message', "<b style=\"color:red;\">You are not allowed to view this snippet!</b>");
redirect('/publicsnippets');
} else {
$this->data['snippet'] = $snippet;
}
$this->load->view('dashboard/view_public_snippet', $this->data);
}else{
$this->session->set_flashdata('message', "<b style=\"color:red;\">snippet does not exist</b>");
redirect('/publicsnippets');
}
}
if no row is found in get_snippet method the $snippet will contain false or null
and the second block of condition will run.
In your model change get_snippet() to check for rows:
public function get_snippet($snippet_id) {
$query = $this->db->get_where('snippets', array('id' => $snippet_id));
if ($query->num_rows() > 0) {
return $query->row_array();
} else {
return false;
}
}
Then in your controller:
if ($snippet = $this->dashboard_model->get_snippet($snippet_id)) {
// code if snippet exists
} else {
// code if snippet doesn't exist
}
OR
$snippet = $this->dashboard_model->get_snippet($snippet_id);
if ($snippet) {
// etc...

Resources