I would like to get the postal code by enter address, I have tried google auto complete place search, But it gives wrong postal code for some places.
Give any idea to get the correct postal code by the given address?
Thank you,
Try below code this may be helps to you:
// Decode json
$decoded_json = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false'));
foreach($decoded_json->results as $results)
{
foreach($results->address_components as $address_components)
{
// Check types is set then get first element (may want to loop through this to be safe,
// rather than getting the first element all the time)
print_r($address_components);
if(isset($address_components->types) && $address_components->types[0] == 'postal_code')
{
// Do what you want with data here
echo $address_components->long_name;
}
}
}
Use below code it will help you.
<?php
$db = mysqli_connect("localhost","root","") or die("Could not connect");
mysqli_select_db($db, "search_db") or die("No database");
$textbox = "";
$out = "";
$title = "";
if (isset($_GET['search'])) {
$searchq = $_GET['search'];
$query = mysqli_query($db, "SELECT * FROM `pincodes_db` WHERE `pincode` LIKE '%$searchq%' OR `divisionname` LIKE '%$searchq%' OR `statename` LIKE '%$searchq%' OR `regionname` LIKE '%$searchq%' OR `officename` LIKE '%$searchq%' OR `circlename` LIKE '%$searchq%'" ) or die("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = 'There was no records found';
}else{
while($row = mysqli_fetch_array($query)){
$pincode = $row['pincode'];
$officename = $row['officename'];
$Deliverystatus = $row['Deliverystatus'];
$divisionname = $row['divisionname'];
$Taluk = $row['Taluk'];
$RelatedHeadoffice = $row['RelatedHeadoffice'];
$RelatedSuboffice = $row['RelatedSuboffice'];
$circlename = $row['circlename'];
$regionname = $row['regionname'];
$contactno = $row['Telephone'];
$out .= '<div style="border-bottom:1px dotted green;">
<h2 style="font-size:18px;"><a href="search-indian-postal-code-in-map.php?postalcodeof='.$pincode.'"/><b><font color="green">Postal code of :</font></b><b><font color="green">'.$pincode.'</font></b> <b><font color="green">'.$officename.'</font></b> <b><font
color="green">'.$regionname.'</font>,</b> <b><font color="green">'.$circlename.'</font>,</b> <b><font color="green">'.$contactno.'</font></b></a></h2>
<table><tr><th>Description</th><th>Result</th></tr><tr>
<td>Pincode</td>
<td>' .$pincode.'</td></tr>
<td>Office Name</td>
<td>' .$officename. '</td></tr>
<td>Delivery system</td>
<td>' . $Deliverystatus. '</td></tr>
<td>Division Name</td>
<td>' . $divisionname. '</td></tr>
<td>Taluk</td>
<td>' . $Taluk. '</td></tr>
<td>Related Head Office</td>
<td>' . $RelatedHeadoffice. '</td></tr>
<td>Related Sub Office</td>
<td>' . $RelatedSuboffice. '</td></tr>
<td>Circle Name</td>
<td>' . $circlename. '</td></tr>
<td>Region</td>
<td>' . $regionname. '</td></tr>
<table/></div>';
}
}
}
?>
i have used same code for my website Please refer
http://www.myworkbook.in/indian-postal-code-search/indian-states.php
Related
My problem is when I enter a specific country name, for example: France, it'll output every data from my database instead of just France. I don't know where I've gone wrong and its probably something very simple but I don't even know how to attempt to fix it so I've come here to get some help
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$country = $_POST['country'];
$_SESSION['country'] = $country;
$sqlQuery = "SELECT * FROM campsites WHERE country LIKE '%$country%'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
//var_dump($result);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td></td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
my Database class
class campDataSet
{
public $dbHandle, $dbInstance;
public function __construct()
{
$this->db = new campData();
$this->conn = $this->db->getCampData();
}
public function fetchAllCamps()
{
//$sqlQuery = "SELECT campsites.id_campsite, campsites.campsite_name, campsites.address, campsites.postcode, campsites.country, campsites.lattitude, campsites.longitude, campsites.email, campsites.phone_number
// FROM sgb220_clientserver.campsites";
$sqlQuery = "SELECT * FROM sgb220_clientserver.campsites";
if ($data = $this->conn->prepare($sqlQuery)) {
$data->execute();
$dataSet = [];
while ($row = $data->fetch()) {
$dataSet[] = new DBdata($row);
}
} else {
echo "<script> alert(\"Could not prepare SQL statement\") </script>";
}
return $dataSet;
}
Your fetchAllCamps() method doesn't accept any arguments.
Instead of defining the $sqlQuery inside fetchAllCamps, use a parameter:
public function fetchAllCamps($sqlQuery) // <- This
{
if ($data = $this->conn->prepare($sqlQuery)) {
$data->execute();
$dataSet = [];
...
A warning about SQL Injection
Because you are inserting $_POST data directly into your query, the user is able to manipulate the sql and thus can extract/manipulate data however he wants to. Read up in SQL Injection and how to prevent it to keep your database safe from attackers.
This might be a good starting point: https://stackoverflow.com/a/601524/2232127
Your issue is that you are running a query that just gets all of the camps instead of only the ones in a certain country. Your fetchAllCamps() function does not accept any parameters.
It would probably be best to move your query into the fetchAllCamps() function, or make another function entirely if you need a function to give you all the camps instead o just ones in a certain country. Instead of passing in the query, just pass the $country variable. Build your query inside the function and run it.
This way you are separating all of your SQL from where you are building your HTML. This is more in line with modern programming standards.
In the first iteration of my database project for school I successfully created a page that will sort a MySQL table when clicking on the headers of each table column by using a $_GET tag. The first time around I created a separate page for each main entry in the database table I was accessing.
For the second iteration I chose to use one page that would display the same information as before but would select from the database when linking to the page using $_GET tags, instead.
I'm trying to figure out why the following code will correctly sort the table I'm displaying:
$query = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='1'";
if ($_GET['sort'] == 'username') {
$query .= " ORDER BY username";
} else if ($_GET['sort'] == 'service_date') {
$query .= " ORDER BY service_date DESC";
} else if ($_GET['sort'] == 'review_date') {
$query .= " ORDER BY review_date DESC";
} else if ($_GET['sort'] == 'review_rating') {
$query .= " ORDER BY review_rating DESC";
} else {
$query .= " ORDER BY review_date";
}
$response = #mysqli_query($db, $query);
$plumber;
if($response){
echo '<table align="left" cellspacing="5" cellpadding="8" width="50%">
<td align="left"><b>Username</b></td>
<td align="left"><b>Service Date</b></td>
<td align="left"><b>Review Date</b></td>
<td align="left"><b>Review Rating</b></td>
<td align="left"><b>Review Details</b></td>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['username'] . '</td>
<td align="left">' . $row['service_date'] . '</td>
<td align="left">' . $row['review_date'] . '</td>
<td align="left">' . $row['review_rating'] . '</td>
<td align="left">' . $row['review_details'] . '</td>';
echo '</tr>';
$plumber = $row['company_name'];
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($db);
}
When this code wont:
$query2 = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='$plumber_id'";
$response2 = #mysqli_query($db, $query2);
if ($_GET['sort'] == 'username') {
$query2 .= " ORDER BY username";
} else if ($_GET['sort'] == 'service_date') {
$query2 .= " ORDER BY service_date DESC";
} else if ($_GET['sort'] == 'review_date') {
$query2 .= " ORDER BY review_date DESC";
} else if ($_GET['sort'] == 'review_rating') {
$query2 .= " ORDER BY review_rating DESC";
} else {
$query2 .= " ORDER BY username";
}
if ($response2) {
echo '<table align="left" cellspacing="5" cellpadding="8" width="50%">
<td align="left"><b>Username</b></td>
<td align="left"><b>Service Date</b></td>
<td align="left"><b>Review Date</b></td>
<td align="left"><b>Review Rating</b></td>
<td align="left"><b>Review Details</b></td>';
while($row = mysqli_fetch_array($response2)) {
echo '<tr><td align="left">' .
$row['username'] . '</td>
<td align="left">' . $row['service_date'] . '</td>
<td align="left">' . $row['review_date'] . '</td>
<td align="left">' . $row['review_rating'] . '</td>
<td align="left">' . $row['review_details'] . '</td>';
echo '</tr>';
$plumber = $row['company_name'];
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($db);
}
The only differences between the two pages is instead of statically defining the variable in the WHERE clause of my SQL query, I pass it a value pulled from the $_GET tag and I renamed a view variables.
Adding '$query2 .= " ORDER BY var";' immediately after defining $query2 will successfully sort the table by whatever row I use for var. I've used a header('location: url') within each line of the if block I used to determine which value for $_GET['sort'] is being passed in to ensure each line is being reached and it's successfully redirecting for each one.
Anyone spot what I'm doing wrong? My professor (and therefore me too) isn't worried about proper practices like using prepared statements and the like - it just needs to be functional. I'm just trying to figure out what broke.
Sorry for the novel and if I'm breaking some rule, this is my first time posting here
Here's what the table looks like for reference
I have escaped your variable. This will fix query issues most of the time.
$query2 = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='".$plumber_id."'";
$response2 = #mysqli_query($db, $query2);
This is just a beaty suggestion:
switch ($_GET['osrt']) {
case 'something':
$query = "";
break
case 'somethingelse':
$query = "";
break;
default:
$query = "";
break;
}
I am wondering if anyone could help? I am trying to send 2 variables which I have extracted from a database to another page when the user clicks on a link. At the moment I can only send one. I know what I am doing below is wrong.....basically I want to send both uninum and groupid over to the other page.
for ($i = 0; $i < $count; $i++){
$q = "SELECT participants.sname, participants.uninum, groups.groupid FROM participants INNER JOIN groups ON participants.uninum =
groups.uninum WHERE groups.groupid ='".$groups[$i]."'";
$result = mysqli_query ($dbcon, $q); // Run the query.
if ($result) { // If it ran, display the records.
// Table header.
echo '<table>
<tr><td><b>Edit</b></td>
<td><b>Surnname</b></td>
<td><b>University ID</b></td>
<td><b>Group</b></td>
</tr>';
// Fetch and display the records:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr>
<td>Edit</td>
<td>' . $row['sname'] . '</td>
<td>' . $row['uninum'] . '</td>
<td>' . $row['groupid'] . '</td>
</tr>';
}
echo '</table>'; // Close the table.
mysqli_free_result ($result); // Free up the resources.
echo "<br><br>";
} else { // If it did not run OK.
// Public message:
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $q . '</p>';
}
}
You have used ? instead of & in your code.
<td>Edit</td>
Should be:
<td>Edit</td>
You can try this :
Edit
So Im having an issue with MySQL returning 2 rows, but only 1 displaying in variable.
Not to sure why its not returning both
$sql->bind_result($c_id, $c_location, $c_type) or die($mysqli_load->error);
while($row = $sql->fetch()){
$mysqli_load2 = new mysqli(HOST, USER, PASS, DB);
$query = "SELECT `badge` FROM `responders` WHERE `cid` = ?";
$sql2 = $mysqli_load2->prepare($query) or die($mysqli_load2->error);
$sql2->bind_param('i', $c_id);
$sql2->execute() or die($mysqli_load2->error);
$sql2->store_result();
$rows = $sql2->num_rows;
$sql2->bind_result($units);
$sql2->fetch();
$sql2->close();
$mysqli_load2->close();
echo '
<tr>
<td align="justify"><a href="viewcall.php?cid=' . $c_id .'"><u><abbr title="View Call">'.$c_id.'</abbr></u></td>
<td align="justify">' . $c_location .'</td>
<td align="justify">' . $c_type .'</td>
<td align="justify">Rows: ' . $rows . ' ' . $units . '</td></tr>';
}
You need to loop with the fetch() method:
while ($sql2->fetch()) {
echo $units;
}
$sql2->close();
So here is the new updated code for anyone who would have a question similar to this:
$sql->bind_result($c_id, $c_location, $c_type) or die($mysqli_load->error);
while($row = $sql->fetch()){
$mysqli_load2 = new mysqli(HOST, USER, PASS, DB);
$query = "SELECT `badge` FROM `responders` WHERE `cid` = ?";
$sql2 = $mysqli_load2->prepare($query) or die($mysqli_load2->error);
$sql2->bind_param('i', $c_id);
$sql2->execute() or die($mysqli_load2->error);
$sql2->bind_result($units);
echo '
<tr>
<td align="justify"><a href="viewcall.php?cid=' . $c_id .'"><u><abbr title="View Call">'.$c_id.'</abbr></u></td>
<td align="justify">' . $c_location .'</td>
<td align="justify">' . $c_type .'</td>
<td align="justify">';
while($sql2->fetch())
{
echo
'#'.$units.' '; } echo '</td></tr>';
}
$sql2->close();
$mysqli_load2->close();
$sql->close();
$mysqli_load->close();
I've created a personal messaging system using PHP and MySQL. Part of the system allows for the sending of group e-mails to people who fit within certain groups.
The code for sending the e-mail is:
session_start();
include_once('../../dbconnect.php');
$email=$_SESSION['email'];
$to = $_POST ['touser'];
$toemail = $_POST['touseremail'];
$from = $_SESSION['name'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$time = time();
if ($to == 'Level 4'){
$usersquery = "SELECT email FROM users WHERE account = 'Level4'";
$getusers = $conn -> query($usersquery);
while ($row = $getusers->fetch_assoc()){
foreach ($row as $value){
$query ="INSERT INTO messages (to_user, to_email, subject, message, from_user, from_email, daterecord) VALUES ('" . $to . "', '" . $value . "', '" . mysqli_real_escape_string($conn, $subject) . "', '" . mysqli_real_escape_string($conn, $message) . "', '$from', '$email', '$time')";
$send = $conn -> query($query);
}
}
}
This works fine, but the problem is when I open the sentbox, each individual message is displayed - when a large group has been messaged, this clogs up the sentbox and is very annoying! The code for the sentbox is:
$emailquery = "SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' ORDER BY daterecord DESC";
$sentemails = $conn->query($emailquery);
$emailcount = $sentemails ->num_rows;
if ($emailcount == 0){
echo '<div>No messages sent</div>';
}
else{
?>
<table class="udtable">
<tr>
<th class="reqhead">To</th>
<th class="reqhead">Subject</th>
<th class="reqhead"></th>
</tr>
<?
$i=1;
while ($sent = $sentemails->fetch_assoc()){
if ($i%2 != 0){
$rowclass = 'reqodd';
}
else {
$rowclass = 'reqeven';
}
echo ' <tr class = "' . $rowclass . '">
<td class="reqfrom">' . $sent['to_user'] . '</td>
<td class="reqsubj">' . $sent['subject'] . '</td>
<td class="req"><a id="link' . $sent['id'] . '" href="#" class="sentopen">Open</a></td>
<td class="reqmessage"><pre class=sentboxmessage>' . $sent['message'] . '</pre></td>
<td class="reqid">' . $sent['id'] . '</td>
</tr>';
$i++;
}
?>
</table>
<?
}
?>
The sentbox looks like this:
I'd like to consolidate all of those messages into a single line.
Any ideas?
Thanks!
You need to use grouping in your mysql select statement in your sentbox code snippet
SELECT * FROM messages WHERE from_email = '$email' AND sent_deleted = 'no' GROUP BY subject, daterecord ORDER BY daterecord DESC
This will work and will group your messages by the subject and by the daterecord. So any messages that are sent all at the same time with the same subject will be grouped together. You might need to add in more grouping options to refine if multiple people could send messages with the same subject at the same time.