How to display all records from database in codeigniter? - codeigniter

I want to display all records from database to my view page. Having a table customers, how can I display all records of this table?
Here is my customer_model.php file..
function all_customss()
{
$query=$this->db->query("select * from customers");
return $query->result();
}
Here are my controller.php file...
function viewpage()
{
$result['data'] = $this->customer_model->all_customss();
$this->load->view('models_view', $result);
}
And here is the link which will display all records after click...
<div class="col-md-12 col-sm-12 text-center"><a class="btn btn-default hvr-bounce-to-right" href="<?php echo site_url('/viewpage/');?>" role="button"><?php echo $this->lang->line('see_more'); ?></a>
and here is my view file...page_view.php
tr><td><?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
?></td></tr>

You must put the name of your contoller before the name of your function like bellow:
<?php echo site_url('name_of_controoler/viewpage/');?>

function all_customss()
{
$query=$this->db->query("select * from customers");
return $query;
}
tr><td><?php
$i=1;
foreach($data->result () as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
I have modified your code. You should retrieve an array from a model. But it in your case you have already extracted the result in the model. And take a look in the view. I have modified your foreach parameter

Related

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> }

codeigniter how can i display data in view from this model

This is request_report function in my report_model how can code tabular data in my view passed by controller
function request_report($from, $to, $member_filter )
{
$this->db->select('requests.request_id,items.item_type,items.quantity,items.item_unit,items.item_name, requests.item_id, requests.quantity_requested, requests.quantity_delivered, requests.purpose');
$this->db->from('requests', 'items');
$this->db->join('items', 'items.item_id = requests.item_id');
$this->db->where('created_time >=', $from);
$this->db->where('created_time <=', $to);
if ($member_filter != 'null') $this->db->where('created_by', $member_filter);
$query = $this->db->get();
if ($query->num_rows() > 0 ) {
echo '<table id="requests" class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Items</th>
<th>Quantity Requested</th>
<th>Quantity Available</th>
<th>Quantity Delivered</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>';
foreach ($query->result() as $row) {
echo '<tr>';
echo '<td>';
echo $row->request_id;
echo '</td>';
echo '<td>';
echo $this->get_item_type_name($row->item_type);
echo '</td>';
echo '<td>';
echo $row->item_name;
echo '</td>';
echo '<td>';
echo $row->quantity . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_requested . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_delivered . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->purpose;
echo '</td>';
echo '</tr>';
}
echo '</tbody>
</table>';
}
else{
return '<h4 style="text-align: center; padding: 37px;"> No Matching Requests Found </h4>';
}
}
and this is my view
<div class="col-sm-12">
<?php echo $this->report_model->request_report($from, $to, $member_filter)>
</div>
my question is how can pass data from model to view through controller, not just calling model directly in view, and i need the tabular data in model to be in view, am looking for any help thanks!!
my question is how can pass data from model to view through
controller, not just calling model directly in view, and i need the
tabular data in model to be in view, am looking for any help thanks!!
In your controller say method is index()
<?php
class Site extends CI_Controller{
function index()
{
// this you can also put in __construct
// if you want to share same model across other methods
$this->load->model('report_model');
// And call your model function like below
$data = $this->report_model->request_report('2015-12-10', '2016-03-10', 'some_user');
// load view
$this->load->view('bootstrap_view',array('model_output' => $data));
}
}
and in your view, say bootstrap_view.php
<div class="col-sm-12">
<?php echo $model_output;>
</div>
Also note you can make use of table class in codeigniter, where you can set template of your interest, for more details follow below link
https://www.codeigniter.com/user_guide/libraries/table.html

Codeigniter after insert success message show in view

