I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.
Related
Please help!!
I am trying to search database to produce results for my search query but it does not output any result even when the searched term exist in the database
Here is my controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("profile_model");
$this->load->model("model_home");
}
public function index(){
$data = array();
$search_term = $this->input->post('search');
if($query = $this->model_home->car_search())
{
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
}
Here is my model
public function car_search($search_term='default')
{
$this->db->select('*');
$this->db->like('car_make',$search_term);
$this->db->or_like('car_model',$search_term);
$this->db->or_like('car_year',$search_term);
$this->db->or_like('registration_number',$search_term);
$this->db->or_like('engine_number',$search_term);
$this->db->or_like('chasis_number',$search_term);
$query = $this->db->get('cars');
return $query->result_array();
}
My search form
<?php
echo form_open('search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','submit');
?>
My search result page
<?php
if(isset($car)) :
foreach($car as $row) {
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif; ?>
Its always outputting "Not Working"
This code is a bit of a mess, if you tidy it up you will see the error
Your code without all the <?php and ?>
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
}
echo $row['car_make']
else :
echo '<h1> Not working </h1>';
endif;
?>
So you should see the } in the wrong place ending the foreach loop, quite easily now, so change it to
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif;
?>
As you are getting the Not Working message I have to assume there is a problem with your querying of the database.
Probably as you are not passing the search term into the car_search method call. So try passing the paramter you get from the POSTed data.
public function index(){
$data = array();
if($query = $this->model_home->car_search($this->input->post('search'))) {
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
You should also look at the default value used in the car_search method as the string 'default' will almost definitely cause the query to return no results.
Please help me. i can not use my dataTable properly. what I want to do is
select from table and use thewherefunction. but i cant do it properly.
here is my controller code
public function reporttable ()
{
$zz = array('empnumber' => $this->input->post('empnumber'));
//$this->db->order_by("surname", "asc");
$this->datatables->select('date_auto,particulars,earned_VL,aul_VL,balance_VL,aulx_VL,earned_SL,aul_SL,balance_SL,aulx_SL,date_leave,action_taken')
->from('tblreport')->where($zz);
echo $this->datatables->generate();
}
this is my supposed query:
select * from tblreport where empnumber = (the empnumber in my textbox.)
there, i get a value from a textbox to my view. but it didn't work. i know that is wrong. can you please help me with my problem? thank you.
<p align="center"> <?php echo $this->table->generate();?></p></div>
<?php foreach ($tblemployee as $row){?>
<input type="text" name="empnumber" readonly="readonly" value="<?php echo $row->empnumber;?>"/>
<input type="hidden" name="empnumber" value="<?php echo $row->empnumber;?>"/>
here is my view for guide. thank you.
as Simple you can use
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Try this :
To make it more simplier.
In model :
public function reporttable ($id){
$this->db->select('*');
$this->db->from('tblreport');
$this->db->where('empnumber', $id);
$query = $this->db->get();
return $query->return_array(); // use this if so want to return many query if not you can also use first_row('array')
}
In controller :
public function function_name (){
$data['variable_name'] = $this->model_name->reporttable($id); // change the model_name by your own model where your function reporttable is located and use the variable_name for your view,
$this->load->view('view_name' , $data); // load the view page you want to display.
}
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}
I am new to codeigniter and could be better at php. I have a news item being retrieved from the database table news, it has a foreign key of partner_id that is tied to the partners table. I want to get that value and use it to get the associated partner and display it's info. This is the latest of my attempts. I think I might be making it too hard. All relevant files are below. Thanks in advance.
In the model if the $id_partner is being assigned in the get_news function I don't think it is passing to the get_partner function.
news_model.php
public function get_news($slug_news = FALSE)
{
$this->load->helper('array');
if ($slug_news === FALSE)
{
$news_query = $this->db->get('news');
return $news_query->result_array();
}
$news_query = $this->db->get_where('news', array('slug' => $slug_news));
return $news_query->row_array();
global $id_partner;
$id_partner = element('partner_id', $news_query);
}
public function get_partner($id_partner = FALSE){
$partners_query = $this->db->get('partners');
$partners_query = $this->db->get_where('partners', array('id' => $id_partner));
return $partners_query->row_array();
}
The $slug_news in the view function doesn't apply to the partner part (obviously). but can I have another function?
controller, news.php
public function view($slug_news)
{
$data['news_item'] = $this->news_model->get_news($slug_news);
$partner_data['partner_listing'] = $this->news_model->get_partner($id_partner);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('news/news_partner', $partner_data);
$this->load->view('templates/sidebar');
$this->load->view('templates/footer');
}
view1, news/view.php
<?php
echo '<img src="/images/'.$news_item['thumb'].'" />';
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
echo $news_item['bus_name'];
?><br>
and view2, news/news_partner.php
<?php
echo '<h2>'.$partner_listing['bus_name'].'</h2>';
echo $partner_listing['address1'];
echo $partner_listing['address2'];
echo $partner_listing['city'];
echo $partner_listing['state'];
echo $partner_listing['phone'];
echo $partner_listing['email'];
?>
Your model is a little all over the place. To start, the two lines after the return are never called in get_news. I would just simplify the model and do a simple join:
public function get_news()
{
$this->db->select('*');
$this->db->from('news');
$q = $this->db->join('partners', 'news.partner_id = partners.id');
return $q->result();
}
Here is what I put with your code:
public function get_news($slug_news = FALSE)
{
if ($slug_news === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$this->db->select('*');
$this->db->from('news');
$query_news = $this->db->join('partners', 'news.partner_id = partners.id');
return $query_news ->result();
}
I only used the "view.php" view as well. Does $slug_news need to be in there somewhere? How does it know what line to get? Thanks for your help, I am having so much trouble.
It was hard to come up with a title. I am using CodeIgniter with models/views/controller. I have the following tables in my MySQL database that are relevant:
In my model I have the following function:
function get_shoptable() {
$this->db->from('productshop')->where('productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
In my controller I use the above function like
$data['bookshop'] = $this->Product_model->get_shoptable();
In my view I am foreaching $bookshop. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.
Try some like this:
function get_shoptable() {
$this->db->from('productshop')
->join('shop', 'productshop.shopId = shop.shopId')
->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
Model:
function get_products() {
$this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName');
$this->db->from('productshop');
$this->db->join('shop', 'productshop.shopId = shop.shopId');
$this->db->where('productshop.productId', $this->productId);
return $this->db->get()->result_array();
}
Controller:
function products() {
$data['products'] = $this->model_name->get_product();
$this->load->view('products', $data);
}
VIEW:
<?php foreach($products as $p): ?>
<h1><?php echo $p['productUrl']; ?></h1>
<h1><?php echo $p['shopName']; ?></h1>
<?php endforeach(); ?>
get an overlook to active class of codeigniter for details of functions
function get_shoptable()
{
$this->db->from('productshop')
$this->db->join('shop', 'productshop.shopId = shop.shopId')
$this->db->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}