limit mysql output by letter


#1

Hi I am building a drinks database/search site.

At present I have 26 tables 1 table for each letter like drinksa, drinkb, drinkc…ect which represent a, b, c…ect

I have realised this methed it not good practice as I can’t search all 26 tables in one go.

What I would like to do now is put all the drink a to z in one table called drinks.

I would then be able to search that 1 table, this is fine I can do that without a problem.

But I am haing some difficulty getting my head around the front end what I mean is on the front end of the site on the drinks page there are links to each section like a, b ,c, d, e,…x, y, z.

But if all the data is in one table then all the drinks a to z would be output I know I can limit how many to output but I was wondering is it possible to limit by starting letter

For example when a user clicks “A” then all the drink starting with “A” are output but not any other drinks starting with a diffrent letter.

This would be done by usin an sql statment I belive by using limit but am not sure if its possble to limit by letter.

I have been looking at a lot of foums and so far I have not come across anything that may be of help so I though I’d ask

If anyone would point me in the rihgt direction or could expmain if this is possible or a better way of doing this I would be very greatful

Thanks

Dave


#2

You should be able to find your way using LIKE.

SELECT * FROM drinks WHERE drinkname LIKE ‘A%’ LIMIT 30

This SQL query would output the first 30 rows of table drinks, where the name of the drink starts with letter A (or equals A). The % sign means ‘0 or more wildcards’ IIRC. http://www.mysql.com should be able to give you all possible special characters for the LIKE clause.


#3

Thanks for that but am getting this error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE 'A%' LIMIT 0, 5' at line 1

I tested your example of a page that already exists and already has limits set so I left the limit off from your example.

I was going to put a drink starting with A into table drinksb just to test it before restructering my tables.

Can you see whats wrong?

mysql_select_db($database_drinks, $drinks);
$query_drinks = "SELECT * FROM drinksb LIKE 'A%'";
$query_limit_drinks = sprintf("%s LIMIT %d, %d", $query_drinks, $startRow_drinks, $maxRows_drinks);
$drinks = mysql_query($query_limit_drinks, $drinks) or die(mysql_error());
$row_drinks = mysql_fetch_assoc($drinks);

Here the full page code

<?php require_once('Connections/drinks.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_drinks = 5;
$pageNum_drinks = 0;
if (isset($_GET['pageNum_drinks'])) {
  $pageNum_drinks = $_GET['pageNum_drinks'];
}
$startRow_drinks = $pageNum_drinks * $maxRows_drinks;

mysql_select_db($database_drinks, $drinks);
$query_drinks = "SELECT * FROM drinksb LIKE 'A%'";
$query_limit_drinks = sprintf("%s LIMIT %d, %d", $query_drinks, $startRow_drinks, $maxRows_drinks);
$drinks = mysql_query($query_limit_drinks, $drinks) or die(mysql_error());
$row_drinks = mysql_fetch_assoc($drinks);

if (isset($_GET['totalRows_drinks'])) {
  $totalRows_drinks = $_GET['totalRows_drinks'];
} else {
  $all_drinks = mysql_query($query_drinks);
  $totalRows_drinks = mysql_num_rows($all_drinks);
}
$totalPages_drinks = ceil($totalRows_drinks/$maxRows_drinks)-1;

$queryString_drinks = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_drinks") == false && 
        stristr($param, "totalRows_drinks") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_drinks = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_drinks = sprintf("&totalRows_drinks=%d%s", $totalRows_drinks, $queryString_drinks);
?>
<html>
<head>
<style type="text/css">
<!--
body,td {
	margin-left: 10px;
	margin-top: 10px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #D4E0FC;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:9px;
}
-->
</style>
</head>
<body>
<!-- contents can be a maximum of 509px -->
<?php do { ?>
<table width="100%" cellspacing="0" cellpadding="04">
  <tr>
    <td><b><?php echo $row_drinks['drinks_name']; ?></b></td>
  </tr>
  <tr>
    <td><?php echo $row_drinks['drinks_ing']; ?></td>
  </tr>
  <tr>
    <td><i><?php echo $row_drinks['drinks_inst']; ?></i></td>
  </tr>
</table>
<?php } while ($row_drinks = mysql_fetch_assoc($drinks)); ?>
<!-- contents can be a maximum of 509px -->
<table border="0" width="50%" align="left">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_drinks > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_drinks=%d%s", $currentPage, 0, $queryString_drinks); ?>"><img src="First.gif" border=0></a>
        <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_drinks > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_drinks=%d%s", $currentPage, max(0, $pageNum_drinks - 1), $queryString_drinks); ?>"><img src="Previous.gif" border=0></a>
        <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_drinks < $totalPages_drinks) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_drinks=%d%s", $currentPage, min($totalPages_drinks, $pageNum_drinks + 1), $queryString_drinks); ?>"><img src="Next.gif" border=0></a>
        <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_drinks < $totalPages_drinks) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_drinks=%d%s", $currentPage, $totalPages_drinks, $queryString_drinks); ?>"><img src="Last.gif" border=0></a>
        <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($drinks);
?>

Thanks


#4

You forgot the ‘WHERE’ part :wink: