Solved

can't figure out how to implement the LIMIT clause

Posted on 2011-03-09
10
803 Views
Last Modified: 2012-06-27
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?

<?php
session_start();

if(!isset($_SESSION['id'])){
	header("location:login.php");
      }else{
      $id = $_SESSION['id']; 
}
//echo "$id";

require 'connections/class.eyemysqladap.inc.php';
require 'connections/class.eyedatagridPORTFOLIO.inc.php';

$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
$x->hideColumn('id');

// Allows filters
$x->allowFilters();

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

?>

Open in new window



class.eyedatagridPORTFOLIO.inc.php
<?php

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
		$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'] . '%';
			else
				$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'];
		else
			$order = '';

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

Open in new window

0
Comment
Question by:MsKrissy
  • 4
  • 3
  • 3
10 Comments
 
LVL 1

Expert Comment

by:kavik379
ID: 35086574
Try add this, maybe after the setQuery method:

$x-> setResultsPerPage(5);
0
 

Author Comment

by:MsKrissy
ID: 35086636
okay, that actually returns 5 records per page, but I only want 5 records returned total.
0
 
LVL 1

Expert Comment

by:kavik379
ID: 35086829
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:MsKrissy
ID: 35086897
I feel like the mother on Good Times when she first heard her husband James had died in a train accident.

DAMN!, DAMN!, DAMN!
0
 
LVL 1

Accepted Solution

by:
kavik379 earned 500 total points
ID: 35086929
Sorry. Maybe you could ask the author for help on it.
0
 

Author Closing Comment

by:MsKrissy
ID: 35086969
Thank you for the help.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35087132
How abot adding this:

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

before this:
            // LIMIT
            if ($this->limit)
                  $limit = "LIMIT " . $this->limit['Low'] . ", " . $this->limit['High'];
            else
                  $limit = '';
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35087279
At least try it.
0
 

Author Comment

by:MsKrissy
ID: 35087541
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.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35087567
Well, I'm just a curious type of guy. But I suspect there IS a solution to that :P
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move wordpress site 3 41
Crazy text will not be removed no matter what I try! 13 30
Dynamic varibles 5 34
How do uses indexes to maximize MySQL Searches 14 33
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question