PHPBuilder - SQL Theory & How-To Page 4



RSS Twitter
Articles Databases

SQL Theory & How-To - Page 4

by: Joe Stump
|
January 3, 2001


<?php

if(isset($submit))
{
  
// Our initial SELECT query - we will build on to it.
  
$sql "SELECT * FROM link_information AS I, link_categories AS C";

  
$where[] = ' C.categoryID=I.categoryID ';

  
// If it's not "Any" then add on a where claus
  
if($f['category' != 'all')
  {
    
$where[] = ' I.categoryID='.$f['category'];
  }

  
// If "hits" is a valid number then add on a where clause
  
if(isset($f['hits_compare']) && strlen($f['hits_limit']))
  {
    
$where[] = ' I.hits '.$f['hits_compare'].' '.$f['hits_limit'];
  }

  
$sql .= ' WHERE '.implode(' ',$where);

  
// decide our limit values
  
if(strlen($f['record_limit']) && strlen($f['record_start']))
  {
    
$limit = ($f['record_limit'] - $f['record_start']);
    if(
$f['record_start'] >= && $limit >= 0)
    {
      
$sql .= ' LIMIT '.$f['record_start'].','.$limit;
    }
  }

  echo 
$sql;
}

?>
That should build intelligent queries for you to then use at your discretion. You can view the above in action at www.miester.org/appendix/dynamic_queries.php and the code at www.miester.org/appendix/dynamic_queries.phps.
You can also add "ORDER BY" clauses with ease and the ability to choose which columns to perform those "ORDER BY" cluases on. In the grand scheme of things users are wanting not only more customization but better search results then they go to your webpages. Giving them the power of a SQL console in their browser will bring them one step close to their goal.
Finally here are some links of interest:

« Previous Page
1
|
2
|
3
|
4

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Joe Stump

Comment:



Comment:

(Maximum characters: 1200). You have characters left.