Jump to content

I need suggestions.


jakebur01

Recommended Posts

I am attempting to create this dealer locator. The database and tables are all setup ok. I need some help with my code. I was hoping to get a few suggestions. I know what I want it to do. I would just like to learn and get this baby going.

 

 

Locator.php

 

 

<HTML> 
<?php 
$db = mysql_connect("localhost", "myuser", "mypassword"); 
mysql_select_db("mydatabase" ,$db) or die('Error connecting to database'); 

//here I am getting the zip code the user typed in and selecting the longitude and latitude off of that same row 
$Getzip = mysql_query("SELECT smithslo, smithsla, smithszi FROM zdat1 WHERE ('$formzip' = smithszi)",$db) or die('Error, could not find Zip Code.'); 
[color=blue]
//My problem is somewhere in this area
//here i am trying to get the area of which i want to extract from my zip code table 
$Maxlon = (smithslo + ".6000"); 
$Minlon = (smithslo - ".6000"); 
$Maxlat = (smithsla + ".6000"); 
$Minlat = (smithsla - ".6000"); 

//here i am trying to select the rows that are in that range, also it is spitting the error back at me that is on the line below. 
$Getziprange = mysql_query("SELECT smithszi FROM '$Getzip' WHERE (smithslo BETWEEN '$Maxlon' AND '$Minlon') AND (smithsla BETWEEN '$Maxlat' AND '$Minlat')",$db) or die('Error, could not find Zip Codes within range.'); [/color] 

//here i am trying to select the rows that have the same zip codes as the ones i selected 
$Getdealer = mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM learndb WHERE ('$Getziprange' = Zip)",$db) or die('Error retrieving dealers!'); 

