NEW TO PHP, PLEASE HELP!

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

Post Reply
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

NEW TO PHP, PLEASE HELP!

Post by HSKrustofsky »

I am working on a real estate website, and I am having some difficult with a part of my search function. On the search page, you can either search by city, state, or zip code, and I have an area where you can search a price range, number of bedrooms, and number of bathrooms. haven't gotten to the bed and bath section, but I can seem to get the price range to work. If someone could help that would be great. Also, if you can tell me in what direction to go with the whole bed and bath thing, that would be great as well. Thanks in advanced.

Below is the code for that portion of my site:

Code: Select all

 
<?php
 
    include('db_connect.php');
    
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Listing</title>
 
<link rel="stylesheet" type="text/css" href="style.css" />
 
<SCRIPT LANGUAGE="JavaScript">
 
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=500,left = 302,top = 134');");
}
</SCRIPT>
 
</head>
 
<body>
 
<?php
 
    if (isset($_GET['search']) || isset($_GET['min']) || isset($_GET['max']))
        {
        
?>
 
    <table align="center" cellpadding="0" cellspacing="0" style="background-color:#FFFFFF;">
        <tr>
            <td colspan="8">
            <img src="../images/header.jpg" />            
            </td>
        </tr>
        <tr>
              <td><a href="index.php" id="bnt1"></a></td>
              <td><a href="#" id="bnt2"></a></td>
              <td><a href="forsale.php" id="bnt3"></a></td>
              <td><a href="#" id="bnt4"></a></td>
              <td><a href="#" id="bnt5"></a></td>
              <td><a href="#" id="bnt6"></a></td>
              <td><a href="#" id="bnt7"></a></td>
              <td><a href="#" id="bnt8"></a></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8">
    <table width="750px" align="center" cellpadding="0" cellspacing="0" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt;">
        <tr>
            <td>
            <fieldset style="border:solid 1px #000000;">
            <legend style="color:#666666; font-size:14pt;">Search Results</legend>
                <table width="100%">
                    <tr>
                        <td align="center" colspan="2">
                        Click on the image to view more information on each listing.                        
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    
<?php   
        
    $query = "SELECT * FROM `listing` WHERE `zip`  LIKE '%".$_GET['search']."%' OR `city` LIKE '%".$_GET['search']."%' OR `state` LIKE '%".$_GET['search']."%' OR `price` >= '%".$_GET['min']."%' AND `price` <= '%".$_GET['max']."%'";
    $result = mysql_query($query) or die(mysql_error());
    
    while (($row = mysql_fetch_array($result)))
        {
        
        $price = $row['price'];
        
        $price = number_format($price);
    
?>
                    
                    <tr>
                        <td width="125px">
                        <a href="javascript&#058;popUp('listing_info.php<?php echo '?id='.$row['autoid']; ?>')"><img src="../images/house-selling-1.jpg" width="165" height="134" /></a>                        </td>
                      <td valign="top">
                        $<?php echo "$price"; ?><br />
                        <?= $row['address'] ?><br />
                        <?= $row['city'] ?>, <?= $row['state'] ?> <?= $row['zip'] ?><br />
                        <?= $row['bed'] ?> Bedroom, <?= $row['bath'] ?> Bath<br />
                        <?= $row['short'] ?><br />                      
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                       <hr size="1" color="#000000" />                        
                       </td>
                    </tr>
                    
<?php
 
        }
 
?> 
                    
                </table>
            </fieldset>            
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8" align="center" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#999999;">
            <a href="index.php" id="link">Home</a> | Sell Your Home | <a href="listing.php" id="link">Homes For Sale</a> | Testimonials | Mortgage | Title | Learning Center | Contact | <a href="admin_login.php" id="link">Admin</a>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
    </table>            
            </td>
        </tr>
    </table>
    
<?php
        
        exit();
        
        }
 
?> 
 
