Jump to content

[SOLVED] Limiting Results of Query


gamerzfuse

Recommended Posts

I have a query

				//Make first query to database
				$query = "SELECT * FROM vehicles";
				$result = mysql_query($query)
					or die ("Couldn’t execute query.");

				//Set Variables
				$vehiclemodel = $vehicles->model->ViewValue;

				$query = "SELECT * FROM vehicles WHERE model='$vehiclemodel'";
				$result = mysql_query($query)
					or die ("Couldn't execute query.");

				// Display results in a table

				echo "<tr><td colspan='5'></td></tr>";
				while ($row = mysql_fetch_array($result))
				{
				extract($row);

				//Table Contents
				$show_price = number_format($price);
				echo "<tr class='dataright'>\n
				<td colspan='5' class='dataleft'><a href='view.php?stock=$stock'> $year $model</a></td>\n
				<td>$odometer km  </td>\n
				<td align='right'>$$show_price.00 </td>\n
				</tr>\n";
				echo "<tr><td colspan='3'></td></tr>\n";
				}

 

I want to limit the results to only display # results. (ie: 10 results)

Google searches for "limit query" etc haven't given me what I'm looking for.

Any hints, links, etc would be much appreciated.

Link to comment
Share on other sites

$query = "SELECT * FROM vehicles LIMIT 10";

 

Additionally, you can use the following to order by the somecolumn being a real column to select what area of first 10 you want.

 

$query = "SELECT * FROM vehicles ORDER BY somecolumn LIMIT 10 ";

 

Link to comment
Share on other sites

$query = "SELECT * FROM vehicles LIMIT 10";

 

Additionally, you can use the following to order by the somecolumn being a real column to select what area of first 10 you want.

 

$query = "SELECT * FROM vehicles ORDER BY somecolumn LIMIT 10 ";

 

Thanks loads guys, that was going to be my second question Stryves.

Thanks again!

Link to comment
Share on other sites

Depends on what you want to do with your application.

 

LIMIT 10 will only show you the first 10 results. But if you want to then show the next 10 results,

you have to provide it like so.

 

 

LIMIT 1,10 = first 10

LIMIT 11,20 = next 10

 

Or you can looking into pagination to provide the full user experience...

Link to comment
Share on other sites

Since you guys are so great at sorting, I'll throw another one out there:

 

I have a list that is sorted based on the column header that is clicked (ie: click Price, sorts by Price)

 

Is there anyway to have it secondarily sort?

I have 400 items in the list and it lists like:

Chevrolet    2008

Honda          2008

Chevrolet    2008

 

Know what I mean?

EDIT: (I want it to sort secondarily by Make or Model regardless, just to keep things looking tidy.)

Link to comment
Share on other sites

$query = "SELECT * FROM vehicles ORDER BY somecolumn DESC, somecolumn2 LIMIT 10 ";

 

If you want to go from highest to lowest, add DESC beside the column name, otherwise it's always ascending.

 

Just put a comma between the 2 column names.

Link to comment
Share on other sites

Just put a comma between the 2 column names.

 

Yeah.. but see, the function I'm using gets the SORT from the URL.

Meaning example.com/items.php?order=make&ordertype=ASC

 

Can I automatically have a second one the defaults to secondary regardless?

$sUrlParm = $this->UrlParm("type=" . mysql_real_escape_string($_GET['type']) . "&order=" . urlencode($fld->FldName) . "&ordertype=" . $fld->ReverseSort());

 

I'm not sure how I would enter commas into this variable?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.