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.
Related
i have uploaded a image and related content for it in the database, now im trying to fetch it from their and display it on my webpage, but im getting some errors while fetching it
, so please can any one help me where im going wrong? i don know the concept correctly but just how much i know i have implemented it please help me
this is my Home.php(controller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Contact_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
public function Latest_news()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
if ($this->form_validation->run()==TRUE)
{
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->latest_news($data);
//Redirecting to success page
redirect(site_url('Home/Latest_news'));
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
}
else
{
$this->load->view('Latest_news');
}
}
public function dispdata_latest_news()
{
$result['data']=$this->Contact_model->displayrecords_latest_news();
$this->load->view('display_records',$result);
}
?>
Contact_model(model)
<?php
class Contact_model extends CI_Model
{
function latest_news($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
//Display
function displayrecords_latest_news()
{
//Displaying Records
$query=$this->db->query("select first_content from latest_news");
return $query->result();
}
}
?>
index.php(view)
<div class="lates">
<div class="container">
<div class="text-center">
<h2>Latest News</h2>
</div>
<div class="col-md-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<img src="<?php echo base_url('images/4.jpg');?>" class="img-responsive" />
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($query as $r): ?>
<tr><?php echo $r->content; ?>
</tr>
<?php endforeach; ?>
</table>
</div>
Hope this will help you :
Your controller dispdata_latest_news should be like this :
public function dispdata_latest_news()
{
$rows = $this->Contact_model->displayrecords_latest_news();
$result = array('data' => $rows);
/* OR use it like this
$result = ['data' => $rows];
*/
$this->load->view('display_records',$result);
}
Your model displayrecords_latest_news should be like this :
function displayrecords_latest_news()
{
$query = $this->db->get("latest_news");
if ($query->num_rows() > 0)
{
return $query->result();
}
}
Your view should be like this :
<?php
if (! empty($data))
{
foreach($data as $r){ ?>
<tr><?php echo $r->first_content; ?></tr>
<?php }
}
else { ?>
<tr>no record found</tr>
<?php } ?>
I have check your all files and codes and i think you have wrong in your view file.
You have use $r->content. I think where you have get the data in model file the column name is different i.e. first_content.
you have to use like this.
<?php foreach($query as $r): ?>
<tr> <?php echo $r->first_content; ?>
</tr>
<?php endforeach; ?>
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']; ?>" />
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; ?>
i've create a custom admin page for my opencart panel. If I try to run route index.php?route=report/remarketing, system will log out instead of display page!
here's code:
controller
class ControllerReportRemarketing extends Controller {
public function index()
{
$template="report/remarketing.tpl";
$this->load->language('report/remarketing');
$this->load->model('report/remarketing');
$this->template = ''.$template.'';
$this->data['record'] = 'test';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
}
model (have no methods)
class ModelReportRemarketing extends Model {
}
view
<?php echo $header; ?>
<div id="content">
<?php echo $record; ?>
</div>
<?php echo $footer; ?>
Also i've setting access permission to user in User Groups
I think you are using the old version opencart's code, here is the example for opencart version 2.0.2.0:
First, creat 'remarketing.php' under admin\language\english\report, add below content:
<?php
// Heading
$_['heading_title'] = 'Marketing Report';
Second, modify 'remarketing.php' under admin\controller\report as below
<?php
class ControllerReportRemarketing extends Controller{
public function index(){
// displayed text import
$this->load->language('report/remarketing');
// get header and footer
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
// the model, need not to import
// $this->load->model('report/remarketing');
// testing
$data['record'] = 'test';
$this->response->setOutput($this->load->view("report/remarketing.tpl", $data));
}
}
?>
Last, modify 'remarketing.tpl' under admin\view\template\report
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid"><?php echo $record; ?></div>
</div>
<?php echo $footer; ?>
i am still learning the codeigniter framework and just completed the news application tutorial, which lets you create a news item with a title and some text for the story. how can i get the user to be able to upload an image with the news item, and display each image when viewing the corresponding news item.
ps. sorry if i made any mistakes, this is my first question
create.php
<h2>Create a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input style="margin-left: 18%; margin-right: 20%;" type="submit" name="submit" value="PUBLISH" />
</form>
news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$this->db->order_by("id", "desc");
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
}
news.php -Controller
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index(){
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('templates/rightpane', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('templates/rightpane', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('templates/rightpane', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];`
index.php
<?php foreach ($news as $news_item): ?>
<div style="margin-left: 18%; margin-right: 20%;">
<h2 ><?php echo $news_item['title'] ?></h2>
<div id="main" >
<?php echo $news_item['text'] ?>
</div>
<p >View article</p>
</div>
<?php endforeach ?>
Take a look at the upload class from the docs :
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
hint : you'll save the file to server and then save the URL in the database, then you can display the URL inside an IMG tag.
The solution is:
img src="" class="img-responsive"