<?php
 
    if (isset($_GET['all']))
        {
        
?>
 
    <table align="center" cellpadding="0" cellspacing="0" style="background-color:#FFFFFF;">
        <tr>
            <td colspan="8">
            <img src="../images/header.jpg" />            
            </td>
        </tr>
        <tr>
              <td><a href="index.php" id="bnt1"></a></td>
              <td><a href="#" id="bnt2"></a></td>
              <td><a href="forsale.php" id="bnt3"></a></td>
              <td><a href="#" id="bnt4"></a></td>
              <td><a href="#" id="bnt5"></a></td>
              <td><a href="#" id="bnt6"></a></td>
              <td><a href="#" id="bnt7"></a></td>
              <td><a href="#" id="bnt8"></a></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8">
    <table width="750px" align="center" cellpadding="0" cellspacing="0" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt;">
        <tr>
            <td>
            <fieldset style="border:solid 1px #000000;">
            <legend style="color:#666666; font-size:14pt;">Listings</legend>
                <table width="100%">
                    <tr>
                        <td align="center" colspan="2">
                        Click on the image to view more information on each listing.                        
                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    
<?php   
        
    $query = 'SELECT * FROM `listing` ORDER BY `autoid` DESC';
    $result = mysql_query($query) or die(mysql_error());
    
    while (($row = mysql_fetch_array($result)))
        {
        
        $price = $row['price'];
        
        $price = number_format($price);
    
?>
                    
                    <tr>
                        <td width="125px">
                        <a href="javascript&#058;popUp('listing_info.php<?php echo '?id='.$row['autoid']; ?>')"><img src="../images/house-selling-1.jpg" width="165" height="134" /></a>                        </td>
                      <td valign="top">
                        $<?php echo "$price"; ?><br />
                        <?= $row['address'] ?><br />
                        <?= $row['city'] ?>, <?= $row['state'] ?> <?= $row['zip'] ?><br />
                        <?= $row['bed'] ?> Bedroom, <?= $row['bath'] ?> Bath<br />
                        <?= $row['short'] ?><br />                      
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                       <hr size="1" color="#000000" />                        
                       </td>
                    </tr>
                    
<?php
 
        }
        
?> 
                    
                </table>
            </fieldset>            
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8" align="center" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#999999;">
            <a href="index.php" id="link">Home</a> | Sell Your Home | <a href="listing.php" id="link">Homes For Sale</a> | Testimonials | Mortgage | Title | Learning Center | Contact | <a href="admin_login.php" id="link">Admin</a>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
    </table>            
            </td>
        </tr>
    </table>         
 
<?php
 
    } else {
 
?>
    
    <table align="center" cellpadding="0" cellspacing="0" style="background-color:#FFFFFF;">
        <tr>
            <td colspan="8">
            <img src="../images/header.jpg" />            
            </td>
        </tr>
        <tr>
              <td><a href="index.php" id="bnt1"></a></td>
              <td><a href="#" id="bnt2"></a></td>
              <td><a href="forsale.php" id="bnt3"></a></td>
              <td><a href="#" id="bnt4"></a></td>
              <td><a href="#" id="bnt5"></a></td>
              <td><a href="#" id="bnt6"></a></td>
              <td><a href="#" id="bnt7"></a></td>
              <td><a href="#" id="bnt8"></a></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8">
    <table width="750px" align="center" cellpadding="0" cellspacing="0" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt;">
        <tr>
            <td style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12pt;">
            <a href="<?= $_SERVER['PHP_SELF'] ?>?all" id="link">View All Listings</a>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td>
                <table align="center" width="452px">
                    <tr>
                        <td>
                            <fieldset style="border:solid 1px #000000;">
                                <legend style="color:#666666; font-size:14pt;">Search</legend>
                                <table width="100%" align="center">
                                    <tr>
                                        <td style="font-size:10pt; font-family:Verdana, Arial, Helvetica, sans-serif; color:#999999;" width="300px">
                                        <form action="<?= $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="get">
                                        <center><input type="text" name="search" onfocus="if(this.value=='Search')this.value='';" onblur="if(this.value=='')this.value='Search';" size="50px" value="Search" /> <br />
                                        Enter City, State or Zip Code</center><br /><br />
                                        <font color="#000000">&nbsp;&nbsp;Price Range:<br />
                                        <center>
                                        From: <input type="text" name="min" />&nbsp;&nbsp;&nbsp;&nbsp;To: <input type="text" name="max" /><br /><br />
                                        Bedrooms:
                                            <select name="bed">
                                                <option value="" selected="selected"></option>
                                                <option value="1">1+</option>
                                                <option value="2">2+</option>
                                                <option value="3">3+</option>
                                                <option value="4">4+</option>
                                                <option value="5">5+</option>
                                            </select>&nbsp;&nbsp;&nbsp;
                                            Baths:
                                            <select name="bath">
                                                <option value="" selected="selected"></option>
                                                <option value="1">1+</option>
                                                <option value="1.5">1.5+</option>
                                                <option value="2">2+</option>
                                                <option value="2.5">1.5+</option>
                                                <option value="3">3+</option>
                                                <option value="3.5">1.5+</option>
                                                <option value="4">4+</option>
                                                <option value="4.5">1.5+</option>
                                                <option value="5">5+</option>
                                            </select>
                                            </center>
                                            </font>
                                            <div style="text-align:right;"><input type="submit" value="Search" /></div>
                                        </form>
                                        </td>
                                    </tr>
                                </table>
                            </fieldset>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td colspan="8" align="center" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:8pt; color:#999999;">
            <a href="index.php" id="link">Home</a> | Sell Your Home | <a href="forsale.php" id="link">Homes For Sale</a> | Testimonials | Mortgage | Title | Learning Center | Contact | <a href="realestate_admin.php" id="link">Admin</a>
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
    </table>            
            </td>
        </tr>
    </table>
    
<?php
    
    }
    
