Loading images from database php codeigniter - php

Im trying to load images from the database, i can get the full string and no display on the image and when i use the full array I get a array message.
here is the model :
public function get_webstore($webstore_id = '') {
/* get video */
if (empty($webstore_id)) {
$webstores = $this->get('webstore');
} else {
$webstores = $this->get('webstore', $webstore_id);
}
$webstore_array = array();
foreach ($webstores as $webstore) {
$singleWebstorePair = array();
if (!empty($webstore['images'])) {
$allWebstores = unserialize($webstore['images']);
foreach ($allWebstores as $allWestores) {
$singleWebstorePair[] = str_replace("./", "", base_url($allWebstores));
}
}
$webstore_array[] = array(
'id' => $webstore['id'],
'title' => $webstore['title'],
'content' => $webstore['content'],
'images' => $singleWebstorePair,
'created' => $webstore['created']
);
}
return $webstore_array;
}
and here is the front end code
<div class="col-md-12" style="height: 225px;">
<div class="col-md-4 text-center thumbnail123">
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
<h3><?php echo $webstore['title']; ?></h3>
<p>Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?></p>
<hr>
<hr>
</div>
here is the controller if needed:
public function __construct() {
parent::__construct();
$this->load->model("post");
$this->load->database();
$this->load->helper("url");
}
public function index() {
$this->data['posts'] = $this->post->get_webstore($limit=null, $offset=null); // calling Post model method getPosts()
$this->data['page_title'] = 'Store';
$this->layout("pages/webstore", $this->data);
and the error:
Severity: Notice
Message: Array to string conversion
Filename: pages/webstore.php
Line Number: 45
thanks guys

When you populate your array, you set 'images' => $singleWebstorePair,˙ which is an array.
In your template, your are echoing this:
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
But here $webstore['images'] is an array. So if $webstore['images'] holds multiple image source, then loop through it:
<?php foreach($webstore['images'] as $image) : ?>
<img class="thumbnail" width="225px" src="<?php echo $image ?>.jpg"/>
<?php endforeach; ?>

Related

loop through table to display all images where register_id = ? in codeigniter

i've been working on a project for a while now using CodeIgniter. I want to display all images of same $register_id from the database with a foreach loop on the view.php.
Controller:
public function view($register_id = NULL){
$data['image'] = $this->image_model->get_all_images($register_id);
if(empty($data['image'])){
show_404();
}
$data['title'] = $data['image']['register_id'];
$this->load->view('templates/header');
$this->load->view('portfolios/view', $data);
$this->load->view('templates/footer');
}
Model
public function get_all_images($register_id = FALSE){
$query = $this->db->get_where('images', array('register_id' => $register_id));
return $query->row_array();
}
view.php
<?php foreach ($image as $img) : ?>
<div class="col-md-3">
<h3><?php echo $image['title']; ?></h3>
<div class="img-box">
<img class="post-thumb" src="<?php echo site_url(); ?>assets/images/portfolios/<?php echo $image['register_id'];?>/<?php echo $image['url']; ?>">
</a></div>
Shows same image 14 times..
query works perfectly on phpmyadmin & shows diff. images..
change your forloop variable. and its value variable too. may be solve.
<?php foreach ($image as $value){?>
<div class="col-md-3">
<h3><?php echo $value['title']; ?></h3>
<div class="img-box">
<img class="post-thumb" src="<?php echo site_url(); ?>assets/images/portfolios/<?php echo $value['register_id'];?>/<?php echo $value['url']; ?>">
</a>
</div>

adding alt attribute to a banner image (opencart)

On the website we have banners all over but non of them have any alt text.
Below is the code for, any pointers as to how i can solve this issue? I was thinking maybe having the alt as the image name if possible so need to find a way to echo it.
Any other suggestions? I have 8 banners and don't mind manually adding the alt text for each banner if possible
<?php if ($module_title){ ?>
<div class="box-heading"><?php echo $module_title; ?></div>
<?php } ?>
<div class="box rich_banner grid_holder">
<?php foreach($sections as $section){ ?>
<div class="banner_<?php echo $columns; ?>">
<div class="image zoom_image_container"><img class="zoom_image" alt="" src="<?php echo $section['image']; ?>" />
<?php echo $section['description']; ?>
</div>
</div>
<?php } ?>
</div>
would this be the section of variable below?
<?php
class ControllerExtensionModuleCosyoneBanner extends Controller {
public function index($setting) {
if(empty($setting['module_title'][$this->config->get('config_language_id')])) {
$data['module_title'] = false;
} else if (isset($setting['module_title'][$this->config->get('config_language_id')])) {
$data['module_title'] = html_entity_decode($setting['module_title'][$this->config->get('config_language_id')], ENT_QUOTES, 'UTF-8');
}
$data['columns'] = $setting['columns'];
if (isset($setting['sections'])) {
$data['sections'] = array();
$section_row = 0;
foreach($setting['sections'] as $section) {
$this->load->model('tool/image');
if (isset($section['block'][$this->config->get('config_language_id')])){
$block = html_entity_decode($section['block'][$this->config->get('config_language_id')], ENT_QUOTES, 'UTF-8');
} else {
$block = false;
}
if (isset($section['thumb_image'])){
$image = 'image/' . $section['thumb_image'];
} else {
$image = false;
}
$section_row++;
$data['sections'][] = array(
'index' => $section_row,
'description' => $block,
'image' => $image
);
}
return $this->load->view('extension/module/cosyone_banner', $data);
}
}
}
You are able to use the description in your alt, I think for what's available that is the best solution.
<div class="image zoom_image_container"><img class="zoom_image" alt="<?php echo $section['description']; ?>" src="<?php echo $section['image']; ?>" />

Trying to get property of non-object error using codeigniter

I've problem with codeigniter. this is the message
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: employee/detail.php
Line Number: 15
I heve model like this:
public function getByID($id){
$this->db->select('*');
$this->db->from('master_employee me');
$this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
$this->db->where('me.me_id', $id);
$query = $this->db->get();
return $query->row();
}
Controller:
public function edit($id){
/**
* [$data get data from database]
* #var array
*/
$data = array($id);
$data['msg'] = $this->_get_flashdata();
$data['rows'] = $this->m_employee->getByID($id);
$data['position'] = $this->m_position->get();
/**
* [$html call all wireframe]
* #var array
*/
$html = array();
$html['header'] = $this->load->view('admin/header',$data,true);
$html['kiri'] = $this->load->view('admin/kiri',null,true);
$html['content'] = $this->load->view('admin/employee/edit',$data,true);
$this->load->view('admin/template',$html);
}
and view like this
<?php if($rows->me_photo == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows->me_name); ?>" class="img-responsive" style="margin-bottom:10px" />
<?php endif; ?>
How to solve my problem? Please
Try this one:
<?php if($rows['me_photo'] == NULL): ?>
<img src="<?php echo base_url('/upload/be/employee/default-no-image.png'); ?>" class="img-responsive" title="no-photo" style="margin-bottom:10px" />
<?php else: ?>
<img src="<?php echo base_url('/upload/be/employee'.$rows['me_name']; ?>" class="img-responsive" style="margin-bottom:10px" />
It's an array. Not object. -> stands for object.
You need to change $rows->m_ephoto like this $rows['me_photo'].
Modify your query
public function getByID($id){
$this->db->select('*');
$this->db->from('master_employee me');
$this->db->join('master_position mp', 'mp.mp_id=me.mp_id');
$this->db->where('me.me_id', $id);
$query = $this->db->get();
return $query->result();
}
in in view use like this
foreach ($query->result() as $row){
echo $row->title;
}
read here i hope you will get the answer

Codeigniter: showing post thumbnail with every post

I am trying to setup a demo blog and i want every post to have a thumbnail. Look at the blog first: my demo blog. I can create new blog post from here creating new post and successfully show them in the blog. I also have gallery page where i can upload images, look here: gallery page. but i want the new post page to have the image upload option and it should add a single image to every post that i publish from the new post page like so: example. How i can i do that? I have tried so many times but failed to show image with every post.
Model for post:
class Post extends CI_Model
{
function get_posts($num=20, $start=0) {
$this->db->select()->from('posts')->where('active',1)->order_by('date_added', 'desc')->limit(20,0);
$query = $this->db->get();
return $query->result_array();
}
function get_posts_count() {
$this->db->select('postID')->from('posts')->where('active', 1);
$query = $this->db->get();
return $query->num_rows();
}
function get_post($postID) {
$this->db->select()->from('posts')->where(array('active'=>1, 'postID'=> '$postID'))->order_by('date_added', 'desc');
$query = $this->db->get();
return $query->first_row('array');
}
function insert_post($data) {
$this->db->insert('posts', $data);
return $this->db->insert_id();
}
}
Controller for post:
class Posts extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->model('post');
$this->load->helper('form');
}
function index($start=0) {
$data['posts']=$this->post->get_posts(5, $start);
$this->load->library('pagination');
$config['base_url'] = base_url() . 'posts/index/';
$config['total_rows'] = $this->post->get_posts_count();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['pages'] = $this->pagination->create_links();
$this->load->view('header');
$this->load->view('post_index',$data);
$this->load->view('footer');
}
function post($postID) {
$data['post']= $this->post->get_post($postID);
$this->load->view('post',$data);
}
function new_post() {
if ($_POST) {
$data=array(
'title'=> $_POST['title'],
'post' => $_POST['post'],
'active'=> 1
);
$this->post->insert_post($data);
redirect(base_url().'posts/');
} else {
$this->load->view('new_post');
}
}
}
Index page view: where all blog posts are shown:
<?php
if (!isset($posts)) {
?>
<p>there is currently no post in the database</p>
<?php
} else {
foreach ($posts as $row) {
?>
<h2 class="title"><a class="link_title" href="<?php echo base_url()?>posts/post/<?echo $row['postID']?>"><?php echo $row['title']?></a><a class="edit" href="<?php echo base_url()?>posts/editpost/<?echo $row['postID']?>">Edit</a> / <a class="delete" href="<?php echo base_url()?>posts/<?echo $row['postID']?>">Delete</a></h2>
<p><?php echo substr(strip_tags($row['post']),0,200) . ".."?></p>
<p>Read more</p>
<hr>
<?php
}
}
?>
<?php echo $pages; ?>
This is the new post page view where i want the image upload option available:
<form action="<?php echo base_url()?>posts/new_post" method="post">
<p>Title: <input name="title" type="text"></p>
<p>Description: <br><textarea name="post" cols="50" rows="30"></textarea></p>
<p><input type="submit" value="Add Post"></p>
</form>
I have also some codes to upload image to the galley page. I am sharing this if it helps any.
Gallery model:
class Gallery_model extends CI_Model
{
var $gallery_path;
var $gallery_path_url;
function __construct()
{
parent::__construct();
$this->gallery_path = './images';
$this->gallery_path_url = base_url() . 'images/';
}
function do_upload() {
$config = array(
'allowed_types' => 'jpg|png|jpeg',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => true,
'width' => 150,
'height' => 75
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images() {
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file) {
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
);
}
return $images;
}
}
Gallery controller:
class Gallery extends CI_Controller
{
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('header');
$this->load->view('gallery', $data);
$this->load->view('footer');
}
}
Gallery view:
<div class="gallery">
<?php if (isset($images) && count($images)):
foreach ($images as $image): ?>
<div class="thumbs">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" alt="">
</a>
</div>
<?php endforeach; else: ?>
<div class="blank_gallery">Please upload some pictures here</div>
<?php endif; ?>
</div>
<div class="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
How can this gallery page codes be used with the new post page page so that every post has a thumbnail .
I am learning php. Don't know know how to figure this out. Please help me.
Maybe i didn't understand your problem, but i can figure that you will need this plugin:
[http://jquery.malsup.com/form][1]
[Example at : http://jquery.malsup.com/form/progress.html][2]
This would help you to upload the pic first then edit/save the article, because your "gallery page" will only save and upload images and you need to link these 2 pages with this ajax image plugin.

Message: Undefined property: stdClass in codeigniter

I got this error:
A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$positionsNum
when i run.
My model:
public function load_job($id){
$Where = array('jobs.id' => $id);
$this->db->select('jobs.id, positionsNum, companyName, contract_type.titleEN as contractType');
$this->db->from('jobs');
$this->db->join('contract_type', 'jobs.contractTypeId = contract_type.id', 'left');
$this->db->where($Where);
$Result = $this->db->get();
return $Result->first_row() ;
}
my view:
<?php if(isset($job) && count($job) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($job->contractType)) echo $job->contractType; ?></div>
</div>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('num_vacancies'); ?> </div>
<div class="col-lg-6"><?php echo $job->positionsNum; ?></div>
</div>
<?php } ?>
my Controller's function:
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$job = $this->job_model->load_job($this->GetLang, $job_id);
$data['job'] = $job;
$this->load->view( 'front/singleJob', $data);
}
}
Also, i printed the returned object and it returns this:
stdClass Object(
[id] => 9
[positionsNum] => 2
[companyName] =>
[contractType] => type1
)
But, contractType won't be printed in page even if it is already has a value.
I can't figure out the problem. Any help?
The problem is here.
you have a function with one parameter.
public function load_job($id);
but in controller you are passing two values.
Try to pass correct values.
$job = $this->job_model->load_job($this->GetLang, $job_id);
it should be like this.
$job = $this->job_model->load_job( $job_id);
try like this
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$data['job'] = $this->job_model->load_job($this->GetLang, $job_id);
$this->load->view( 'front/singleJob', $data);
}
}
So, it is solved now :)
This problem happened twice in two files:
First is which stated in my question, and I had an error in another query at the same page.
Second problem was in the HTML file. My complete view was like this:
<?php if(isset($service) && count($service) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($service->contractType)) echo $service->contractType; ?></div>
</div>
<?php } ?>
<ul>
<?php foreach ($similarServices as $service) { ?> <<========= NOTICE: $service here is the same as the above condition
<li>
<div class="title"><?php echo $service->title; ?>
</div>
</li>
<?php }//end foreach similarServices ?>
</ul>
It appeared because i used the same variable name $service, so i changed it and it works now. Hope this helps you.

Categories