Google maps v3 - adding php generated markers by user input - php

Hello i'm trying to make a bus route finder in google maps.
So far i've got a static route to display on the map with the help of google's "Using PHP/MySQL with Google Maps" example.
That works by a php file extracting data from a database and making a xml file, and then the google maps javascript using that xml data to make markers/polyline.
My version: http://www.ykothari.co.uk/gmap-php/gmap-php.html
php code:
<?php
require("dbserverinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect ($dbserver, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM bus109;";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<?xml version="1.0"?>' . "\n";
echo '<markers>' . "\n";
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<markers109 ';
echo 'stopname="' . parseToXML($row['stopname']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'service="' . $row['service'] . '" ';
echo 'stopid="' . $row['stopid'] . '" ';
echo '/>' . "\n";
}
// End XML file
echo '</markers>';
?>
The dataset is like this: https://spreadsheets.google.com/ccc?key=0AqEGwIC84XiqdGZXM2VMS1VPazdrVExsa0t6TjhPWlE&hl=en&authkey=CMSJup8M
Now i want to add a search box for user to type in the bus route number that they want to see, so when they type in say "1" in the search box the php script only gets markers for that routenumber. I assume that the "select * from table" in the php file, is getting the data. Is there a way i can modify this line as the user types it or is there any other way to achieve this? So that the file only fetches the row data for that route and not the entire database as the database about 50,000 entries.
Any links to examples or tutorials are appreciated.

There are a number of steps you'll need to do.
1). Modify your php file to accept GET or POST request parameters with the specific information desired. See this link for more info: http://us.php.net/manual/en/reserved.variables.get.php
2). You'll need to modify the SQL statement:
$query = "SELECT * FROM bus109;";
to include a WHERE clause specific to the structure of your database. http://www.w3schools.com/sql/sql_where.asp
3). You'll need to set the request parameters in your javascript where you make the xmlHttpRequest
downloadUrl('xmlgen.php?bus_route_id=1&....'
...if you choose to use an HTTP Get.
4). Make a searchbox either in a form or with a javascript listener to allow for user input.

Related

Return Specific Row From DB

