Model
//returns an array having different arrays inside it with keys id ,name and thumbnail taken from the database
function m_get_thumbnails($category){
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
if($query->num_rows()>0)
{
$count =0;
$arr = array();
foreach ($query->result() as $row)
{
$arr[$count++] = array(
'id'=>$row->id,
'name'=>$row->name,
'thumbnail'=>$row->thumbnail
);
}
return $arr;
}
}
function in controller
After that i place the result in an array and pass the array to my view.
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food.php',$data);
}
View
<script>alert(<?php $cafe[1]; ?>);</script>
//this alert is coming blank.......
Controller:
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food',$data);
}
In Model
function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
$result = $query->result_array();
return $result;
}
In your View
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php
if(!empty($cafe)) {
foreach($cafe as $c){
?>
<tr>
<td> <?php echo $c['id']?> </td>
<td> <?php echo $c['name']?> </td>
<td><img src="<?php echo $c['thumbnail']?>" /></td>
</tr>
<?php } } else { ?>
<tr><td colspan="3">No record found.</td></tr>
<?php } ?>
</table>
Simplify your function in Model
function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food')->result_array();
return $query;
}
In your View
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php for($i=0;$i<count($cafe);$i++){?>
<tr>
<td> <?php echo $cafe[$i]['id']?> </td>
<td> <?php echo $cafe[$i]['name']?> </td>
<td> <?php echo $cafe[$i]['thumbnail']?> </td> <!-- Use <img> if you have a url -->
</tr>
<?php }?>
</table>
Related
I am creating a tabulation web app and displaying the score of candidates.
I am trying to place these data in their correct value, but I'm failing to do so, I have already used distinct and group by but still, the problem persists.
Here is an image of the problem:
What I would like to achieve is this data:
-
Swimwear (20%)
Gownwear (40%)
Rose Ann
30
40
Elle
50
60
Code:
Model:
public function get_score_candidates(){
$this->db->distinct();
$this->db->select('categories.description,candidates.fullname,categories.percent');
$this->db->from('tabulations');
$this->db->join('categories','categories.cat_id = tabulations.cat_id');
$this->db->join('candidates','candidates.cand_id = tabulations.cand_id');
$query = $this->db->get();
return $query->result_array();
}
public function get_scores(){
$this->db->distinct();
$this->db->select('score');
$this->db->from('tabulations');
$query = $this->db->get();
return $query->result_array();
}
View: Here, wha I did is reconsrtuct a table so I can achieve the column and row needed.
<thead class="text-center">
<tr>
<th></th>
<?php foreach($scores as $score): ?>
<th scope="col"><?php echo $score['description'].' ('.$score['percent'].'%)' ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody class="text-center">
<?php foreach($scores as $score): ?>
<tr>
<td><?php echo $score['fullname'] ?></td>
<?php foreach($cand_scores as $cand_score): ?>
<td><input type="number" name="score[]" class="form-control" value="<?php echo $cand_score['score'] ?>"></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
//query in controller
$this->db->select('cat_id,description,percent');
$this->db->from('categories');
$query = $this->db->get();
$data['categories'] = $query->result_array();
$cat_select = '';
foreach($data['categories'] as $cat){
$cat_id = $cat["cat_id"];
$cat_desc = $cat["description"];
$cat_select .= 'sum( CASE WHEN categories.cat_id =
'.$cat_id.' THEN
tabulations.score END ) AS '.$cat_desc.',';
}
$this->db->select($cat_select .' fullname');
$this->db->from('candidates');
$this->db->join('tabulations','tabulations.cand_id =
candidates.cand_id');
$this->db->join('categories','categories.cat_id =
tabulations.cat_id');
$this->db->group_by('fullname');
$query = $this->db->get();
$data['cand_scores'] = $query->result_array();
/*****************************************************/
//view
if(!empty($cand_scores)) { ?>
<table>
<thead class="text-center">
<tr>
<th></th>
<?php foreach($categories as $score): ?>
<th scope="col"><?php echo $score['description'].'
('.$score['percent'].'%)' ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody class="text-center">
<?php
foreach($cand_scores as $emp_group){
echo '<tr>';
echo '<td>'. $emp_group['fullname'].'</td>';
foreach($categories as $score){ ?>
<td>
<input type="number" name="score[]" class="form-control"
value="<?php echo $emp_group[$score['description']]?>">
</td>
<?php }
echo '</tr>';
} ?>
</tbody>
</table>
<?php }
I have already created an search function in Codeigniter and its working properly. The search function shows exactly what I want but I want the result to be link to another page. I am new to Codeigniter. Can somebody please help me?
Here is my Model
public function search($searchterm)
{
$sql = "SELECT title as title, author as author
FROM `edubooks`
WHERE title LIKE '$searchterm' OR author LIKE '$searchterm'";
$q = $this->db->query($sql);
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
return 0;
}
}
/******************Handler for search******************/
public function searchterm_handler($searchterm)
{
if($searchterm)
{
$this->session->set_userdata('searchterm', $searchterm);
return $searchterm;
}
elseif($this->session->userdata('searchterm'))
{
$searchterm = $this->session->userdata('searchterm');
return $searchterm;
}
else
{
$searchterm ="";
return $searchterm;
}
}
Controller
public function search()
{
$searchterm = $this->search_model->searchterm_handler($this->input->get_post('searchterm', TRUE));
$data['results'] = $this->search_model->search($searchterm);
$data['searchterm'] = $searchterm;
$this->load->view('search',$data);
}
and my view
<h2>Library Search System</h2>
<div id="formborder">
<form action ="search" method="post" id="searchform" name="searchform">
<b> Name of book : </b><input type="text" name="searchterm" id="searchterm" value="<?=$searchterm?>" />
<input type="submit" value="search" id="submit" />
</form>
</div>
<div id="tablebody">
<table id="table" width="100%" = border>
<?php if($results == false):?>
<tr>
<td>No records found.</td>
</tr>
<?php else:?>
<tr>
<th>Title</th>
<th>Author</th>
</tr>
<?php foreach($results as $r):?>
<tr>
<td align="center"><?php echo ($r->title); ?></td>
<td align="center"><?php echo ($r->author); ?></td>
</tr>
<?php endforeach;?>
<?php endif;?>
</table>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
I just want the result to go another page.
if you want a link per each result to redirect you to another pge, you have to place that new page's URL in the href of your anchor tag, just like any
HTML link. I realize you have a table, what I would do is create another column with a "learn more" link.
So in your case, change this:
<?php foreach($results as $r):?>
<tr>
<td align="center"><?php echo ($r->title); ?></td>
<td align="center"><?php echo ($r->author); ?></td>
</tr>
<?php endforeach;?>
To this:
<?php foreach($results as $r):?>
<tr>
<td align="center"><?= $r->title; ?></td>
<td align="center"><= $r->author; ?></td>
<td align="center">Learn More</td>
</tr>
<?php endforeach;?>
the anchor now looks like this:
<?= base_url()?>controller_name/controller_method/$r->id
which you would want to change to the method name of your choice, perhaps something like this:
<?= base_url()?>books/about/$r->id
^ ^ ^
| | |
| | parameter
| method
controller
it will redirect to that specific controller method passing the id of your book as a parameter. your job now is to build that method so that you can retrieve that specific book by id and parse it to a view.
Here's the issue : CodeIgniter loads data from the controller but not from the view
When I go the the source of the page and click the form action link its load data in the source code but not in the html view.
--view
<table class="table hovered">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Type</th>
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin/manage_customer') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->cus_id; ?></td>
<td class="right">Admin</td>
<td class="right"><?php echo $row->cus_name; ?></td>
<td class="right"><?php echo $row->cus_email; ?></td>
<td class="right"><?php echo $row->cus_phone; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
---controller
function inbox()
{
$data = array();
if($query = $this->mod_contactus->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_messages',$data);
}
---Model
<?php
class Mod_contactus extends CI_Model //Users (model name) share all the class and functions CodeIgniter models have
{
function get_records()
{
$query = $this->db->get('tbl_contactus');
return $query->result();
}
function add_record($data)
{
$this->db->insert('tbl_contactus', $data);
return;
}
function update_record()
{
}
}
i would go with hussain in this. first check if the necessary data is returned by the model or not. Only then you should check the html structure. But even if there is a problem with structure, something should be displayed. So, check the flow of data to the view first.
I'm having some trouble displaying the results of my query using a foreach loop in codeigniter. Heres my controller:
function viewall()
{
$this->load->model('all');
$data['query'] = $this->all->viewall();
$this->load->view('all', $data);
}
Entire Model File:
<?php
class All extends CI_Model
{
function insert_into_db()
{
$data = array('Error' => $this->input->post('f1'),
'Solution' => $this->input->post('f2')
);
$this->db->insert('Errors', $data);
}
function viewall()
{
$query = $this->db->select("Error, Solution")->from("Errors")->get();
return $query->result();
}
}
My view (where I think the problem is)
<table class="table table-striped">
<thead>
<tr>
<td><h3>Error</h3></td>
<td><h3>Solution</h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($query->result_array() as $entry) ?>
<tr>
<td><?php echo $entry->Error; ?></td>
<td><?php echo $entry->Solution;?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
Controller:
function viewall()
{
$this->load->model('all');
$data['results'] = $this->all->viewall();
$this->load->view('all', $data);
}
Model:
function viewall()
{
$query = $this->db->select("Error, Solution")->from("Errors")->get();
return $query->result();
}
View:
<table class="table table-striped">
<thead>
<tr>
<td><h3>Error</h3></td>
<td><h3>Solution</h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($results as $entry): ?>
<tr>
<td><?php echo $entry->Error; ?></td>
<td><?php echo $entry->Solution;?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I have this code block in html for inputting the search.
<?php echo form_open('bookstore/_searchbook'); ?>
<table>
<tr>
<td>
<label for="searchid">Search:</label>
</td>
<td>
<input type="text" size="15" name="searchid" />
</td>
</tr>
<tr>
<td>
<label for="searchtype">Type:</label>
</td>
<td>
<select>
<option value="book_id">Id</option>
<option value="book_author">Author</option>
<option value="book_name">Title</option>
</select>
</td>
</tr>
<tr>
<input type="submit" value="Search" />
</tr>
</table>
<?php echo form_close(); ?>
And i have this on my controller,
public function booksearch()
{
if($_POST['submit'])
{
$col= $this->input->post('searchtype', TRUE);
$val= $this->input->post('searchval', TRUE);
$data['book'] = $this->books_model->showbook($col,$val);
$this->load->view('showbooks',$data);
}
}
and this would be my model
public function showbook($col, $searchid)
{
$this->db->select()->from('books')->where(array($col=>$searchid));
$query=$this->db->get();
return $query->first_row('array');
}
Further info on my view,
I have this to print the results of my search.
<table cellpadding='5'>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>
<?php
if(isset($books)) : foreach($books as $book) :
?>
<tr>
<td><?php echo $book['book_id'] ?></td>
<td><?php echo $book['book_name'] ?></td>
<td><?php echo $book['book_author'] ?></td>
<td><?php echo $book['book_year'] ?></td>
<td><?php echo $book['book_isbn'] ?></td>
</tr>
<?php
endforeach;
?>
<?php
else :
?>
<h5>No records</h5>
<?php
endif;
?>
</table>
It returns nothing so all i see is no records. Someone direct me to the thing i did wrong.
Your form's action (view) is _searchbook while your controller function is booksearch
Your submit input needs a name attribute
in the controller function you have $data['book'] while in your view (in the foreach loop) you reference that variable as $books
Model: you need to select at least something; e.g. $this->db->select('*')->from('books')->where(array($col=>$searchid)); instead of $this->db->select()
Model: I think you need return $query->result_array(); instead of $query->first_row('array')
Because your submit button has not got any name attribute. Instead of if($_POST['submit']) write $this->input->post(null)
Try adding global $_POST; to the top of your function. In php all variables used(declaired) in a function will be private unless you tell it otherwise by adding it to the global statement in the function.
public function booksearch()
{
global $_POST;
if($_POST['submit'])
{
$col= $this->input->post('searchtype', TRUE);
$val= $this->input->post('searchval', TRUE);
return $data['book'] = $this->books_model->showbook($col,$val);
}
}