?>    
    
</body>
</html>
 
Last edited by HSKrustofsky on Mon Oct 27, 2008 1:05 am, edited 2 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: NEW TO PHP, PLEASE HELP!

Post by requinix »

Before I say anything, go back and edit your post. Put [ php ] tags around the code.

Do it now. Right now. Scroll up a bit, find the Edit button, and do it.

Thanks.
Last edited by requinix on Mon Oct 27, 2008 1:18 am, edited 2 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: NEW TO PHP, PLEASE HELP!

Post by s.dot »

OK, to address your problem..

In your SQL query, you need to group your price range with ()'s.

Code: Select all

OR (`price` >= '%" . $_GET['min'] . "%' AND `price` <= '%" . $_GET['max'] . "%')
You also need to escape all $_GET variables (and actually any posted variables) with mysql_real_escape_string() to help avoid SQL injection.
And finally, don't do "<?=". Use "<?php echo" instead. This aids portability.

Hope this helps. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: NEW TO PHP, PLEASE HELP!

Post by HSKrustofsky »

Thanks tasairis and scottay for your help.

Tasairs - Went back and did what you said, and thanks for that. New to this forum stuff, and always glad for someone to help, and not yell. THANKS

Scottay - Did what you said, about putting the price range in (), but when I try to test my search, nothing happens. Also, I saw what you said about the mysql_real_escape_string(), where would I add that?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: NEW TO PHP, PLEASE HELP!

Post by requinix »

Are you really storing your prices as strings? That's what it looks like.

Try something more like

Code: Select all

...OR (`price` >= " . intval($_GET['min']) . " AND `price` <= " . intval($_GET['max']) . ")
By the way, AND has higher precedence than OR, meaning parentheses aren't necessary. Helps readability though.
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: NEW TO PHP, PLEASE HELP!

Post by HSKrustofsky »

tasairis:

You are a genius! Thank you very much for your help. I have one question for you. Would I do the same for bedrooms and baths as I did for the price rage. I pretty much have it to where if they choose something, it will search the number plus every above.

Ex. if they choose 2+ bedrooms, it will show houses with 2 or more.

Once again thank you for the help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: NEW TO PHP, PLEASE HELP!

Post by requinix »

If you store the number of bedrooms as a number then yes, it's the same basic idea.
Rule is this: with numbers you don't use quotes, with strings you do.

Here's something fun you can try:

Code: Select all

<select name="bedrooms">
<option value="=0">None</option> <!-- studio appt? -->
<option value="=1">1</option>
<option value=">=2">2+</option>
</select>

Code: Select all

$bedroom_cond = $_POST["bedrooms"];
if (!preg_match('/^(<=?|>=?|=)\d+$/', $bedroom_cond)) $bedroom_cond = ">=0";
 
$query = "SELECT ... WHERE `bedrooms` $bedroom_cond";
The preg_match validates the condition: has to be <, >, =, <=, or >= followed by a positive number.
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: NEW TO PHP, PLEASE HELP!

Post by HSKrustofsky »

Well, tried doing it, but for some reason it won't give me anything greater than or equal to anything. It just shows all listings, and the only way it will show a certain listing is if I match the number of bedrooms with the number of baths.

Any ideas?

-Added-:
Don't know what I did wrong,but it works now.
Post Reply