I have multiple links on a page where each link is suppose to return a specific row of data from a database. When the link is clicked, the user is forwarded to another page where the info associated with that link is displayed. Here is the code:
//db connection: (using xampp)
mysql_connect('localhost', 'root', '');
mysql_select_db('db_name');
$sql = "SELECT * FROM user_input";
$records = mysql_query($sql);
//code:
<div>
$open_report = mtsql_fetch_assoc($records);
echo "Error Report# {$open_report['id']};
echo "<p>" .$open_report['comments'] . "</p>";
</div>
The problem is it always returns the same row of data. Each row in the db is associated with a link and when that link is clicked I want to return the associated row of data in the db. I think it may have to do with this line: $sql = "SELECT * FROM user_input"; but I'm not sure how to fix it. If anyone can help it would be greatly appreciated.
I have restructured my answer to give it a better flow. I also noticed you are using mysql_ not mysqli_ . You need to use mysqli_ as mysql is depreciated.
EDIT: This would be the page that displays all the error reports. You would want to output them in the form of a hyperlink that passes a GET parameter to the page that shows the details.
$sql = "SELECT ID, Description, etc, etc from reports";
$open_reports = mysqli_query($sql);
//error check here as well if ANY results were returned
while($row = mysqli_fetch_array($open_reports, MYSQLI_ASSOC)) {
echo ''' . $open_reports['Description'] . '';
}
This will give you links that look like
detailspage.php?id=1 detailspage.php?id=2
etc...
On the "detailspage.php" You can capture that ID and display dynamic information on that same page.
if (isset($_GET['ID'])){
$sql = "Select * from user_input where ID='" . $_GET['id'] . "'";
$records = mysqli_query($sql)
while($open_report = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
echo "Error Report# " . $open_report['id'] . "<br/>";
echo "<p>" .$open_report['comments'] . "</p>";
}
}

How to create a series of DIVs using PHP + mySQL?

I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)
Here is the PHP code so far, simply to get the information from the database and display it on a webpage...
(I've been using XAMPP to provide an environment for me to test the code in)
<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());
mysql_select_db("Database1") or die(mysql_error());
$strSQL = "SELECT * FROM Products";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";
}
mysql_close();
?>
I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.
If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.
You have an extra semicolon in your code
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Replace with
echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
mysql_fetch_array needs to be used like this (see PHP Doc):
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
}
or you could just use "mysql_fetch_assoc" instead.
HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Also, as mentioned, you have an extra semi-colon.
Dont forget these basics markups :
`<HTML>
<HEAD>
</HEAD>
<BODY> put in here your divs
</BODY>
</HTML>`

Search script that searches a txt file and prints or echos results

I have parts website that I need to update the search script for. I have an Excel sheet with 3 columns, Part No, Part Name, and Page Number. I just want to create a simple search box and when an entry is made and you click submit it searches through the txt list and displays/echos/prints on the web page any lines of the txt file that correspond, be it part number or description name. It is to help people locate a part and then link them to the page of a catalog it can be found on - so the page number is a link to the page.
This search script below does exactly what I need it to do, I did not write it, it was written by a former employee - it connects to a mySQL db - BUT I cannot do that anymore, the mySQL is on a shared hosting service and the minimum character search is set to 4 and cannot be modified. I need to be able to search 3 character words such as 'oil', 'brm', 'rod'.
I've been trying for a few days to find an alternative method. I would like to be able to connect or search the txt or Excel file that contains the list of part numbers and the page they can be found on and NOT have it connect to the mySQL db.
I don't know how to modify this to have it connect to a txt file instead of the mySQL db.
Is that even possible? Do I need to use a different kind of script?
I appreciate any guidance. Thank you!
<?php
if ($_POST['action'] =="search") {
function make_page_url($pageno) {
return "../vwcatalog/2013/" . $pageno . ".html";
}
echo "<tr><td>Search Results : (HINT-If Nothing Is Listed Below, Try the Table of Contents at the Bottom of the Page<br>
Search Hint: If necessary, try using singular words, instead of plural, i.e.; 'seat' - instead of 'seats'.)</td></tr>\n";
$dbsearchlink=mysql_connect($db["host"],$db["user"],$db["pass"])
or die("Failed to make database connection: " . mysql_error());
mysql_select_db($db["used"])
or die("Failed to select database: " . mysql_error());
$query=sprintf("select partno,description,pageno from part_to_page where year=2013 and match(partno,description) against ('%s')",
mysql_real_escape_string($_POST['searchfor']));
$result=mysql_query($query) or die("Query failed: " . mysql_error());
echo "<tr><td><table>\n";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$url=make_page_url($row['pageno']);
echo "\t<tr>";
echo "<td>" . $row['partno'] . "</td>";
echo "<td><a href=$url>" . $row['description'] . "</a></td>";
echo "</tr>\n";
}
echo "</table></td></tr>\n";
mysql_free_result($result);
mysql_close($dbsearchlink);
}
?>
Why not just switch the query
if(strlen($_POST['searchfor']) < 4){
$query=sprintf("select partno,description,pageno from part_to_page where year=2013 and ( partno LIKE '\%%s\%' OR description LIKE '\%%s\%')",
mysql_real_escape_string($_POST['searchfor']),
mysql_real_escape_string($_POST['searchfor']));
} else {
$query=sprintf("select partno,description,pageno from part_to_page where year=2013 and match(partno,description) against ('%s')",
mysql_real_escape_string($_POST['searchfor']));
}

How to Make a table field return as a link (from a simple echo command)

I've been trying for hours to figure out how to put a link into the following text output via PHP echo(). I basically want to make the title field I'm pulling from my events table (as seen in #4 in the code below) to come back into the browser as a link instead of just text...
the original code that brings back the event title:
<?php
// 3. Perform database Query to bring list of events
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
// 4. Use returned Data
while ($row = mysql_fetch_array($result)) {
echo $row ["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
How would I go about getting that $row["eventtitle"] to appear in the browser as a link? Let's say if the link was just "eventprofile.php". This is probably an easy fix, but I've been getting a million errors with trying different things with <a href>s.
<?php
$result = mysql_query("SELECT * FROM events", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo "".$row["eventtitle"]."<br/>".$row["eventdesc"]."<br/>";
}
?>
add an anchor tag for it!!
echo "" . $row['eventtitle'] . '' . "<br />" .$row["eventdesc"]."<br/>";
And thats it.

Fetching queries from MySQL and printing it in a javascript calendar

I have a hardcoded javascript calendar and I need to print the events from my database which is a PHP MySQL server side database. But I'm not sure how to fetch the queries from mysql and print it into the javascript calendar :/ I found something that makes use of VBSCRIPT but its very confusing :/
The general outline:
Use PHP to access the database and fetch rows of calendar data (using a query). Then, print it out to a Javascript data structure like JSON or native Javascript that is loaded into the browser. Then, work with the Javascript calendar code to load the data into the view the browser presents to the user on the webpage.
There's a lot of variation that can occur here, but roughly (in semi-psuedo code):
Within page contains the calendar (outputs in JSON notation)
<script type="text/javascript">
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query('SELECT * FROM Events', $link);
$count = 0;
echo "var events = {";
while ($row = mysql_fetch_assoc($result)) {
if ($count !== 0) echo ",";
echo "{";
echo "'eventid':'" . add_slashes($row['eventid']) . "',";
echo "'title':'" . add_slashes($row['title']) . "',";
echo "'description':'" . add_slashes($row['description']) . "',";
echo "'date':'" . add_slashes($row['date']) . "',";
echo "'time':'" . add_slashes($row['time']) . "'";
echo "}";
count++;
}
echo "};";
?>
$calendar = new Calendar(events);
</script>
Of course, there are a number of ways that this could be done, but this is one way it could flow. This is only an example; dropping this code into a PHP page on a server will not work, it's only meant to demonstrate how in general it could flow.

Categories