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; ?>
Related
I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach ($userdetail_list as $row) {
?>
<tr>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach ($userdetail_list as $row) { ?>
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load this view file I do see the echoeod email but not the username? And there is also no link to the users profile page..
Here is my controller file (User.php):
<?php
class User extends CI_Controller {
public function index() {
$this->load->view('profile', $data);
}
public function __construct() {
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id) {
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user', $data);
}
public function profile() {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
?>
And here is my model (User_model.php) :
<?php
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function getUserInfo($user_id) {
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if ($this->db->affected_rows() > 0) {
$row = $q->row();
return $row;
} else {
error_log('no user found getUserInfo(' . $user_id . ')');
return false;
}
}
?>
Database information:
I have 2 tables:
users:
user_id
voornaam
achternaam
email
password
(products:
product_id
product_name
product_description)
I hope someone can help me,
Thanks!
You are concatenating user name in anchor tag herf separate it and write between <a> and </a>
change your code as below:
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
You can try this solution for your problem.
Please change your view file.
details.php
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
and please changes your controller function:
User.php
<?php
public function profiel_user($user_id) {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$data['user_id'] = $user_id; // you can used user id for getting data
$this->load->view('profile');
}
?>
Thanks
I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load the view page I do not see the username and no link to the users profile.
Here is my controller (User.php):
<?php
class User extends CI_Controller
{
public function index()
{
$this->load->view('profile', $data);
}
public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}
public function profile()
{
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
full model file (User_model.php)
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
}
?>
Problably because of this line:
<?=$row['username']; ?>
Maybe your server does not support short hand tags?
try this:
<?php echo $row['username']; ?>
You also missed a semicolon here at the end:
echo base_url() . 'User/profiel_user?id='.$row['user_id']
and also here:
<a href="<?php echo base_url() ?>/Cadeauaanvragen">
You also use table row and table data tags without a surrounding table tag. Or remove all tr and td tags.
See: How do browsers analyze <tr> <td> without <table>?
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.
I get repeatedly the same error. dropdown data is not fetched from the form. I have my code here
this is my controller:ProductController
{<?php
class ProductController extends Controller
{
public function actionCreate()
{
$model=new CreateForm;
// collect user input data
if(isset($_POST['CreateForm']))
{
$model->attributes=$_POST['CreateForm'];
$model->setAttributes($_POST['CreateForm']);
// validate user input and redirect to the previous page if valid
if($model->validate())
{
$product=new Product;
$product->save();
}
else {
echo "Hi";
}
}
// display the login form
$this->render('create',array('model'=>$model));
}
}
?>}
My model:CreateForm
{<?php
class CreateForm extends CFormModel
{
public $product_name;
public $category_name;
public $description;
public function rules()
{
return array(
array('product_name, category_name, description', 'required'),
array('product_name', 'unique', 'className' => 'Product', 'attributeName' => 'product_name', 'message'=>'This product name is already in use'),
);
}
public function attributeLabels()
{
return array(
'product_name'=>'PRODUCT NAME',
'category_name'=>'CATEGORY',
'description'=>'DESCRIPTION'
);
}
}
?>
}
Category
{
<?php
class Category extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'category';
}
public function attributeLabels()
{
return array(
'category_id'=>'CATEGORY ID',
'category_name'=>'CATEGORY NAME',
);
}
}
?>}
MY view:create.php
{<?php
$this->pageTitle=Yii::app()->name . ' - Create';
$this->breadcrumbs=array(
'Create',
);
?>
<h1>CREATE</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'create-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'product_name'); ?>
<?php echo $form->textField($model,'product_name'); ?>
<?php echo $form->error($model,'product_name'); ?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$list = CHtml::listData($records, 'category_id', 'category_name');
echo CHtml::DropDownList('category_name', null, $list, array('prompt'=>'select '));
echo $form->error($model,'category_name');
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?></br>
<?php echo $form->textArea($model,'description',array('style' => 'height:100px;width:500px;','maxlength'=>500)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('CREATE'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
}
if I enter all the fields, again it gives an error stating "PLS enter the category"
What you have done is called massive assignment
$model->attributes=$_POST['CreateForm'];
this would not assign all the values and is not safe in handling datas also
the datas would be saved only if they are in safe in the model
you will redeclare the posted data as
$model->attributes=$_POST['CreateForm'];
$model->dropdownname=$_POST['CreateForm']['dropdownname'];
Hope this will help you
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/#hh2
I think you change your code of dropdown.
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$list = CHtml::listData($records, 'category_id', 'category_name');
echo CHtml::DropDownList('category_name', null, $list, array('prompt'=>'select '));
echo $form->error($model,'category_name');
?>
To
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$result = array();
foreach($records as $p) {
$result[p->category_id] = p->category_name ;
}
echo CHtml::activeDropDownList($model, 'category_name', $result);
echo $form->error($model,'category_name');
?>
Now you need to check, either your category id or name is coming or not.
Then you debug your controller file, as in given below.
public function actionCreate()
{
$model=new CreateForm;
// collect user input data
if(isset($_POST['CreateForm']))
{
$model->attributes=$_POST['CreateForm'];
echo $model->category_id ;
exit ; // Finish program here, and drop down will be printed.
$model->setAttributes($_POST['CreateForm']);
// validate user input and redirect to the previous page if valid
if($model->validate())
{
$product=new Product;
$product->save();
}
else {
echo "Hi";
}
} // display the login form
$this->render('create',array('model'=>$model));
}
Hope it help you.
Thanks
It is the first time for me to use codeIgniter, I tried to build a simple DB driven application.
First the model:
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news()
{
$query = $this->db->get('news');
return $query->result_array();
}
}
second the controller
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('news/index', $data);
$this->load->view('templates/footer');
}
The view "index":
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
When I run the code it displays the header and the footer, but no database record is displayed.
If I put a print statement in the view outside the foreach it works, but not inside the foreach.
I use if($news) to find out if the query retrieved any result, but there is no result retrieved.
How can I know why there is no result retrieved from the DB?
You need to call another foreach inside your foreach. example:
foreach ($result as $row) {
foreach ($row as $key => $val) {
if ($key === "reservation_info") {
$data['decoded'] = json_decode($val);
} else {
$data[$key] = $val;
}
}
}
and i would use the '$val' values in the view. just by calling them as variables, like so
//in the view
echo $decoded
echo $firtname
... and so on
to check if new is empty or not, just
print_r($news);
or you can modify your view :
<?php if(!empty($new)): ?>
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
<?php else: ?>
Empty news
<?php endif; ?>