Codeigniter, Displaying results from database - codeigniter

I am new to CI, I've been working on this for hours with no success, please help! I am trying to generate user profiles by retrieving information from a database and displaying it in the view (using the USERID). I am getting both an Undefined Variable Error and Trying to Get Property Out of Non-Object error. Here is my code:
Model:
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
return $query->result();
}
Controller:
public function profile()
{
$data['query'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $user);
$this->load->view('footer');
}
View:
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $data->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $data->occupation;?>
</div>
Thanks so much in advance, I feel like I have tried everything!

Hi as new to CI you will face some problems but later you will enjoy it here are some simple stuff you are doing wrong
Model
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
//this will return multiple rows or object of arrays
//return $query->result();
// you need to send only single row
return $query->row();
}
Controller
public function profile()
{
// in data array key name should be same which you will pass to view
$data['row'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
View
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $row->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $row->occupation;?>
</div>
for more information please read CI user guide you will understand so many stuff from there CI user guide

here change $user to $data
public function profile()
{
$data['result'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
then if your query returns only one row , you can use
return $query->row_array();
instead of
return $query->result();
in view you can now use
Name: <?php echo $result['name']; ?>
Location: <?php echo $result['location'];?>
Occupation: <?php echo $result['occupation'];?>

Related

Error: page requested not found codeigniter

Controller
public function index()
{
//load session library
$this->load->library('session');
if($this->session->userdata('user')){
// redirect('home');
$this->load->view('heropage');
}
else{
$this->load->view('login_page');
}
}
public function login(){
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$grade=$data[0]->grade;
$points=$data[0]->points;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->session->set_userdata('grade',$grade);
$this->session->set_userdata('pts',$points);
$this->getImg();
redirect('home');
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found'); }
}
View
<?php if(isset($_SESSION['success'])) :?>
<div class="alert alert-success"><?=$_SESSION['success'];?></div>
<?php endif; if(isset($_SESSION['error'])) :?>
<div class="alert alert-warning"><?=$_SESSION['error'];?></div>
<?php endif;?>
<!-- End alerts -->
<form action="<?php echo base_url();?>index.php/User/login" method="post" accept-charset="utf-8">
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="email" placeholder="Email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"name="password" placeholder="Password">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<button class="btn btn-sm btn-success" type="submit" align="center" name="login" class="submit">Log in</button>
</div>
</div>
</form>
model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
Upon trying to log in, I got the error page cant be found. I want it to go to the home page if the session is correct. here is the error message:
404 Page Not Found
The page you requested was not found.
How can I solve the error because I have also set as needed in the routes
I think your form action should be <?php echo base_url(); ?>user/login
Also in your model you're not checking for password anywhere.
You're also not returning anything if the email is not found or more than 1 results are found -
($query->num_rows() == 1)
Model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password))->result(); // you should use row() here to return only 1 row.
return $query; // you should check the uniqueness of email on registration, not here -- not allow duplicate email on registration
}
Controller
public function login(){
$email = $_POST['email']; // $this->input->post('email');
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if( !empty($data) ) // if no result found it'll be empty
{
// your code
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
See, if this helps you.

How do i retrieve images from folder which is linked to an item id?

The main issue is with the model, i cant figure out how to join the two tables so that i can display an item with its image which are stored in different locations.
model
$this->db->select('bs_items.id, bs_items.description, bs_items.title,
bs_items.touch_count,bs_items.price');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$this->db->from('bs_items');
Controller:
this->load->model('Popular_car');
$this->load->model('Recent_car');
$data['popular_cars']= $this->Popular_car->popular_car();
$data['recent_cars']=$this->Recent_car->get_listing();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('pages/home_recent', $data);
$this->load->view('templates/footer');
Please update your question with some code you have done , or database structure. i'm giving you the answer based on what i understood using your given code snippet.
Model
function get_items()
{
$this->db->select('*');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$query = $this->db->get();
return $query->result();
}
Controller
function get_allitems()
{
$this->load->model('model_name');
$data['items'] = $this->model_name->get_items();
$this->load->view('view_name', $data);
}
View
<?php foreach ($items as $row): ?>
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->touch_count;?></p>
<p><?php echo $row->price;?></p>
<p><?php echo $row->description;?></p>
<img src="<?php echo base_url().'path_of_image/'.$row->image_from_db; ?>
<?php endforeach; ?>
$myString = $this->db->get_where('profile_table' , array('id' => $edit ) )->row()->file_name_galery;
// $json11 = '{ "1":"s1.jpg", "2":"s2.jpg", "3":"s4.jpg", "4":"s4.jpg", "5":"s5.jpg" }';
$array = json_decode($myString);
if (empty($array)) {
echo '<p class="tx-danger"> NO IMAGE..</p>';
}
foreach($array as $key => $photo) { <div class="column_galery text-center zoom_img_app">
<a onclick="selected_photo(' echo $photo; ')" data-toggle="modal" data-target="#edit_image_mod" >
<img class="" src="http://yourapp.com/uploads/profile_gallery/ echo $photo; " style="width:100%"> <span class="tx-danger">DELETE</span> </a>
</div> }

Not displaying correct out put on view

I have two database tables called forum and forum_category.
In the forum table "General" and "Vegetables"
On the forum_categories there are two forum categories for General and one in Vegetables.
But for some reason my forum Vegetables is showing multiple categories should be only displaying one.
My question is how can I get the correct categories to display in
correct forum.
Note: It shows the correct results in the print_r image
Preview
View
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php foreach ($forums as $forum) {?>
<div class="panel panel-custom">
<div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
<div class="panel-body">
<?php foreach ($forum['categories'] as $category) {?>
<table class="table">
<tbody>
<tr>
<td><?php echo $category['category_name'];?></td>
</tr>
</tbody>
</table>
<?php }?>
</div>
</div><!-- Panel -->
<?php }?>
</div>
</div>
Model Functions
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
Printed Results
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories_results = $this->get_result_categories($result['forum_id']);
echo "<pre>";
print_r($categories_results);
echo "</pre>";
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
}
Think I have the solution now
Solution
Placing $categories = array();
above $categories_results did the trick
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories = array();
$categories_results = $this->get_result_categories($result['forum_id']);
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
Image

How I display title in categories only one title in one blogpost?

Hi all i'm a new of codigniter I create post and join two table I want to show categories title only one in my blog post .but it not folow me.it show many title categories in every post.
My Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
function index(){
$this->home();
}
function home(){
$this->load->view('header_view');
$this->load->view('slide_view');
$this->load->view('crumb_view');
$this->load->view('footer_view');
}
function menu(){
$this->load->model('tinjeat_md');
$data["menu_pro"] = $this->tinjeat_md->menu_get();
$this->load->view('header_view');
$this->load->view('crumb_view');
$this->load->view('menu_view',$data);
$this->load->view('footer_view');
}
}
<?php
class Tinjeat_md extends CI_Model{
function __construct(){
parent::__construct();
}
function menu_get(){
$this->db->select('*');
$this->db->from('menu','categories_menu');
$this->db->join('categories_menu','menu.cate_id=
categories_menu.id','left');
$query = $this->db->get();
return $query->result();
}
}
?>
View:
<div class="menu">
<?php foreach ($menu_pro as $value): ?>
<p class="title_cate">
<?php echo $value->cate_name_menu; ?>
</p>
<div class="menu_image">
<?php
echo img(array(
'src'=>'./images/'.$value->image,
'width'=>'220px',
'height'=>'150px'
));
?>
</div>
<div class="menu_description">
<h3><?php echo $value->title; ?></h3>
<p><?php echo $value->description; ?></p>
<p>Prices:<?php echo $value->prices; ?></p>
</div>
<?php endforeach; ?>
</div>
My database:
categories_menu
-id
-cate_menu_name
menu
-id_menu
-title
-image
-description
-prices
-cate_id
In your model.Try like this.
function menu_get(){
$this->db->select('*');
$this->db->from('menu');
$this->db-join('categories_menu','menu.cate_id=categories_menu.id','left');
$query = $this->db->get();
return $query->result();
}
for user guide
you can check your sql query by using $this->db->last_query().

Search is not working in codeigniter

i did search like this but it is not taking passed value in text box,can any one help me to solve this issue..thanks in advances........my model query is correct or any error is there......
if i enter in search term it is redirecting to search results page but it is showing blank results.......and it is saying invalid argument supplied for foreach()
if i print last_query() in that it is showing Like below.....
SELECT * FROM (`clients`) WHERE `clientName` LIKE '%%'
This is My Controller:
class Client extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("client_model");
$this->load->model("interview_model");
$this->load->model("candidate_model");
$this->load->model("requirement_model");
}
function search()
{
$search_term = $this->input->post('search');
$data['query'] = $this->client_model->search_client($search_term);
echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Search Results";
$this->layout->view("client/search",$data);
}
}
Model:
function search_client($search_term)
{
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
This is form in index.php(View):
<form action="<?php base_url();?>client/search">
<input type="text" name="search" id="search" style="width:180px" placeholder="Search here..." />
<input type="submit" value="Search" name="submit" class="btn btn-primary btn-sm" /></form>
This is search.php(view):
<?php foreach($query as $client){ ?>
<tr>
<td><?php echo $client->clientName;?></td>
<td><?php echo $client->clientSName; ?></td></td>
<td>
<?php } ?>
change this line:
<form method="POST" action="<?php base_url();?>client/search">
to this:
<form action="<?php echo base_url();?>client/search">
and try the following approch:
function search_client($search_term){
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
else
return false;
}

Resources