is there better way of doing it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

is there better way of doing it?

Post by rami »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i have seen in most of site like this
[url=http://imageshack.us][img]http://img167.imageshack.us/img167/3229/image1xc1.jpg[/img][/url]
i have this code to work for this

Code: Select all

<?php 
 $cb = @$_GET['cb'] ;//property type
 $var = @$_GET['cid'] ;//price value if price is not all
 $cb1 = @$_GET['cb1'] ;//area 0=all or else means not all
 $cb2 = @$_GET['cb2'] ;//price returns 0 or < or > or = 
  $cb3 = @$_GET['cb3'] ;//property type 0 or 1=sale or 2=hire
  $trimmed = trim($var); //trim whitespace from the stored variable

require_once ('../../dbconnect.php');
 if ($cb1==0)//if area is any
 {
 		if ($cb2==0)//price is all
 			{
				if ($cb3==0)//type is any
 					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where properrty_type=$cb order by postedon desc";
					}
					else if($cb3==1) //type is not all but sale
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"sale\" order by postedon desc";
					}
					else //type is hire
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"hire\" order by postedon desc";
					}
				}
				else //price not all,ie < > =
		{
		 if ($trimmed == "")
  			{
 			 echo "<p>Please enter a search...</p>";
  				exit;
  				}
 				 if (!isset($var))
				  {
				  echo "<p>We dont seem to have a search parameter!</p>";
				  exit;
				  }
if ($cb3==0)//type is any
 					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where properrty_type=$cb and  price $cb2 $trimmed order by postedon desc";
					}
					else if($cb3==1) //type is not all but sale
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"sale\" and  price $cb2 $trimmed order by postedon desc";
					}
					else //type is hire
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"hire\" and  price $cb2 $trimmed order by postedon desc";
					}
							
							}//end of if price is < > =
		
		}//end of if area==0
		else //area is not any
		 
		 
		 {
 		 if ($cb1==0)//if area is any
 {
 		if ($cb2==0)//price is all
 			{
				if ($cb3==0)//type is any
 					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where properrty_type=$cb order by postedon desc";
					}
					else if($cb3==1) //type is not all but sale
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"sale\" order by postedon desc";
					}
					else //type is hire
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"hire\" order by postedon desc";
					}
				}
				else //price not all,ie < > =
		{
		 if ($trimmed == "")
  			{
 			 echo "<p>Please enter a search...</p>";
  				exit;
  				}
 				 if (!isset($var))
				  {
				  echo "<p>We dont seem to have a search parameter!</p>";
				  exit;
				  }
if ($cb3==0)//type is any
 					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where properrty_type=$cb and  price $cb2 $trimmed order by postedon desc";
					}
					else if($cb3==1) //type is not all but sale
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"sale\" and  price $cb2 $trimmed order by postedon desc";
					}
					else //type is hire
					{
 					$query = "SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where property_type=$cb and  property_for like \"hire\" and  price $cb2 $trimmed order by postedon desc";
					}
							
							}//end of if price is < > =
		
		}//end of else area==0

echo "<b><center><h2>Following users Found having name similar to<font color=red> <i>$trimmed</i></font></h2></center></b>";
echo "<center><i>click on telephone number for full details</i></center>";


echo '<table border="1" width="100%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="15%" bgcolor=\"cyan\"><b>Property id</b></td>
<td align="left" width="20%" bgcolor=\"cyan\"><b>Property for</b></td>
<td align="left" width="10%" bgcolor=\"cyan\"><b>District</b></td>
<td align="left" width="5%" bgcolor=\"cyan\"><b>Area</b></td>
<td align="left" width="18%" bgcolor=\"cyan\"><b>Price</b></td>
<td align="left" width="25%" bgcolor=\"cyan\"><b>Views</b></td>
<td align="left" width="7%" bgcolor=\"cyan\"><b>Posted on</b></td>
</tr>';
$result = mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) 
{	
if (mysql_num_rows($result) == 0) 
{
echo "<p>No Result meeting this criterea!</p>";
  exit();
  }
  else
  {
	echo "	<tr>
	<td align=\"left\"><a href=\"distotaltel.php?mid={$row['property_id']}\">{$row['property_id']}</a></td>

<td align=\"left\">{$row['property_for']}</td>
<td align=\"left\">{$row['pdistrict']}</td>
<td align=\"left\">{$row['property_area']}</td>
<td align=\"left\">{$row['price']}</td>
<td align=\"left\">{$row['views']}</td>
<td align=\"left\">{$row['postedon']}</td>

	</tr>\n";	
} 
}
mysql_close(); 
 ?>
