我已经在Codeigniter中创建了一个搜索功能,并且它正常工作。搜索功能显示我想要的内容,但我希望结果链接到另一个页面。我是Codeigniter的新手。有人可以帮帮我吗?
这是我的模特
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;
}
}
控制器
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);
}
和我的观点
<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"><a href="#"><?php echo ($r->title); ?></a></td>
<td align="center"><a href="#"><?php echo ($r->author); ?></a></td>
</tr>
<?php endforeach;?>
<?php endif;?>
</table>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
我只想让结果转到另一页。
答案 0 :(得分:0)
如果您希望每个结果都有一个链接将您重定向到另一个pge,则必须将新网页的网址放在锚标记的href
中,就像任何
HTML链接。我意识到你有一张桌子,我会做的是创建一个带有&#34的另一列;了解更多&#34;链接。
所以在你的情况下,改变这个:
<?php foreach($results as $r):?>
<tr>
<td align="center"><a href="#"><?php echo ($r->title); ?></a></td>
<td align="center"><a href="#"><?php echo ($r->author); ?></a></td>
</tr>
<?php endforeach;?>
对此:
<?php foreach($results as $r):?>
<tr>
<td align="center"><?= $r->title; ?></td>
<td align="center"><= $r->author; ?></td>
<td align="center"><a href="<?= base_url()?>controller_name/controller_method/$r->id">Learn More</a></td>
</tr>
<?php endforeach;?>
锚现在看起来像这样:
<?= base_url()?>controller_name/controller_method/$r->id
您希望将其更改为您选择的方法名称,可能是这样的:
<?= base_url()?>books/about/$r->id
^ ^ ^
| | |
| | parameter
| method
controller
它将重定向到特定的控制器方法,将您的图书的ID作为参数传递。您现在的工作是构建该方法,以便您可以通过id检索该特定书籍并将其解析为视图。