Forum Moderators: coopster

Message Too Old, No Replies

Passing search results to a dynamic PHP map image file

         

benbg

5:47 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



I am working on a search page that plots coordinates on a U.S. map image based on the results of the search.

On my main page ("search.php") is a form that searches my database and produces results. Each of these results contains corresponding zip code coordinates. Currently, the action of the form is itself ("search.php") because I want the user to remain on this page. The map image is a separate PHP file ("map.php") that uses PHP drawing functions to plot dot images on the map image. The coordinates of these dot images need to come from the results of the search from "search.php". This works fine if I set the form action to "map.php". However, this only displays the map without the form. I need it to stay on the "search.php" page so that a new location can be entered into the form for different map results.

My problem is this: Due to the fact that "map.php" is not an include, but an external file wrapped in an <img> tag, the search results from "search.php" don't make it to "map.php". So, on the "search.php" page, "map.php" is displayed, but without any dots plotted from the search results.

Is there a way to pass the search results from "search.php" to "map.php" while remaining on the "search.php" page?


An example of my page is the following:
When a visitor first accesses the main page ("search.php"), he sees a search box and a blank map of the U.S. (with no locations plotted). When he searches for "New York" in the search box, he should still be on the "search.php" page (which contains the image produced by "map.php", and a dot should appear on the map where New York is located, based on the coordinates in the database. He would then have the option to
search for another location, say "Chicago". The map would then display a dot on Chicago's location, and he's still on the "search.php" page.


I'm hoping my real question here has a relatively simple answer that I am just overlooking:
Is there a way to pass search results to an external image file, which will be used within the same search page (with an <img> tag), without ever navigating away from the search page?

Sorry this question is so long! I just wanted to make it as clear as possible.

Thanks for your help!

Readie

6:04 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Should be able to do this with a session.

<?php
session_start();
$_SESSION['some_name'] = (!empty($_POST['some_other_name']))? $_POST['some_other_name'] : FALSE;

// All your form stuff etc

?>

And you can then call $_SESSION['some_name'] anywhere, in any file. Just remember to use session_start() (preferably right after the opening php tag - it doesn't like it when header info has already been sent) on any page that you use session variables.

benbg

11:08 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



Thank you! I'm new to PHP and haven't used sessions before. This works great!

One more question:
My search often produces multiple results, so multiple dots need to be plotted in the map file.
How would I put an array from mysql_fetch_assoc($results) into a $_SESSION variable to be used in the map image file?

Readie

8:55 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can just set the session as the array:

$_SESSION['something'] = mysql_fetch_assoc(mysql_query($sql));

If you're returning multiple rows, then you'd just do this:

while($_SESSION['something'] = mysql_fetch_assoc(mysql_query($sql))) {
// Stuff here
}

benbg

1:02 am on Apr 25, 2010 (gmt 0)

10+ Year Member



I tried this, but it's still only returning one result (and there are multiple results in reality):

On the main file, I set the session as an array to:

$_SESSION['row_rsMap'] = mysql_fetch_assoc($rsMap);

($rsMap is already set equal to mysql_query($sql).)

Then, to return multiple rows on the other file, I used:

while($_SESSION['row_rsMap'] = mysql_fetch_assoc($rsMap)) {
echo $_SESSION['dot_x'];
echo $_SESSION['dot_y'];
}


I get an error message saying that there is no parameter given for mysql_fetch_assoc. The map file doesn't recognize $rsMap from the main file.

I tried setting:
$_SESSION['rsMap'] = $rsMap;

and then using:
while($_SESSION['row_rsMap'] = mysql_fetch_assoc($_SESSION['rsMap'])) {
echo $_SESSION['dot_x'];
echo $_SESSION['dot_y'];
}

but that didn't work either.

I think I'm misunderstanding something in your instructions because it seems that every variable that is originally declared in the main file, but is then used in the secondary file, would need to be part of a SESSION variable.

Readie

11:25 am on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while($_SESSION['row_rsMap'] = mysql_fetch_assoc($rsMap)) {
echo $_SESSION['dot_x'];
echo $_SESSION['dot_y'];
}

If dot_x and dot_y are supposed to be part of what's returned from the mysql_fetch_assoc() function, then what you acctually need to do is this:

echo $_SESSION['row_rsMap']['dot_x'];
echo $_SESSION['row_rsMap']['dot_y'];

$_SESSION is an array already, so setting one of it's values as an array creates a multidimensional array.

every variable that is originally declared in the main file, but is then used in the secondary file, would need to be part of a SESSION variable.

Unless you are using include/require functions, yes.

benbg

9:57 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Since $rsMap is originally declared in the main file, but needs to be used in the attached image file, wouldn't I need to save it in a session variable like
$_SESSION['rsMap'] = ($rsMap);

and then in the attached image file, write
while($_SESSION['row_rsMap'] = mysql_fetch_assoc($_SESSION['rsMap'])) {
echo $_SESSION['row_rsMap']['dot_x'];
echo $_SESSION['row_rsMap']['dot_y'];
}


When I use in (the attached image file)
while($_SESSION['row_rsMap'] = mysql_fetch_assoc($rsMap)) {
echo $_SESSION['row_rsMap']['dot_x'];
echo $_SESSION['row_rsMap']['dot_y'];
}

I get an error message:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given...

Thanks for sticking with me on this!

Readie

10:01 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SESSION['rsMap'] = ($rsMap);

Remove the brackets around $rsMap. I suspect that'll fix your problems here if you are trying to run the session value through the mysql_fetch_assoc() function.

By the way, as $_SESSION is persistant (i.e. it lasts until the browser is closed or until the user has been inactive for a certain length of time) I suggest doing this after you've echoed the image out:

unset($_SESSION['rsMap']);

benbg

10:49 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Oops. The new warning says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, integer given...

Here's my whole code for two sample files that I'm using to test it.

main_page.php
<?php
session_start();

// Database Connection
require_once('Connections/dbFeedback.php');

// Dreamweaver Recordset Script
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_dbFeedback, $dbFeedback);
$query_rsFeedback = "SELECT * FROM feedback";
$rsFeedback = mysql_query($query_rsFeedback, $dbFeedback) or die(mysql_error());
$row_rsFeedback = mysql_fetch_assoc($rsFeedback);
$totalRows_rsFeedback = mysql_num_rows($rsFeedback);
?>


<!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>Untitled Document</title>
</head>

<body>
<?php
$_SESSION['rsFeedback'] = $rsFeedback;
$_SESSION['row_rsFeedback'] = mysql_fetch_assoc($rsFeedback);
$_SESSION['title'] = $row_rsFeedback['title'];
$_SESSION['author_name'] = $row_rsFeedback['author_name'];
?>
<a href="session_test.php">Session Test</a>
</body>

</html>

<?php
mysql_free_result($rsFeedback);
?>


secondary_page.php
<?php
session_start();
?>
<!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>Session Test</title>
</head>

<body>
<?php
while ($_SESSION['row_rsFeedback'] = mysql_fetch_assoc($_SESSION['rsFeedback'])) {
echo $_SESSION['title'];
echo $_SESSION['author_name'];
}
?>
</body>
</html>


I'm not sure what I'm doing wrong here.

benbg

2:02 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Oops. In main_page.php, the link is actually
<a href="secondary_page.php">Session Test</a>

benbg

7:04 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



I figured out how to make my search page with the external map file work. I just performed the search query in the map file itself and used a session variable to carry the search terms from the main file to the map file. The query used the session variable to find results and plot the points.

Thanks for all your help! I really appreciate it!

Readie

7:18 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you figured it out. Sorry it took me so long to get back after your post last night by the way: I went to bed, and I've had the most horribly demanding day today.

Good luck with the rest of your project, and if you need any more help post here and I'll do my best to assist :)

benbg

3:20 am on Apr 28, 2010 (gmt 0)

10+ Year Member



Thanks for being so incredibly helpful! I'm sure I'll post again.

benbg

7:13 pm on May 5, 2010 (gmt 0)

10+ Year Member



Hello Readie,

I am again in need of your help.

I have an SQL query that returns a single set of x and y coordinates to plot the user's current location on my map, as entered in a search form.
I have another query that returns the x and y coordinate locations of various cities around the user's location.
I have a third query that returns the information for each of these cities in a scroll box so the user can see detailed information.

I'm trying to sort this third query by proximity. I worked out a PHP equation that accurately finds the distance between an individual city and the user's location. I put the equation in a loop so it finds all the distances between the user's location and each appropriate city. I echoed the results to test it, and it works perfectly. Now, however, I need to save the results to an array variable instead of echoing them, so that I can use them in the ORDER BY
... ORDER BY '$proximity' ...
section of my third query that provides details about each city.

How can I save the results of the loop as an array inside the "$proximity" variable?

I assume this is what I need to do in order to sort the detailed results from the third query by each city's proximity to the user's location. It would be easy if the distances between the user's location and each city were always the same. Then, I would just add that column in the database and sort by it. But, the user gets to choose his or her location, so the distances need to be calculated on the fly.

Of course, if you know a better way to approach this, I'm all ears.

Thanks again for your help!

Readie

7:48 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the immediate method that leaps to mind is developing an array after the third query, then having a fourth query that get's executed multiple times to develop the results.

So you'd have something like this:
$current_x = 13;
$current_y = 42; // These are ofcourse dynamic, user input etc.
// Strongly suggest checking with is_numeric()

$sql = QUERY_THREE_HERE;
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
$sql_res = array();

for($i = 0; $i < $rows; $i++) {
$loc_x = mysql_result($result, $i, "x_axis");
$loc_y = mysql_result($result, $i, "y_axis");

$distance_x = abs($loc_x - $current_x);
$distance_y = abs($loc_y - $current_y);

$distance = sqrt(pow($distance_x, 2) + pow($distance_y, 2));
$loc = mysql_result($result, $i, "location_id");

$sql_res[$loc] = $distance;
}

asort($sql_res); /* You may need to use arsort() here instead of asort(), not sure */

foreach($sql_res as $loc => $distance) {
$sql = 'SELECT some_columns FROM some_table WHERE location_id = "' . $loc . '" LIMIT 1';
$result = mysql_query($sql);
// Build your table etc here
}

benbg

4:00 am on May 19, 2010 (gmt 0)

10+ Year Member



I ended up having to complete some other tasks before I got to this, but I finally worked it out. This was just the insight I needed to make it work.

Thanks again for your help!