if ($Shin) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_shin = 1)",$db) or die('Error retrieving Shindiawa dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getshin = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getshin["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getshin["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getshin["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getshin["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getshin["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getshin["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Little) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_lw = 1)",$db) or die('Error retrieving Little Wonder dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getlittle = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getlittle["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getlittle["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getlittle["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getlittle["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getlittle["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getlittle["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Man) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_m = 1)",$db) or die('Error retrieving Mantis dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getman = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getman["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getman["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getman["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getman["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getman["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getman["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Class) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_cl = 1)",$db) or die('Error retrieving Classin dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getclass = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getclass["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getclass["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getclass["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getclass["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getclass["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getclass["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Smiths) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_smith = 1)",$db) or die('Error retrieving Smiths dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getsmith = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getsmith["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getsmith["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getsmith["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getsmith["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getsmimth["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getsmith["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Troy) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_troy = 1)",$db) or die('Error retrieving Troy Bilt dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Gettroy = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Gettroy["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Gettroy["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Gettroy["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Gettroy["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Gettroy["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Gettroy["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($White) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_white = 1)",$db) or die('Error retrieving White Outdoor dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getwhite = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getwhite["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getwhite["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getwhite["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getwhite["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getwhite["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getwhite["Phone"]; 
} 
echo "</TABLE>"; 
} 

if ($Oregon) 
{ 
mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM '$Getdealer' WHERE (smiths_oregon = 1)",$db) or die('Error retrieving Oregon dealers.'); 
echo "<TABLE BORDER=1>"; 
echo"<TR><TD><B>Dealer</B><TD><TD><TD><TD><TD><TD><B>Address</B><TD><TD><TD><TD><TD><TD><B>City</B><TD><TD><TD><TD><TD><TD><B>State</B><TD><TD><TD><TD><TD><TD><B>Zip Code</B><TD><TD><TD><TD><TD><TD><B>Phone Number</B></TR>"; 
while ($Getoregon = mysql_fetch_array($Getdealer)) 
{ 
echo "<TR><TD>"; 
echo $Getoregon["Dealer"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getoregon["Address"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getoregon["City"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getoregon["State"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getoregon["Zip"]; 
echo "<TD><TD><TD><TD><TD><TD>"; 
echo $Getoregon["Phone"]; 
} 
echo "</TABLE>"; 
} 
?> 
</HTML>

 

 

 

 

and here is the form

 

<form id="form2" name="form2" method="post" action="dealerlocator/locator.php"> 
<table width="579"> 
<!--DWLayoutTable--> 
<tr> 
<td width="569"><label> </label> 
<div align="center"> 
<input type="radio" name="Select Product" value="Shin" /> 
<img src="Images/shin.gif" alt="Shindiawa" width="95" height="33" /><span class="style1">---- 
<input type="radio" name="Select Product" value="Little" /> 
<img src="Images/little.gif" alt="Little Wonder" width="95" height="33" />---- 
<input type="radio" name="Select Product" value="Man" /> 
<img src="Images/mantis.gif" alt="Mantis" width="95" height="33" />---- 
<input type="radio" name="Select Product" value="Class" /> 
<img src="Images/classin.gif" alt="Classin" width="95" height="33" /></span></div> 
<p><span class="style1">kjh lkjhlkjh kjh k </span></p></td> 
</tr> 
<tr> 
<td><label> </label> 
<div align="center"> 
<input type="radio" name="Select Product" value="Smiths" /> 
<img src="Images/smith-logo.gif" alt="Smiths" width="73" height="48" /><span class="style1">------ 
<input type="radio" name="Select Product" value="Troy" /> 
<img src="Images/troy.gif" alt="Troy Bilt" width="95" height="33" />---- 
<input type="radio" name="Select Product" value="White" /> 
<img src="Images/white.gif" alt="white Outdoor" width="95" height="33" />---- 
<input type="radio" name="Select Product" value="Oregon" /> 
<img src="Images/oregon copy.gif" alt="Oregon" width="95" height="33" /></span></div></td> 
</tr> 
</table> 
<div class="style1">Content for New Div Tag Goes Here 
<div>Content for New Div Tag Goes Here 
<div class="style26"><strong>Enter Your Zip Code Here: 
<label for="textfield"></label> 
</strong> 
<input type="text" name="formzip" id="textfield" /> 
<span class="style1">----- 
<input type="submit" name="Submit" value="Get Dealers!" id="Submit" /> 
</span></div> 
</div> 
</div> 
<p class="style1"> </p> 
</form>

 

thank you,

 

Jake

Link to comment
Share on other sites

I am just learning. I am gettin back my error in the section below.

 

$Maxlon = (smithslo + .6000);
$Minlon = (smithslo - .6000);
$Maxlat = (smithsla + .6000);
$Minlat = (smithsla - .6000);

$Getziprange = mysql_query("SELECT smithszi FROM '$Getzip' WHERE (smithslo BETWEEN '$Maxlon' AND '$Minlon') AND (smithsla BETWEEN '$Maxlat' AND '$Minlat')",$db) or die('Error, could not find Zip Codes within range.');

 

smithslo and smithsla are the longitude and latitudes I selected from my zip code table.

Link to comment
Share on other sites

Am I trying to go through to many resources? or Do I have too many mysql_query's goin on?

 

As I said, mysql_query() returns a result resource, not what you think. You need to use it in combination with mysql_fetch_assoc() to actually get a result. A simple example.

 

<?php

  // connect to db.

  $sql = "SELECT foo,bar FROM tbl";
  // store your result resource in $result.
  if ($result = mysql_query($sql)) {
    // check $result holds records
    if (mysql_num_rows($result)) {
      // fetch the first row.
      $row = mysql_fetch_assoc($result);
      // display record.
      echo $row['foo'];
    }
  }

?>

Link to comment
Share on other sites

I see what your saying.

 

Well then, take these two lines for instance.

 

$Getziprange = mysql_query("SELECT smithszi FROM '$Getzip' WHERE (smithslo BETWEEN '$Maxlon' AND '$Minlon') AND (smithsla BETWEEN '$Maxlat' AND '$Minlat')",$db) or die('Error, could not find Zip Codes within range.');

$Getdealer = mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM learndb WHERE ('$Getziprange' = Zip)",$db) or die('Error retrieving dealers!');

 

In your second query you have the variable $Getziprange, what do you expect this to be? It is a result resource.

Link to comment
Share on other sites

Like this?

 

$zip = mysql_query("SELECT smithslo, smithsla, smithszi FROM zdat1 WHERE ('$formzip' = smithszi)",$db) or die('Error, could not find Zip Code.');
$Getzip = mysql_fetch_assoc($zip)or die('Error, could not fetch $Getzip.');

$Maxlon = (smithslo + .6000);
$Minlon = (smithslo - .6000);
$Maxlat = (smithsla + .6000);
$Minlat = (smithsla - .6000);

$ziprange = mysql_query("SELECT smithszi FROM '$Getzip' WHERE (smithslo BETWEEN '$Maxlon' AND '$Minlon') AND (smithsla BETWEEN '$Maxlat' AND '$Minlat')",$db) or die('Error, could not find Zip Codes within range.');
$Getziprange = mysql_fetch_assoc($ziprange);

$Getdealer = mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM learndb WHERE ('$Getziprange' = Zip)",$db) or die('Error retrieving dealers!');

 

After these last changes I am still getting the error: 'Error, could not find Zip Codes within range.

Link to comment
Share on other sites

Also, no html tags in caps :)

 

<HTML> = not good.

<html> = beautiful

 

;D

 

Why....?

I Always Write HTML In Caps

I Just Looks Nicer, Neater And Is Easier To Read (In My Opinion)

 

Caps are not xhtml compliant. That is why he stated to do them in lowercase. HTML is not picky about case, just making it standard is the picky portion. Heck you could do the tags like <HtMl> </HTml> and it would work just fine. But to be compliant with W3C standards, lowercase is the way to go.

Link to comment
Share on other sites

Like this?

 

Thats more like it, but your code still has plenty of issues. For starters, always wrap your queries within tests, lines like....

 

$Getziprange = mysql_fetch_assoc($ziprange);

 

Assume your query succeeded which is very error prone and a bad habit to get into.

 

Also, this bit...

 

$Maxlon = (smithslo + .6000);
$Minlon = (smithslo - .6000);
$Maxlat = (smithsla + .6000);
$Minlat = (smithsla - .6000);

 

What is smithsla ?

Link to comment
Share on other sites

smithsla is a column in my zip code table that stands for latitude...    and    smithslo    is the longitude column..................  I got in a habit of naming the columns thing that I can rememeber...  I was having trouble getting my data to go in to the table in the beginning.  I first had my longitude column named Long, I found the word "Long" on the list of words not to use. Since then I have been naming my stuff odd abbeviations and names.            The zip code table which I have labeled "zdat1" holds 40,000 zip codes.

Link to comment
Share on other sites

If that is the case then you would need something like....

 

<?php

  $sql = "SELECT smithslo, smithsla, smithszi FROM zdat1 WHERE ('$formzip' = smithszi)"
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($zip);
      $Maxlon = $row['smithslo'] + 6000;
      $Minlon = $row['smithslo'] - 6000;
      $Maxlat = $row['smithsla'] + 6000;
      $Minlat = $row['smithsla'] - 6000;
    }
  }

?>

Honestly, as I said. This code is not ready to be stepped through with someone pointing out all the issues. I suggest you read up on how all these functions work and apply them to your code.

Link to comment
Share on other sites

You could, but you at least need a basic understanding of what is going on. Im not palming your problem off, merely suggesting you read some tutorials or at minimum, the manual for mysql_query.

 

Where would be a good place to find tutorials related to this? I want to learn!! Point me in the right direction.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.