though it gets the work done
but i myself couldnot agree with number of select and length of if and else
i think there is better way to do this ..
any suggestion
i made a decision tree and coded come out like this
as select are in if else i dont think it will put much pressure in database but any way too long..

any better way
if i now add one another thing it will be too complicated...

i myself found that rather than in that greater than ,less than ..i can put value


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I tend to build a single SQL string based on the form parameters .. so I would do something like:

Code: Select all

$sql  = "select * from table";
if (isset($type)) { $whereFields[]  = "type = ".$type; }
if (isset($price)) { $whereFields[]  = "price <= ".$price; }
if (isset($area)) { $whereFields[]  = "area = ".$area; }

if (is_array($whereFields)) {
  $sql .= " ".implode(" and ", $whereFields);
}
You end up with one (or more) if statement per form field, but it has the advantage of not being nested, and it's easy to add another.[/syntax]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Use isset() and drop error suppression ('@'). Also, take onion's advice and build a single string for your SQL based on the params instead of an entire string for each if.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

php:

Code: Select all

$sql  = "select * from table"; 
if (isset($type)) { $whereFields[]  = "type = ".$type; } 
if (isset($price)) { $whereFields[]  = "price <= ".$price; } 
if (isset($area)) { $whereFields 
that code some how doesnt work
i didnt got whole idea
i think it doesnt consider if "all" is selected

i tried that
the all problem starts with that
all field if some body chooses "all" option i have to make another query and if all is not choosen then i have make query including that feld
if i have 4 such slection constraints...then...................it beomes impossible to manage
how to manage that all thing
is there any working expample
please help
if i dont keep that all field and just make query based on the field in database the there will be only one slect statement but i feel that not giving "all" in select will be unprofessional

is it good from database load factor?
there any not much cactive rows ,just some thousands
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rami wrote:i think it doesnt consider if "all" is selected
You're right. It doesn't. I just posted the basic structure to get you started. You'll have to think about how to handle each parameter yourself. switch() is a useful function for handling enumerated form fields.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

then thats not of my work
and all that complicacy is bcoz of that "all" other wise it will be done in single statement

any way aren't there any way of doing it
i really need it
thanks
switch wont help either switch and if else will be almost same
i have do pagination as well in this page so if my use my currrent styke there will be
current number of if x 2
so i am worried
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rami wrote:and all that complicacy is bcoz of that "all" other wise it will be done in single statement
The 'all' option really isn't the problem you seem to think it is. All you need to do is write some code to handle it. Something along the lines of:

Code: Select all

if (isset($price)) {
    switch ($price) {
        case "1": $where[] = "price <= 10"; break;
        case "2": $where[] = "price > 10 and price <= 20"; break;
        case "3": $where[] = "price > 20 and price <= 30"; break;
        case "4": $where[] = "price > 30 and price <= 40"; break;
        case "5": $where[] = "price > 40"; break;
        case "all": // Leave the price part of the where clause blank;
            break;
        default: // Leave the price part of the where clause blank;
            break;
    }
}
NOTE: This code probably won't work in your page. You will need to write your own. This is just to point you in the right direction.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

i am not being able to do it at all
may be i need to remove that "all" option
but that way it will be unprofessional
dont have idea...what to do..?
is it really that difficult?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

rami wrote:may be i need to remove that "all" option
You should have all the code you need, you just need to tailor it to build up the correct SQL for your database query. This is not complicated.
rami wrote:is it really that difficult?
No.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'm not sure why, but I felt like messing with your code. Start with this and see what you can come up with. I seriously doubt I'll want to touch it again, but this should be enough to get you started.

Code: Select all

<?php
/*
	Might I suggest using more descriptive field names?
	What in the hell is cb1, cb2, cb3?!?!?!?
*/
// default to 0 if not set
$cb = ( isset($_GET['cb']) ) ? $_GET['cb'] : 0; //property type

// Default to 0 (all) if not set
$var = ( isset($_GET['cid']) ) ? trim($_GET['cid']) : 0; //price value if price is not all

// Default to 0 if not set
$cb1 = ( isset($_GET['cb1']) ) ? $_GET['cb1'] : 0; //area 0=all or else means not all

// Default to 0 if not set
$cb2 = ( isset($_GET['cb2']) ) ? $_GET['cb2'] : 0; //price returns 0 or < or > or =

// Default to 0 if not set
$cb3 = ( isset($_GET['cb3']) ) ? $_GET['cb3'] : 0; //property type 0 or 1=sale or 2=hire

// Lose this add trim in the initiall assignment - see above
$trimmed = trim($var); //trim whitespace from the stored variable

// Include DB stuff
require_once ('../../dbconnect.php');

/*
	Rather than rewriting your queries, why not just append them?
	Start with the base of the query, then add to it as needed.
	Also take note that the base query returns all records, so there
	is no need to make something specific for all.
*/
$query = 'SELECT property_id,property_for,property_type,pdistrict,property_area,price,sdescription,postedon,views FROM realstate where properrty_type=' . $cb;

// this is a default order by clause
$query_append = ' order by postedon desc';

if ($cb1 == 0) //if area is any
{
    if ($cb2 == 0) //price is all
    {
        $prop_for = ( $cb3 == 1 ) ? 'sale' : 'hire';
		$query .= ' and property_for like \'' . $prop_for . '\'';
    }
    else //price not all,ie < > =
    {
        if (empty($var))
        {
            echo "<p>Please enter a search...</p>";
            exit;
        }
        if ($cb3 == 0) //type is any
        {
            $query .= ' and price ' . $cb2 . ' ' . $var;
        }
		else
		{
		    $prop_for = ( $cb3 == 1 ) ? 'sale' : 'hire';
		    $query .= ' and property_for like \'' . $prop_for . '\' and price ' . $cb2 . ' ' . $var;
        }
    } //end of if price is < > =
} //end of if area==0
else //area is not any
{
    if ($cb1 == 0) //if area is any
    {
        if ($cb2 == 0) //price is all
        {
            $prop_for = ( $cb3 == 1 ) ? 'sale' : 'hire';
		    $query .= ' and property_for like \'' . $prop_for . '\'';
        }
        else //price not all,ie < > =
        {
            if (empty($var))
            {
                echo "<p>Please enter a search...</p>";
                exit;
            }
            
            if ($cb3 == 0)//type is any
            {
                $query .= ' and price ' . $cb2 . ' ' . $var;
            }
		    else
    		{
	    	    $prop_for = ( $cb3 == 1 ) ? 'sale' : 'hire';
		        $query .= ' and property_for like \'' . $prop_for . '\' and price ' . $cb2 . ' ' . $var;
            }
        } //end of if price is < > =
    }//end of else area==0
}

$query .= $query_append;

// At this point your query is built... Now do what you want with it.
echo "<b><center><h2>Following users Found having name similar to<font color=red> <i>$trimmed</i></font></h2></center></b>";
echo "<center><i>click on telephone number for full details</i></center>";
echo '<table border="1" width="100%" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td align="left" width="15%" bgcolor=\"cyan\"><b>Property id</b></td>
		<td align="left" width="20%" bgcolor=\"cyan\"><b>Property for</b></td>
		<td align="left" width="10%" bgcolor=\"cyan\"><b>District</b></td>
		<td align="left" width="5%" bgcolor=\"cyan\"><b>Area</b></td>
		<td align="left" width="18%" bgcolor=\"cyan\"><b>Price</b></td>
		<td align="left" width="25%" bgcolor=\"cyan\"><b>Views</b></td>
		<td align="left" width="7%" bgcolor=\"cyan\"><b>Posted on</b></td>
	</tr>';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{       
	if (mysql_num_rows($result) == 0)
	{
		echo "<p>No Result meeting this criterea!</p>";
		exit();
	}
	else
	{
		echo "  <tr>
		<td align=\"left\"><a href=\"distotaltel.php?mid={$row['property_id']}\">{$row['property_id']}</a></td>

		<td align=\"left\">{$row['property_for']}</td>
		<td align=\"left\">{$row['pdistrict']}</td>
		<td align=\"left\">{$row['property_area']}</td>
		<td align=\"left\">{$row['price']}</td>
		<td align=\"left\">{$row['views']}</td>
		<td align=\"left\">{$row['postedon']}</td>

	</tr>\n";       
	}
}
mysql_close();
?>
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

you're missing a single quote, Everah:

Code: Select all

$query .= ' and property_for like \'' . $prop_for . '\';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You posted as I was correcting. But thanks for spotting it. Syntax highlighting is a wonderful thing.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Yes... if only everyone would use it... :roll: :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Hence the little notices the moderators put into peoples posts...
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

THANKS FOR MR Everah, Mr. Modnificent FOR GIVING THAT MUCH TIME
this is the reason i always love to come to this forum
though u get some not good response there are always some people who are ready for help others
really thanks
i have not tested code but i think its right
but if may be if it is for some reason not right as well,thanks for giving so much time for that code
i was really needing it

really thanks
i am relatively new to php i think people say php is easy but i feel it has been made easy bcoz of helpful person and repository like u.
once thanks for code
i will try it....
Post Reply