Link to home
Start Free TrialLog in
Avatar of MsKrissy

asked on

can't figure out how to implement the LIMIT clause

I have a grid that works perfect, almost...
I would like to use the LIMIT clause to only display the top 5.  I have it ordering properly, but it shows all the records, not the top 5.  Can someone please help me get the LIMIT 0,5 to work properly?


      $id = $_SESSION['id']; 
//echo "$id";

require 'connections/';
require 'connections/';

$db = new EyeMySQLAdap('host', 'user', 'password', 'name');
$x = new EyeDataGrid($db);
$where = "id=$id";

// Set the query
$x->setQuery("Symbol,Full_Name,Industry,Affiliation,Current_Value,Movement,id", "portfolio", "", "$where");

// Hide ID Column

// Allows filters

// Change column type
$x->setColumnType('Symbol', EyeDataGrid::TYPE_HREF);
$x->setColumnType('Industry', EyeDataGrid::TYPE_IMAGE);


Open in new window

class EyeDataGrid
	private $results_per_page = 9;
	private $query; // SQL query
	private $filter = false; // Current filter
	private $limit = true; // Current limit
	private $_db, $result; // Database related
	private $select_fields = ''; // Field used to select
	private $select_where = ''; // Where clause
	private $select_table = ''; // Table to read

* Outputs the datagrid to the screen
	public function printTable()
		// Set the limit
		$this->setLimit(($this->page - 1) * $this->results_per_page, $this->results_per_page);

		$filter_query = '';
		if ($this->select_where)
			$filter_query .= "(" . $this->select_where . ")";

		if ($this->allow_filters and $this->filter)
			if (!strstr($this->filter['Value'], '%'))
				$filter_value = '%' . $this->filter['Value'] . '%';
				$filter_value = $this->filter['Value'];

			if ($this->select_where)
				$filter_query = $filter_query . " AND ";

			$filter_query .= "(`" . $this->filter['Column'] . "` LIKE '" . $filter_value . "')";
		if ($filter_query)
			$filter = 'WHERE ' . $filter_query;

		// ORDER
		if ($this->order)
			$order = "ORDER BY `" . $this->order['Column'] . "` " . $this->order['Order'];
			$order = '';

		// LIMIT
		if ($this->limit)
			$limit = "LIMIT " . $this->limit['Low'] . ", " . $this->limit['High'];
			$limit = '';

Open in new window

Avatar of kavik379

Try add this, maybe after the setQuery method:

$x-> setResultsPerPage(5);
Avatar of MsKrissy


okay, that actually returns 5 records per page, but I only want 5 records returned total.
Ok. Using the code as is, I don't think it is possible to do what you want. If you notice in the first line of code in the method printTable, the lower limit is set according to the page number, so it will always change, thus changing the limit.
I feel like the mother on Good Times when she first heard her husband James had died in a train accident.

Avatar of kavik379

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the help.
How abot adding this:

$this->limit['Low'] = 0;

before this:
            // LIMIT
            if ($this->limit)
                  $limit = "LIMIT " . $this->limit['Low'] . ", " . $this->limit['High'];
                  $limit = '';
At least try it.
Thanks Roads Roads,
I tried that, it still gives me all of the records, it just shows whatever value I give.
$this->limit['Low'] = 0;

The problem is there are over 1000 records and I only want to show the top 10, I fear this might slow down the site over time.  As an alternative, I am just hiding the footer which allows you to toggle through pages.

it sucks I know.
Well, I'm just a curious type of guy. But I suspect there IS a solution to that :P