Jump to content

How to echo out all rows?


amg182

Recommended Posts

Hi guys.

 

I have the following code that works out the distance from the postcode entered into a form and returns the distance in miles to a postcode stored within a database table.

It works fine but only finds the distance between the postode entered and the first row of the table(ID 1). The table has approx 10 more rows of data each having there own postcode. So how can i get this script to find the distance between the postcode entered in the form and all the postcodes within the rows of my table?

 

Here is the link to the webpage its on  http://cars.netau.net/index.php

 

<form action="php question.php" method="get">
Enter Your Postcode: (i.e. BT22)<input type="text" name="enter" />
<input type="submit" />
</form> 


Your Postcode: <?php echo $_GET["enter"]; ?>.
<br></br>

<?php

$pcode=$_GET["enter"];

?>

<?php


$pcode=$_GET["enter"];

$dbcnx = mysql_connect("host",

"user", "pass");

if (!$dbcnx) {

echo( "

Unable to connect to the " .

"database server at this time (this is a host connect problem).
" );

exit();

}

// Select the database

if (! mysql_select_db("db_name", $dbcnx) ) {

echo( "

Unable to locate the " .

"database at this time(this is a dbconnect problem).
" );

exit();


}


//finds value from database field

$from = mysql_query("SELECT * FROM `tbl_name`");



while($row = mysql_fetch_array($from))
  {
  


$start=$pcode;

$fin=$row['Location'];
}

{


// Postcode enterd via input form

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'");

$row=mysql_fetch_array($result);

$gridn[0]=$row['Grid_N'];

$gride[0]=$row['Grid_E'];



// Postcodes within table

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'");

$row=mysql_fetch_array($result);

$gridn[1]=$row['Grid_N'];

$gride[1]=$row['Grid_E'];

// TAKE GRID REFS FROM EACH OTHER.

$distance_n=$gridn[0]-$gridn[1];

$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS

$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;


}
?>


<table border="1">
<tr>
<th>Your Postcode</th>
<th>Destination</th>
<th>Distance</th>
</tr>
<tr>
<td><?php echo ''.$start.'' ?></td>
<td><?php echo ''.$fin.'' ?></td>
<td><?php echo ''.round($hypot/1000,2).' Miles';?></td>
</tr>
</table>

 

Any help would be much appreciated!

 

 

Link to comment
Share on other sites

Hi thanks for reply.

 

I tried changing this but still only echoes out one row....

 

here is what I have changed it to as you said.

 

<form action="php question.php" method="get">
Enter Your Postcode: (i.e. BT22)<input type="text" name="enter" />
<input type="submit" />
</form> 

Your Postcode: <?php echo $_GET["enter"]; ?>.

<?php

//connect to DB



$result = mysql_query("SELECT * FROM `cars`");


while($row = mysql_fetch_array($result))
  {

$start=$_GET["enter"];

$fin=$row['Location'];
}

{


// Postcode enterd via input form

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'");

while($row=mysql_fetch_array($result)){

$gridn[0]=$row['Grid_N'];

$gride[0]=$row['Grid_E'];
}


// Postcodes within table

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'");

while($row=mysql_fetch_array($result)){

$gridn[1]=$row['Grid_N'];

$gride[1]=$row['Grid_E'];
}

// TAKE GRID REFS FROM EACH OTHER.

$distance_n=$gridn[0]-$gridn[1];

$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS

$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;


}
?>


<table border="1">
<tr>
<th>Your Postcode</th>
<th>Destination</th>
<th>Distance</th>
</tr>
<tr>
<td><?php echo ''.$start.'' ?></td>
<td><?php echo ''.$fin.'' ?></td>
<td><?php echo ''.round($hypot/1000,2).' Miles';?></td>
</tr>
</table>

 

Any idea why its only outputing the first row?

Link to comment
Share on other sites

this is untested, just off the top of my head... but try it out. Maybe it'll work... lol

 


<table border="1">
<tr>
<th>Your Postcode</th>
<th>Destination</th>
<th>Distance</th>
</tr>
<?php
$start=$_GET["enter"];
$ends = array();
//finds value from database field
$result = mysql_query("SELECT * FROM `cars`");
while($row = mysql_fetch_array($result)){
$ends[]=$row['Location'];
}

// Postcode enterd via input form
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start'");
while($row=mysql_fetch_array($result)){
$gridn[0]=$row['Grid_N'];
$gride[0]=$row['Grid_E'];
}

foreach($ends as $fin){
// Postcodes within table
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin'");
while($row=mysql_fetch_array($result)){
	$gridn[1]=$row['Grid_N'];
	$gride[1]=$row['Grid_E'];
}

// TAKE GRID REFS FROM EACH OTHER.
$distance_n=$gridn[0]-$gridn[1];
$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS
$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;
echo '<tr>
		<td>'.$start.'</td>
		<td>'.$fin.'</td>
		<td>'.round($hypot/1000,2).' Miles</td>
		</tr>';
}
?>
</table>

Link to comment
Share on other sites

Postcode Table:

 

"Postcode_ID"        "PCode"            Grid_N          Grid_E        Latitude      Longitude

1                                  AB10            392900        804900        57.135        -2.117

2                                  AB11            394500        805300        573138        -2.092

3                                  AB12            393300        801100        57.101        -2.111

4                                  AB13            385600        801900        57.108        -2.237

5                                  AB14            383600        801100        57.101          -2.27

 

 

Location Table:

 

"ID"    "Location"

1          ME14

2          ME16

3          L7

4          TN24

5          ME23

 

Thanks,

 

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.