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>?
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; ?>
I'm making a website in CodeIgniter and I'm trying to make a menu bar with all the categories, and when you click on a certain category it must show all the products that are in that category but its not really working.
The categories are showing on the category side menu bar, but when I click on a certain category link its going to the right view page but instead of the product information I'm getting this error:
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type stdClass as array
Filename: views/category.php
Line Number: 41
Backtrace:
Line 41 in category.php is this line:
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
So I'm guessing it can't echo product detail information?
Here is my Allecadeaus controller to echo products:
<?php
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('Product_model');
$this->load->model('allecadeaus_model');
$data['products'] = $this->allecadeaus_model->get_products();
$this->load->view('allecadeaus', $data);
}
}
My Allecadeaus_model model file which I use to echo products:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_products()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
category.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My whole allecadeaus.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category['name']; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My Product.php controller file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
var $data = array();
public function index()
{
//Laad kado uploaden view
$this->load->view('product_form', $this->data);
}
public function details($product_id)
{
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'details';
$this->load->view('details',$data);
}
//categorie product function
public function category($id)
{
$data['title'] = 'Category';
$data['page'] = 'Product/category';
$data['category'] = $this->Category_model->findByCategory($id);
$data['products'] = $this->Product_model->findByCategory($id);
$this->Category_model->findByCategory($id);
$this->load->view('category', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('Product_model');
$this->load->helper(array('form', 'url'));
}
My Product_model file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function getdata()
{
$this->db->select('users.user_id,users.email,users.voornaam,products.product_id,products.category_id,products.product_naam');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function findAll(){
return $this->db->get('products')->result();
}
public function findByCategory($id){
$this->db->where('id', $id);
return $this->db->get('categories')->result();
}
}
?>
My Category_model file:
<?php
class Category_model extends CI_Model {
public function findAll(){
$this->db->get('categories')->result();
}
public function findByCategory($id)
{
$this->db->where('id', $id);
$this->db->get('categories')->result();
return $this->db->get('categories')->row();
}
}
?>
My db_helper.php
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
Some database information:
Table 1: 5 columns: products
-product_id
-product_naam
-product_beschrijving
-user_id
-category_id
table 2: categories: 2 columns:
1.id
2.name
You can try this solution for your problem:
Whole allecadeaus.php view file:
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
is your database already have the filed category_id, is the name correct? can you show screen from your categories and products table?
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 am fetching the data from the database.But I am getting error.Here I need to display product name and image in the page.But I am getting the error undefined variable 'name' in the page. Please check my code give your valuable suggestions,Thank you.
My controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
my model
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->select('*');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
my view
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
Model:
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
}
?>
view:
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
Change in model.like this.use row() which returns result in object format having matched row.so that you can access columns using arrow operator.
And what about your $where variable.set it.
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
inside the foreach loop, remember $row var is a object
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row->field;
}
I am creating a website with Codeigniter 3 HMVC style for a client and he wants have a function on this page where the logged user has the choice of picking their own news. For example if a user chooses Juventus then he will recieve all the news added where Juventus is named. Basically like a add to basket or cart thing and I have never done something like this so here is what I have created:
Controller
class Usernews extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->model('model_usernews');
$this->load->module('template');
}
function search(){
$data['pageTitle'] = "User news";
$search_name = $this->input->post('band');
$data['bands'] = $this->model_usernews->get_clubs($search_name);
$data['module'] = "usernews";
$data['view_file'] = "usernews";
$this->template->user_news($data);
}
}
Model
class Model_usernews extends CI_Model {
function get_table(){
$table = "bands";
return $table;
}
function get_clubs($keyword) {
$table = $this->get_table();
$this->db->like('band_name', $keyword, 'both');
$this->db->order_by('band_name');
$query=$this->db->get($table);
return $query;
}
and part of the View
<div class="row">
<div class="col-lg-8 ">
<!-- USER BAND LIST -->
<!-- LIST OF ARTICLES -->
</div>
<div class="col-lg-4">
<div class="row">
<?php $attributes = array('id' => 'list_of_bands');
echo form_open('usernews/search',$attributes); ?>
<input type="text" class="form-control" name="band"
placeholder="Search">
<button type="submit" class="btn btn-default btn-lg" >OK</button>
<?php echo form_close(); ?>
<?php $num_rows = $listing->num_rows();
if($num_rows > 0){ ?>
<ul id="the_bands" class="list-unstyled">
<li> <?php foreach ($listing->result() as $row){?>
<li><?php echo $row->band_name; ?>" ><button id="add_band"
class="btn btn-default btn-sm" >Add to list</button></li>
<?php }
} else{
echo "<p>No bands with that name</p>";
} ?>
</div>
</div>
I can search for the bands in the database but now I am clueless.
In your view use $bands instead of $listing.