<?php
// example3.php
//
// using EZ Select to generate two similar queries
require("ez_select.php");
// real query, gets our results
$real_query = new ez_select();
// we want to select from "foo" and "bar" tables
$real_query->add_table("foo");
$real_query->add_table("bar");
// count query, gets the total results in the database
$count_query = $real_query;
$count_query->add_field("COUNT(*)");
// add the fields we want to select
$real_query->add_field("foo.id");
$real_query->add_field("foo.name");
$real_query->add_field("bar.id");
// where clauses to restrict our query
$real_query->add_where_clause("foo.id = bar.id");
$count_query->add_where_clause("foo.id = bar.id");
// the user doesn't want to see foobars
if( isset($dont_show_foobars) )
{
$real_query->add_where_clause("foo.name != 'foobar'");
$count_query->add_where_clause("foo.name != 'foobar'");
}
// how did they want to order the results?
switch( $order_by )
{
case 'foo.id':
$real_query->add_order_by("foo.id");
break;
case 'foo.name':
$real_query->add_order_by("foo.name");
break;
default:
$real_query->add_order_by("RAND()");
break;
}
// how many results to show?
if( $num_results )
{
$real_query->set_limit(0, $num_results);
}
// or do 25 by default
else
{
$real_query->set_limit(0, 25);
}
// Let's see our queries
$real_query->show();
$count_query->show();
// at this point you would run the query through mysql_query() or whatever
// $result = mysql_query( $real_query->make() );
// $count_result = mysql_query( $count_query->make() );
?>
|