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.
Related
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 have created a search function but it only works by entering in one key word. How can I let someone enter in a phrase and it will display results? at the minute im having to use _ and telling customers to use _ instead of space.
<div class="post">
<h1 class="title">Search Results for: <?php echo $_POST['keywords']; ?></h1>
<table style="width: 100%">
<tr>
<td></td>
</tr>
<?php
$searchterm = $_POST['keywords'];
include 'db.inc.php';
$query = mysql_query("SELECT * FROM search WHERE keywords like '%$searchterm%'");
while($row = mysql_fetch_array($query))
{
?>
<tr>
<td style="font-size:x-large"><a href="./<?php echo $row['link']; ?>">
<?php echo $row['title']; ?> </a></td>
</tr>
<tr>
<td><?php echo $row['description']; ?></td>
</tr>
<tr>
<tr>
</tr>
<?php
}
?>
</table>
</div>
<div class="post">
</div>
</div>
Finally my pagination worked.Still there are some problems.In every page its showing only one data and edit delete functionality not working.I tried changing almost everything.Any guidance will be great.
*This is my controller*
function view($page=0){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view";
$config["total_rows"] = $this->emp_expenses_model->getTotalStudentCount();
$config["per_page"] = 5;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getStudent($config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
This is the code in my model
function getTotalStudentCount() {
return $this->db->count_all("emp_expenses");
}
function getStudent($limit, $start) {
$this->db->limit($limit, $start);
$qry= $this->db->get("emp_expenses");
return $qry->result();
}
and this is the view
<table cellspacing="0" cellpadding="2" border="0" id="tbl" style="width:100%">
<tr style="background-color:#045c97">
<td class="heading">Expenses ID</td>
<td class="heading">Employee ID</td>
<td class="heading">Drop Down</td>
<td class="heading">Mode OF Payment</td>
<td class="heading">Amount</td>
<td class="heading">Edit</td>
<td class="heading">Delete</td>
</tr>
<?php
foreach($results as $m)
//var_dump($results);die('asd');
?>
<tr style="text-align:center;">
<tr>
<td><?php echo $m->expenses_id ?></td>
<td><?php echo $m->id ?></td>
<td><?php echo $m->dropdown ?></td>
<td><?php echo $m->modeofpayment ?></td>
<td><?php echo $m->amount ?></td>
<td>Edit</td>
<td>
<?php
echo anchor('view_expenses/delete_expenses/'.$m, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>
</tr>
<?php echo $this->pagination->create_links()?>
</table>
Firstly, probably not related to the main problem but your class="" attribute on the edit button has no space between the start of the attribute and the closing quotes of the href.
The main problem seems to be that your trying to echo $m, on this line:
<?php echo site_url('view_expenses/edit_expenses/'.$m) ?>
$m is an object (containing multiple variables of information), your getting an error because your trying to treat it like a string.
Instead, you need to access one of these variables from within the object, just like you do further up in the code. I am guessing, you want the id, which would be $m->id.
Give this a go instead:
<?php echo site_url('view_expenses/edit_expenses/'.$m->id) ?>
The same goes for your delete button.
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);
}
}
Evening all, I have the form, which is populated from a sql database.
<table class="sortable" border="2" cellspacing="2" cellpadding="2">
<tr>
<th> Serial Number </th>
<th> Date </th>
<th> Time </th>
<th> Color </th>
<th> Design </th>
<th> Result </th>
</tr>
<?php
$i = 0;
while ($i < $num)
{
$serial = mysql_result($results, $i, 'serial_number');
$date = mysql_result($results, $i, 'date');
$time = mysql_result($results, $i, 'time');
$airport = mysql_result($results, $i, 'color');
$terminal = mysql_result($results, $i, 'design');
$result = mysql_result($results, $i, 'result');
?>
<tr>
<td> <?php echo $serial; ?> </a></td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
<?php
$i++;
}
?>
What I would like to do is have each row of the table clickable. When each row is clicked, one cell of data from that row (the first cell) is sent via (POST) to the next page. Can a form be integrated into each tr??
You specify jQuery in your tags, so I assume you can use that.
// when any row is clicked
$('table').on('click', 'tr', function () {
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = nextPage + '?data=' + value;
});
Where nextPage is the URL of the page you want to redirect to.
The short answer: yes, a form can be integrated into each tr.
The long answer - use just as you did before:
<tr>
<td>
<form method="post" target="details.php">
<input type="submit" name="more" value="<?php echo $serial; ?>"
</form>
<?php echo $serial; ?>
</td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
But GET is easier, make a series of links that go details.php?number=123, or whichever:
<td>
<a href="details.php?number=<?php echo $serial; ?>"
<?php echo $serial; ?>
</a>
</td>
Although get can use a form, the data send is not user-customised, so the form data can be generated to use like a link.
Try with that code in echo when create your table:
<td><?php echo(''.$serial.'');?></td>
For each data that you have!
OTHER SOLUTION WITHOUT ACTION
<td><?php echo(''.$serial.'');?></td>
my_page.php - where to send the data
?[variable_name] - where is stored the data