显示查询在codeigniter中有多少个结果

qv7cva1a  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(263)

我的页面上有一个搜索表单,可以从数据库中的表中搜索记录。我想显示每个查询给我们多少个结果。所有这些都是用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">&times;</a>Please enter something in your query before searching!</div>';

			}
		}
		$this->load->view('welcome', $data);
	}

}

所以我怎么才能在每次有人搜索时都加上这个呢?比如“查询有x个结果”
这样地:

我在许多不同的网站上搜索了这个问题,但我找不到任何关于我的具体问题。

u5rb5r59

u5rb5r591#

我认为这是滥用像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:可能是因为我刚写了一些错字

jum4pzuy

jum4pzuy2#

你已经在你的代码里用过了,

$query->num_rows()

将为您提供从查询中获得的行数。

相关问题