Dynamically display a single profile image - codeigniter

I tried different ways, but so far nothing. There are similar topics here but they don’t seem to address my particular situation. I’d like to display a single user profile image from an “images” table with a foreign key link to a “users” table. I have a scenario that works but it’s displaying multiple images with a “foreach” statement. The images are also saved in the local “uploads” folder in the root directory. Here is the code that works for multiple images in the view:
<?php foreach($images_model as $images):?>
<img src="<?php echo base_url()."uploads/".$images->fileName;?>" alt="" class="img-thumbnail">
Where “fileName” is the column from the “images” table. The images_model looks like this:
function get_images(){
$this->db->from('images');
$this->db->order_by('date_uploaded', 'asc');
$query = $this->db->get();
return $query->result();
}
The image-upload section from the controller is below:
if($this->upload->do_upload()) {
$file_data = $this->upload->data();
$data['user_id'] = $this->session->userdata('user_id');
$data['fileName'] = $file_data['file_name'];
$data['filePath'] = $file_data['full_path'];
$data['fileType'] = $file_data['file_type'];
$data['fileSize'] = $file_data['file_size'];
$data['date_uploaded'] = date('Y/m/d H:i:s');
}
Here is the necessary portion of the ‘images’ table:
Is there a painless way to display a single image from the “images” table that would take into account the active “user_id” which is the foreign key in the “images” table? In other words, to display a user’s desired image as profile.
Thank you for helping. Appreciate any and all input.

In your model function get_images you are getting all the records from the table so multiple images are displaying.
You have user_id in your session use it to get one user record.
Controller
public function SingleUserImage(){
$data['images'] = $this->images_model->get_images();
return $this->load->view('filename', $data);
}
Change model query like this
MODEL
function get_images(){
return $this->db->get_where('images', ['user_id' => $this->session->userdata('user_id')])->row();
}
You have single user record(only one row) so no need to use foreach loop
View
According to table data that you have provided, image full path is saved use only variable in the src attribute of img tag
<img src="<?php echo $images->fileName;?>" alt="" class="img-thumbnail">
Update:-
According to your requirement with the loop use this
<?php foreach($images_model as $images):
if($images->user_id == $this->session->userdata('user_id')):?>
<img src="<?php echo base_url()."uploads/".$images->fileName;?>" alt="" class="img-thumbnail">
<?php endif; endforeach;?>

Related

How to get order details dynamically?

I need to retrieve an order from Magento by its id.
How do I load a specific order by id?
so far i have tried this:
<?php
$orderNumber = 145000013;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
// get order item collection
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item){
$product_id = $item->product_id;
$product_sku = $item->sku;
$product_name = $item->getName();
$_product = Mage::getModel('catalog/product')->load($product_id);
$cats = $_product->getCategoryIds();
$category_id = $cats[0];
$category = Mage::getModel('catalog/category')->load($category_id);
$category_name = $category->getName();
echo "orderNumber=".$orderNumber."<br/>";
echo "orderValue=".$orderValue."<br/>";
echo "product_name=".$product_name."<br/>";
echo "product_id=".$product_id."<br/>";
echo "product_sku=".$product_sku."<br/>";
echo "category_id=".$category_id."<br/>";
echo "category_name=".$category_name."<br/><br/>";
}
?>
it works fine for static order...but i want to get dynamically.
First you need to create a form that post to a custom controller that include your code as above
Then update your code with
$orderNumber = $this->getRequest()->getParams('field_name');;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
See this How to get post data variables after submition a form in magento
For this you need to follow the following steps:
Create a form to search for order details
You can add your custom code on your desired location.
Then you need to submit your search form on the URL where you want to show the order details. there must be one template file associated with the form submitted URL.
In the Template file you can use your above specified code. but as you want to make it dynamic need to fetch submitted order number using the following way:
$orderId = $this->getRequest()->getParam('search_orderid'); //search_orderid is name of search box
Search Form:
<form id="orderSerchForm" method="post" action="<?php echo $this->getUrl('sales/order/history') ?>">
<input type="text" name="search_order" id="search_order" placeholder="Enter Order ID" />
<input type="submit" />
</form>

Get image from database using codeigniter