I am new in Codeigniter and I need to show success and error message after insert data's in database.
How can I show the message in the view page?
This is my coding:
Model
function addnewproducts($data)
{
if($data['product_name']!="" && $data['product_qty']!="" && $data['product_price']!="" && $data['date']!="")
{
$res=$this->db->insert('product_list',$data);
return $this->db->insert_id();
}
else
{
return false;
}
}
Controller
function addnewproduct()
{
$this->load->model('products');
$data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
$data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
$data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
$data['datetime']=date('d-m-Y');
$res = $this->products->addnewproducts($data);
if($res==true)
{
$data['success'] = 'Successful';
$this->load->view('addproduct',$data);
}
}
View
<p><?php echo $success; ?></p>
There are many ways but below is which i recommend:
Set temp session in controller on success or error:
$res = $this->products->addnewproducts($data);
if($res==true)
{
$this->session->set_flashdata('success', "SUCCESS_MESSAGE_HERE");
}else{
$this->session->set_flashdata('error', "ERROR_MESSAGE_HERE");
}
In View you can display flashdata as below:
echo $this->session->flashdata('success');
or
echo $this->session->flashdata('error');
Source : Codeigniter official website https://codeigniter.com/userguide3/libraries/sessions.html
I appreciate that you got your answer but I think flash data is bit old now, as we can use bootstrap to alert if any error and that looks good to on web page.
In controller
$res = $this->products->addnewproducts($data);
if($res==true)
{
$this->session->set_flashdata('true', 'write_the_message_you_want');
}
else
{
$this->session->set_flashdata('err', "write_the_message_you_want");
}
In View
<?php
if($this->session->flashdata('true')){
?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('true'); ?>
<?php
} else if($this->session->flashdata('err')){
?>
<div class = "alert alert-success">
<?php echo $this->session->flashdata('err'); ?>
</div>
<?php } ?>
Controller:
function addnewproduct()
{
$this->load->model('products');
$data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
$data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
$data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
$data['datetime']=date('d-m-Y');
if($this->products->addnewproducts($data));
{
$this->session->set_flashdata('Successfully','Product is Successfully Inserted');
}
else
{
$this->session->set_flashdata('Successfully','Failed To
inserted Product');
}
// redirect page were u want to show this massage.
redirect('Controller/Fucntion_name','refresh');
}// close function
view :
On Redirect Page write This code top of Form
<?php if($responce = $this->session->flashdata('Successfully')): ?>
<div class="box-header">
<div class="col-lg-6">
<div class="alert alert-success"><?php echo $responce;?></div>
</div>
</div>
<?php endif;?>

submit multiple inputs within a forloop in codeigniter

My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}

how make to form search in CodeIgniter using two fields

I want to make a search form in Codeigniter using two fields.
But I'm confused about making the controller and model.
View:
echo form_open("home/pencarian","class='form-inline navbar-search'");
<input id="srchFld" class="srchTxt" name="keyword" type="text" value=" echo $this->session->userdata("keyword");" />
<select class="srchTxt" name="kategori">
<option>All</option>
foreach ($kat as $kategori){
$id = $kategori['id'];
$nama_kategori = $kategori['nama_kategori'];
$created_at = $kategori['created_at'];
<option value=" echo $id; ?>"><?php echo $nama_kategori; </option>
}
</select>
echo form_submit('action', 'Cari', "class='btn btn-primary'");
echo form_close();
How do I make the controller and model?
Model example_model.php
public function search($search_txt, $search_category) {
$this->load->database();
$sql = "select * from search_table where search_field = ? and search_category = ?;";
$query = $this->db->query($sql, array($search_txt, $search_category));
return $query->result();
}
Controller home
public function pencarian() {
$this->load->model('example_model', 'm');
$search_txt = $this->input->post('srchFld');
$search_category = $this->input->post('kategori');
$data['search_result'] = $this->m->search($search_txt, $search_category);
$this->load->view('example_view', $data);
}
View example_view
<?php foreach($search_result as $r): ?>
<div><?php echo $r->field1 . ' ' . $r->field2; ?></div>
<?php endforeach; ?>

Resources