create google maps url in php - php

I Have a database with latitude and longitude values, I´m trying to write a PHP file that gets these values and then create links to google maps, so when I run the PHP file the values can be clicked pointing to google maps with the latitude and longitude values.
I never worked with PHP before and I cannot make it work, and I will be very thankful if anyone here can help me, please.
Here is the code I´ve written so far:
$result = mysqli_query($con,"SELECT * FROM user");
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>message</th>
<th>location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "<td>" . '<a href="http://maps.google.com/maps?z=12&t=m&q=loc:', urlencode $row['location'], '">' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

the error code is telling you whats up. Look at line 35. You arent concatenating correctly. You need to change
echo "<td>" . '<a href="http://maps.google.com/maps?z=12&t=m&q=loc:', urlencode $row['location'], '">' . "</td>";
To
echo '<td><a href="http://maps.google.com/maps?z=12&t=m&q=loc:'. urlencode($row['location']) . '"></td>';
Have a read up on String Operators

Related

PHP MYSQL - $_GET and Query Issue

I have a page which contains a table. The 'change_id' field contains hyperlinks which direct the user to a different page with an overview of that change_id's details.
'<td>' . $row['change_id'] . '</td>';
Now, just to test that the change_id is being received on the home2.php page, I used the following code:
<?php
include 'config.php';
$change_id=$_GET['change_id'];
print_r($_GET);
?>
This test successfully displayed the correct change id's:
Array ( [changeid] => 1006 )
Now, when I go to query the SQL Database using the change_id it doesn't work as desired.
<?php
include 'config.php';
$change_id=$_GET['change_id'];
$query1 = "SELECT * FROM `change_request_tbl` WHERE `change_id` = $change_id";
$result = mysqli_query($conn, $query1);
echo "<fieldset><legend><strong>New Requests:</legend></strong>
<table border=4 bordercolor=black class=table>
<tr>
<th>Change ID:</th>
<th>Customer Name:</th>
<th>Change Requestor:</th>
<th>Date CR raised:</th>
<th>CPM/Ticket:</th>
<th>Out of Hours:</th>
<th>Change Category:</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['change_id'] . "</td>";
echo "<td>" . $row['customer_name'] . "</td>";
echo "<td>" . $row['change_requestor'] . "</td>";
echo "<td>" . $row['date_cr_raised'] . "</td>";
echo "<td>" . $row['cpm_ticket'] . "</td>";
echo "<td>" . $row['out_of_hours'] . "</td>";
echo "<td>" . $row['category_of_change'] . "</td>";
echo "</tr>";
}
echo "</table></fieldset><br><br><br>";
?>
The table are headers are shown without any data. Any ideas on how to fix? Thanks in advance
You are setting $change_id to $_GET['change_id']
However you are passing $_GET the parameter name of changeid
If you change
$change_id = $_GET['change_id'];
To
$change_id = $_GET['changeid'];
It should work as expected :)

For loop not working in PHP script