I have tried to set image in database using blob datatype and retrieve from database using codeigniter
this is my viewfile to retrive image from database
Database
VIEW
<img src="<?php echo site_url("imageUpload/loadImg/")?>/3">
controller
function loadImg($id){
$this->load->model("imageUploads");
$rec = $this->imageUploads->getimage($id);
$ext = $this->imageUploads->getext($id);
$this->output->set_header('Content-type: image/'.$ext);
echo $rec;
}
Model
function getimage($image){
$query2 = $this->db->query("SELECT images FROM windows_users_image_upload WHERE user_id = $image")->row();
return $query2->images;
}
function getext($id){
$query = $this->db->query("SELECT ext FROM windows_users_image_upload WHERE user_id = $id")->row();
return $query->ext;
}
But its return me text not image.
Please Help Me to Retrive image from blob datatype
using codeigniter.
I used this to retrieve blob image
<img src="<?php echo 'data:' . $uploaded->file_type . ';base64,' . base64_encode($uploaded->image_data); ?>" width="200" />

Add custom category attribute to the frontend.

Pages of different category should display different footers and is the idea behind.
I know there are lot of similar questions and I tried them all, but they didn't work.
I have added custom category attribute to category page, and I want to see it in the footer.
I tried to use:
<?php if($_customAttribute = $this->getCurrentCategory()->getCustomAttribute()): ?>
<?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'footer_text') ?>
But I have not got any results for this.
You can try it like this.
$category = Mage::registry('current_category');
<?php if ($category->getFooterText()) : ?>
<?php echo Mage::helper('catalog/output')->categoryAttribute($category, $category->getFooterText(), 'footer_text');?>
<?php endif;?>
But keep in mind...if you add this in footer.phtml it won't work with the block cache on. The footer is cached, so once you load a page the code above will have no effect on the next pages.
[EDIT]
If you want to have a different footer on some pages you need to modify the cache key for the footer block. There is an explanation on how to do that in here but it's for a simple page not a category. The concept is the same.
What you need to do is to change only the getCacheKeyInfo method to depend on the category. Something like this:
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
$category = Mage::registry('current_category');
if ($category) {
$info[] = $category->getId();
}
return $info;
}

CakePHP How to store an image in db and display it?

I do not know how to store an image in db and display it?
What i want to do is to store image path in db and store the image in /img/ folder and then display it to the user.
In my registration form i am asking user to upload image like this
<?php echo $this->Form->input('image', array('class'=>'forminput', 'label' => 'Upload Image:', 'type' => 'file')); ?>
and in database type of image field is longblob.
Displaying it to the user like this...
<?php echo $this->html->image('Auth.User.image');?>
You store the image on the server and in the DB you save only the name of the image.
Usualy the images are stored in img folder in webroot. When you will display the image you will have something like this:
<?php echo $this->Html->image('img' . DS . CakeSession::read('Auth.User.image')); ?>
where image is the name of the image you are uploading saved to the image field in the Users table.
Inside the controller you should upload the file as you do normaly in PHP.
$file_name = $_POST['User']['name'];
$tmp_file = $_POST['User']['tmp_name'];
if(is_uploaded_file($tmp_file)){
if(move_uploaded_file($tmp_file){
...
$this->data['User']['image'] = $file_name;
$this->User->save($this->data);
}
}
paste it in your controller and change the field and model names accroding to
public function add() {
$this->set('status_tbl', $this->Player->Status->find('list'));
if ($this->request->is('post')) {
$this->Player->create();
if(!empty($this->request->data)){
if(!empty($this->request->data['Player']['image']['name'])){
$file = $this->request->data['Player']['image'];
$ext = substr(strtolower(strrchr($file['name'], '.')), 1);
$arr_ext = array('jpg', 'jpeg', 'gif');
if(in_array($ext, $arr_ext)){
$this->request->data['Player']['image'] = $file['name'];
move_uploaded_file($file['tmp_name'],'uploads/file'.$file['name']);
$this->Player->save($this->request->data);
}
}
if($this->Player->save($this->request->data)){
return $this->redirect(array('controller'=>'Players',"action"=>'index'));
}
}
}
}

Post data in codeigniter can be retrieved as an array or object?

In one of my new codeigniter project, one of my colleague wrote a helper method array_to_object so that he can call the variables in views as $row->field instead of $row['field'].
I thought codeigniter default behaviour allows us to retrieve data from database in $row->field (as object). Can anyone pls enlight me data flow in codeigniter?
Codeigniter supports both an array and an object oriented style to retrieve data from DB so both of these styles are equal(from the user guide):
oop style
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
array style
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['email'];
}
here is the user guide:
http://codeigniter.com/user_guide/database/examples.html

Resources