我在页面上有一个搜索表单,用于搜索数据库中表格的记录。我想展示每个查询给我们多少结果。所有这些都是用codeigniter编写的。
该页面上的所有代码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$data['title'] = "Welcome | randomsite";
$data['html'] = "";
if($this->input->post()) {
$uq = $this->security->xss_clean($this->input->post('query'));
if(trim($uq) != "") {
$searchBy = $this->security->xss_clean($this->input->post('searchBy'));
$searchByJSON = json_encode(array(1 => "Email", 2 => "Username", 3 => "IP", 4 => "FullName", 5 => "Phone"), JSON_FORCE_OBJECT);
if(isset(json_decode($searchByJSON)->$searchBy)) {
$start = microtime(true);
$this->db->from('Databases');
$rs = $this->db->get()->result();
$end = microtime(true);
$data['html'] .= "Search completed in: " . ($end - $start) . " seconds.<p></p>";
foreach($rs as $row) {
$this->db->distinct();
$this->db->select('Username, Password, Email, FullName, IP, Phone, salt');
$this->db->from('Accounts');
$this->db->where(json_decode($searchByJSON)->$searchBy, $uq);
$this->db->where('DatabaseID', $row->ID);
$query = $this->db->get();
if($query->num_rows() > 0) {
if($searchBy == 5 && $query->row()->Phone == 0) {
break;
}
$resultsHTML = "";
foreach($query->result() as $qr) {
$resultsHTML .= "<div class='card-block-results' style='table-layout:fixed; word-wrap:break-word;'><table class='table table-hover' style='font-size:13px;'>";
foreach($qr as $key => $value) {
if(!empty($value)) {
if($key == "FullName") {
$key = "Full Name";
}
$resultsHTML .= "<tr><td>" . $key . ": " . $value . "</td></tr>";
}
}
$resultsHTML .= "</table></div>";
}
$data['html'] .= $row->Website . " has: <b style='color:lime;'>" . $query->num_rows() . "</b> result(s) found. This data was hacked on approximately " . $row->Date . ". <button class='btn btn-success btn-sm' style='margin-bottom:5px;' id='button" . $row->ID . "'>view results</button><div id='results" . $row->ID . "' style='display:none;'><div class='card card-outline-primary' style='margin-bottom:10px;text-align:left;margin-top:5px;'><div class='card-header card-primary'>Results</div>" . $resultsHTML . "</div></div><script type='text/javascript'>$('#button" . $row->ID . "').click(function() { $(this).hide(); $('#results" . $row->ID . "').show(); });</script><br>";
}
}
if($data['html'] == "Search completed in: " . ($end - $start) . " seconds.<p></p>") {
$data['html'] .= "No results found!<p></p>Are you searching in the right fields? Searching for an email in the phone number field will not work.<br>Make sure first and last names are correct. Example: Mike Tyson";
}
$data['html'] .= "<br><br><br>";
$this->db->from('Lookups');
$query = $this->db->get();
$new_lookup = $query->row()->Number + 1;
$qdata = array(
"Number" => $new_lookup
);
$this->db->update('Lookups', $qdata);
}
} else {
$data['html'] = '<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>Please enter something in your query before searching!</div>';
}
}
$this->load->view('welcome', $data);
}
}
&#13;
那么我怎样才能在每次有人搜索时添加它?喜欢&#39;查询有x个结果。&#39;
像这样:
我在很多不同的网站上搜索过这个问题,但我找不到任何针对我具体问题的内容。
答案 0 :(得分:1)
您已在代码中使用过它,
$query->num_rows()
将为您提供从查询中获得的行数。
答案 1 :(得分:0)
我认为这是滥用CI这样的框架 - 我给你一些关于这个框架的见解 - 如果你愿意学习,研究这段代码是因为 你几乎不使用他的任何内置功能
在我看来,你要将你的代码重组到一定程度,以至于你甚至不能认出你之前的尝试;)
我会试着给你预感 - 但为了理解 - 你必须使用这段代码
在你的模特文件夹中放入以下型号
数据库模型
class Database_model extends CI_Model
{
private $objCollection;
public function __construct()
{
$this->objCollection = new Database_Collection();
}
public function getCollection()
{
$this->benchmark->mark('code_start');
$this->db->from('Databases');
$this->objCollection->append($this->db->get()->result());
$this->benchmark->mark('code_end');
$this->objCollection->queryTime = $this->benchmark->elapsed_time('code_start', 'code_end');
return $this->objCollection;
}
}
class Database_Collection extends ArrayObject
{
public $queryTime = 0;
}
帐户模型
class Accounts_model extends CI_Model
{
private $objCollection;
public function __construct()
{
$this->objCollection = new Accounts_Collection();
}
public function getCollection()
{
$this->benchmark->mark('code_start');
$this->db
->distinct()
->select('Username, Password, Email, FullName, IP, Phone, salt')
->from('Accounts');
$this->objCollection->append($this->db->get()->result());
$this->benchmark->mark('code_end');
$this->objCollection->queryTime = $this->benchmark->elapsed_time('code_start', 'code_end');
return $this->objCollection;
}
}
class Accounts_Collection extends ArrayObject
{
public $queryTime = 0;
}
搜索模型
class Search_model extends CI_Model
{
private $strSearchQuery = "";
public function getSearchObject()
{
if ($this->isValidSearch())
{
$this->load->model("Database_Model");
$this->load->model("Accounts_Model");
$objSearch = new Search;
$objDBCollection = $this->Database_Model->getCollection();
foreach($objDBCollection AS $objItem)
{
$this->db
->where("DatabaseID", $objItem->ID)
->where($this->input->post("searchBy"), $this->strSearchQuery);
$objItem->collection = $this->Accounts_Model->getCollection();
}
$objSearch->addResultCollection($objDBCollection);
return $objSearch;
}
return false;
}
public function isValidSearch()
{
$strSearchQuery = $this->security->xss_clean($this->input->post('query'));
$this->strSearchQuery = trim($strSearchQuery);
if (empty($this->strSearchQuery)) return false;
$arrValidSearchCriteria = array("Email", "Username", "IP", "FullName", "Phone");
return (in_array($this->input->post("searchBy"),arrValidSearchCriteria));
}
}
class Search
{
public $queryTime = 0;
private $objCollection;
public function addResultCollection(Database_Collection $objCollection)
{
$this->objCollection = $objCollection;
}
public function getRenderedTime()
{
if ($this->queryTime == 0)
{
$this->queryTime += $this->objCollection->queryTime;
foreach($this->objCollection AS $objItem)
{
if (isset($objItem->collection)
{
$this->queryTime += $objItem->collection->queryTime;
}
}
}
return $this->queryTime;
}
public function getCountSearchResults()
{
if (!$this->countSearchResults)
{
$this->countSearchResults = 0;
foreach($this->objCollection AS $objItem)
{
if (isset($objItem->collection)
{
$this->countSearchResults += $objItem->collection->count();
}
}
}
return $this->countSearchResults;
}
}
您的控制器
class Welcome extends CI_Controller
{
public function index()
{
$this->load->model("Search_model");
$data['title'] = "Welcome | randomsite";
if ($this->Search_model->isValidSearch()
{
$objSearch = $this->Search_model->getSearchObject();
$arrViewData = array("objSearch" => $objSearch);
$this->load->view("list-search", $arrViewData);
}
}
}
pS:也许有一些拼写错误,因为我刚把它写下来