I have written a PHP script to pull data from a SQL database to display on a webpage. Everything seems fine but when I run the script it throws the error:
Parse error: syntax error, unexpected ')', expecting ';' in db_data.php on line 21
Line 21 is my FOR loop, I have checked the syntax of the loop and it seems to be correct so I can't understand why it is failing.
$result = mysqli_query($con,"SELECT * igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
for($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Change for to while
<?php
// Missing FROM here vv
$result = mysqli_query($con,"SELECT * FROM igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
As Anthony Thompson and RJParick says, you have to change for to while. The error you're encountering is due tu for that need 3 params like this :
for($i; $i<10; $i++) {//Note the ; used to separate params.
//do something
}
So your loop would begin like this :
while($row = mysqli_fetch_array($result)){
Another detail, your SQL isn't correct use SELECT * FROM igi instead.

How can I retrieve specific information from my sql query based on what information is filled in the HTML form fields?

I am brand new to PHP and everything else, so please try to keep any response as simple as possible. Thank you. I just can't figure out how to only get the information from the field that is filled in. When I do my search, it brings up the empty fields as well. I need just the information in the HTML form to be found. For example, if I put in a project code, I need all information that pertains to that code. Or if I put in a specific budget, I need everything that has that budget. I hope this makes sense...I'm such a rookie, I don't even know how to word the question!!!! I have tried using !is_null, (isset), !empty but none of them seem to work.
<?php
include 'connect.php';
// Get values from form
$p_code=$_GET['projectcode'];
$p_name=$_GET['projectname'];
$budget=$_GET['budget'];
$s_date=$_GET['startdate'];
$e_date=$_GET['enddate'];
$c_year=$_GET['cyear'];
$c_qtr=$_GET['cqtr'];
$c_grp=$_GET['cgrp'];
$sponsor=$_GET['sponsor'];
$client=$_GET['client'];
$p_lead=$_GET['projectlead'];
$orig_hrs=$_GET['originalhrs'];
$risk_per=$_GET['riskpercent'];
$notes=$_GET['notes'];
if ($db_found) {
$SQL = "SELECT * FROM minimodtable
WHERE projectcode='$p_code'
OR projectname='$p_name'
OR budget='$budget'
OR startdate='$s_date'
OR enddate='$e_date'
OR cyear='$c_year'
OR cqtr='$c_qtr'
OR cgrp='$c_grp'
OR sponsor='$sponsor'
OR client='$client'
OR projectlead='$p_lead'
OR originalhrs='$orig_hrs'
OR riskpercent='$risk_per'
OR notes='$notes'";
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);
?>
You've made a good first try there. This method should save you having to repeat yourself so much, though. You were correct to start looking at isset - the issue is that you need to dynamically build up your SQL query.
In fact, you'll probably want to change the glue in the implode() function from 'OR' to 'AND' - that way, you can find things that are a specific budget AND that pertain to a certain code.
I've switched it to AND for now - but feel free to switch it back to OR - as per your example.
<?php
// All possible parameters
$params = array(
'projectcode',
'projectname',
'budget',
'startdate',
'enddate',
'cyear',
'cqtr',
'cgrp',
'sponsor',
'client',
'projectlead',
'originalhrs',
'riskpercent',
'notes'
);
$wheres = array();
foreach ($params as $param) {
// Is the param set?
// If so, let's add it to our list of WHERE clauses
if (isset($_GET[$param])) {
$wheres[] = sprintf("%s = '%s'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
}
}
if ($db_found) {
// Now let's make the SQL.
$SQL = 'SELECT * FROM minimodtable';
// We only want to add the WHERE clause if we had some parameters passed
if (count($wheres) > 0) {
$SQL .= ' WHERE ' . implode(' AND ', $wheres);
}
$result = mysqli_query($connect, $SQL);
echo "<table border='1'>
<tr>
<th>Project Code</th>
<th>Project Name</th>
<th>Budget</th>
<th>Start Date</th>
<th>End Date</th>
<th>Year</th>
<th>Quarter</th>
<th>Group</th>
<th>Sponsor</th>
<th>Client</th>
<th>Project Lead</th>
<th>Original Hours</th>
<th>Risk Percent</th>
<th>Notes</th>
</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['projectcode'] . "</td>";
echo "<td>" . $row['projectname'] . "</td>";
echo "<td>" . $row['budget'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['enddate'] . "</td>";
echo "<td>" . $row['cyear'] . "</td>";
echo "<td>" . $row['cqtr'] . "</td>";
echo "<td>" . $row['cgrp'] . "</td>";
echo "<td>" . $row['sponsor'] . "</td>";
echo "<td>" . $row['client'] . "</td>";
echo "<td>" . $row['projectlead'] . "</td>";
echo "<td>" . $row['originalhrs'] . "</td>";
echo "<td>" . $row['riskpercent'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
mysqli_close($connect);

PHP Button on each row of table

im making some application form in PHP.
im putting all info returned from the database into a table.
Now i want to create a button on each line that changes something in the DB of that line.
but i have no idea to do that :S
Thank you!
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<input type="submit" name="submit" value="accept">' . "</td>";
echo "</tr>";
}
echo "</table>";
EDIT: get it working using another script:
echo "<td>Approve</td>";
and edit.php:
<?php
include("dbconnect.php");
$member_id = $_GET['id'];
$status = $_GET['status'];
echo $member_id;
echo $status;
if ($status == 'app')
$query = "update apps set status = 'approved' where id = $member_id";
mysql_query($query) or die (mysql_error());
?>
Create small form with parameters in the cell you want the button to do smth. This is the simpliest approach (approach with refresh).
One more solution is to use AJAX on button click and forward action to some endpoint. This way it would be dynamic and probably what you are want to implement.
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<form type="POST"><input type="hidden" name="whatever" value="$row['id']"><input type="submit" name="submit_btn" value="accept"></form>' . "</td>";
echo "</tr>";
}
echo "</table>";
in this way, you could get a form in each row of the table. Now, you just need to use php POST function.
if(isset($_POST['submit_btn']))
{
//whatever u need to do
}

Putting a link in a php/html table which uses mysql

can anyone help?
Apologies if I'm being stupid but I'm very new to this and I don't really know what I'm doing...
I have generated a table which uses php to get data from a mysql database, but I want to put an extra column at the end (of every row) which contains a link. The link will be to further details about that entry.
The code I have which displays the table is as follows:
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "</tr>";
}
echo "</table>";
Like I say, I'm a beginner. Basically I am reasonably happy (using the code above) in making html tables which display the results of a mysql query. However I need the users to be able to click on rows/cells in order link to other tables.
Any help much appreciated...
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<!-- extra column here -->
<th> </th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
// add link here
echo "<td><a href=''>link</a></td>";
echo "</tr>";
}
Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
try this. add a column. It will link to another page "view_more_details.php". To identify rows uniquely, pass a unique id with this link. Here I passed order_no. In the
"view_more_details.php" page, you can select this value by using $_GET['key']
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<th> </th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "<td>" . "View More" . "</td>";
echo "</tr>";
}
echo "</table>";

Categories