Show more info on database item - php

I need help with a php page that shows information from a mysql database concerning buildings around a town square. I want it set up so that the addresses are the only thing displayed at first. Then when someone clicks on a certain address it shows them more information on that particular building.
I am new to PHP. There are two solutions I know will work but I don't want to go that route unless I must.
Those two solutions are
create a page for each building and link each address to the specific page and
Insert each database item into the page (instead of having a PHP loop) and hidden div that could be toggled for each address.
The code I have right now (and it works) is for displaying the address:
echo "<p><h3> " . stripslashes($rowBuildings[building_address]) . "</h3><br>\n";
But how do I display the rest of the building info if (and only if) they click on the building address? Sorry if this is a broad topic. I've read several forums but with no luck. My problem isn't getting the info from the database.

I found the fix to my dilemma . . .
I was using the Javascript to Toggle a div (that was fairly simple). My problem was toggling each separate div as they were added by the PHP script (according to the number of database entries). I couldn't figure out how to have incremental div id names for both the link and the div. Example:
<!--This was the link to toggle the div -->
<h3>Address</h3>
<!--This was the div to toggle -->
<div id="divid">Hello</div>
My PHP was inserting multiple divs and links though so when you would click on an address all the divs named "divid" would appear. To fix the problem I added a PHP variable to each div id such as:
$uniqueID = 0;
$PleaseWork = 0;
I then placed at the bottom of the php code:
$uniqueId++;
$PleaseWork++;
This allowed me to place variables in the link and the div id that would count consistently together. So altogether, here are the codes:
Java to toggle
> <script type="text/javascript">
>
> function toggleMe(a){ var e=document.getElementById(a); if(!e)return
> true; if(e.style.display=="none"){ e.style.display="block" } else{
> e.style.display="none" } return true } </script>
PHP CODE - THE LINK & Visible Items
<div class="building" align="left" style="margin-left:100px;">
<?
{
$uniqueID = 0;
$PleaseWork = &$uniqueID;
}
$selectAddress ="SELECT * FROM `buildings` order by building_address";
$resultAddress = mysql_query($selectAddress);
while($rowBuildings = mysql_fetch_array($resultAddress)){;
echo "<p><a id=\"displayText\" href=\"#\" onclick=toggleMe(\"divn$PleaseWork\");><h3> " . stripslashes($rowBuildings[building_address]) . "</a></h3><br>\n";
echo "<b>For Sale or Rent:</b> " . $rowBuildings[building_saleorrent] . "";
if(!empty($rowBuildings[building_permonth]))
echo "<b>Rent Per Month: </b>" . $rowBuildings[building_permonth] . "";
if(!empty($rowBuildings[building_saleprice]))
echo "<b>Sale Price: </b>" . $rowBuildings[building_saleprice] . "";
echo "<br>\n";
PHP CODE - The hidden DIV
echo "<div id=\"divn$uniqueID\" style=\"display:none\"><b>Is the Property Listed with a Realtor? </b> " . $rowBuildings[building_realtor] . "<br>\n";
if(!empty($rowBuildings[building_target]))
echo "<b>Best Suited for Building: </b>" . $rowBuildings[building_target] . "<br>\n";
echo "<b>Owner: </b>" . $rowBuildings[building_owner] . "";
if(!empty($rowBuildings[building_ownphone]))
echo "<b> Phone: </b>" . formatPhone($rowBuildings[building_ownphone]) . "";
if(!empty($rowBuildings[building_ownemail]))
echo "<b> Email: </b>" . $rowBuildings[building_ownemail] . "";
echo "<br>\n";
echo "<b> Is the building occupied? </b>" . $rowBuildings[building_isoccupant] . "<br>\n";
if(!empty($rowBuildings[building_occupant]))
echo "<b>Current Occupant:</b>" . $rowBuildings[building_occupant] . "<br>\n";
if(!empty($rowBuildings[building_occupantphone]))
echo "<b>Occupant Phone:</b>" . formatPhone($rowBuildings[building_occupantphone]) . "<br>\n";
echo "<b>Utilities: </b>" . $rowBuildings[building_utilities] . "<br>\n";
echo "<b>Stories: </b>" . $rowBuildings[building_stories] . "<br>\n";
echo "<b>Total Sq. Footage: </b>" . $rowBuildings[building_square] . "<br>\n";
echo "<b>Footage Breakdown:</b><br> ";
if(!empty($rowBuildings[building_residential]))
echo "<b>Residental: </b>" . $rowBuildings[building_residential] . " ";
if(!empty($rowBuildings[building_lightindustry]))
echo "<b>Light Industrial: </b>" . $rowBuildings[building_lightindustry] . " ";
if(!empty($rowBuildings[building_commercial]))
echo "<b>Residental: </b>" . $rowBuildings[building_commercial] . " ";
echo "<br>\n";
echo "<b>Storage: </b>" . $rowBuildings[building_storage] . " <b>Storage Sq. Footage: </b>" . $rowBuildings[building_storefoot] . "<br>\n";
echo "<b>Inside of Building: </b>" . $rowBuildings[building_inside] . "<br>\n";
echo "<b>Outside of Building: </b>" . $rowBuildings[building_outside] . "<br>\n";
echo "<b>Parking: </b>" . $rowBuildings[building_parking] . "<br>\n";
if(!empty($rowBuildings[building_issues]))
echo "<b>Issues With the Building: </b>" . $rowBuildings[building_issues] . "<br>\n";
if(!empty($rowBuildings[building_features]))
echo "<b>Main Features of the Building: </b>" . $rowBuildings[building_features] . "<br>\n";
if(!empty($rowBuildings[building_notes]))
echo "<b>Notes on the Building: </b>" . $rowBuildings[building_notes] . "<br>\n";
echo "</div></p>";
$uniqueId++;
$PleaseWork++;
}
?>
I used various sites to gather this information. Here are some:
http://www.dynamicdrive.com/forums/showthread.php?41829-Toggle-Div-in-PHP
http://php.net/manual/en/language.references.unset.php
Sorry if this is confusing. Thanks for all your help!

Related

How to list all value in array one by one for many items from json URL

<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.

HTML & PHP Information Table

So i'm hosting servers and trying to make a table displaying the information server name players etc i have a script that displays this but i suck at css, What i'm trying to get is a little like in this website http://echelon.kthx.at/pubbans.php
<?php
include ("BC2Conn.php");
// opens a connection to gameserver
$BC2Conn = new BC2Conn("31.185.143.136", 48888);
if (!$BC2Conn->isConnected()) {
echo "Connection could not be established. " .
"To debug, set '-d' as 3rd parameter to new connection.<br />" .
"<br />" .
"Example: \$BC2Conn = new BC2Conn(\"127.0.0.1\", 48888, \"-d\");";
return 0; // stop executing this script
}
// secure login
// $BC2Conn->loginSecure("password");
// unsecure login (not salted)
// some random serverinformation
echo "Servername: " . $BC2Conn->getServerName() . "<br />";
echo "Players: " . $BC2Conn->getCurrentPlayers() . "/" . $BC2Conn->getMaxPlayers() . "<br />";
echo "Playmode: " . $BC2Conn->getCurrentPlaymodeName() . "<br />";
echo "Current Map: " . $BC2Conn->getCurrentMapName() . "<br /><br /><br /><u>Players:</u><br /><br />";
// playerlist
$playerNames = $BC2Conn->getPlayerlistNames();
foreach ($playerNames as $key => $content) {
if ($BC2Conn->getPlayerClantag($content) != "") {
echo "[" . $BC2Conn->getPlayerClantag($content) . "]";
}
echo " " . $BC2Conn->getPlayername($content) . " - Kills: ";
echo $BC2Conn->getPlayerKills($content) . " | Deaths: ";
echo $BC2Conn->getPlayerDeaths($content) . " | Score: ";
echo $BC2Conn->getPlayerScore($content) . "<br />";
}
// logout
$BC2Conn->logout();
?>

Input regarding this PHP script to query a mysql database?

I was using this script and everything was great except for the fact that when the actual search was executed and "No Results" was the answer, I wanted the script to display such.
When doing research to see where the FAIL was I discovered that I should be using MySQLi. I have been at this script for 2 days and I seem to be getting further instead of closer. A little help here fellas?
What I am using:
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
include 'dbclose.php';
}
mysql_close($con);
You can retrieve the count of the rows with:
mysqli_num_rows($page_query);
Simply verify that it is >0 to chose what to display, the error message or the results
Just add a condition to display something if your query returns 0
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
if (mysqli_num_rows($page_query) > 0){
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
} else {
echo 'Your search returned 0 results';
}
include 'dbclose.php';
}
mysql_close($con);

Using a GET Id from mysql query

I am trying to run a mysql query within some php and have the results echoed as HTML. I got it working but now I would like to insert an id into the links so I can use a page to GET the id. Does anyone know how to do this. What I have tried (and is not working) is below. And at the bottom of the post is what was working originally, but without an id on the link...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
$uniqueid = $_GET["$row['User_ID']"];
echo $uniqueid;
This is from the first page that worked...
<?
echo "<tr bgcolor=\"#CCCCCC\" style=\"border-bottom:1px solid gray;\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr bgcolor=\"#F0F0F0\"><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . "$row[User_ID]" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
The problem that you are having is that you are trying to access an array element from within double quotes. You could make this work by wrapping it in curly braces {$row['User_ID']}.
To make your code more readable, and to avoid this problem, just concatenate or use a list of values for echo. I also recommend the usage of htmlspecialchars() to ensure you are creating valid HTML.
echo '<tr><td>',
'<a href="2012week1.php?id=',
htmlspecialchars($row['User_ID']),
'">',
htmlspecialchars($row[User_ID]),
'</a>',
'</td><td>'
//etc.

Problems passing PHP string to javascript function

Im having problems passing PHP Strings to Javascript functions, I'v read a number of the post regarding this problem and tried several methods but none of them have worked for me.
basically I have two functions one written in PHP that pulls information from a database and the other written in Javascript designed to allow me to geocode an address for google maps and pass some info to be added to the info window, the two bits of code are shown below:
PHP
try {
$bubbleData = $dbConnection->getBubbleData();
} catch (Exception $e) {
echo "The following error occoured while attempting to get Google map info window data" .
" " . $e->getMessage() .
" " . "In file" .
" " . $e->getLine() .
" " . "on line" .
" " . $e->getLine();
}
if (mysql_num_rows($bubbleData) == 0) {
echo "No Placement data found to populate map";
} else {
while ($row = mysql_fetch_array($bubbleData)) {
$companyName = $row['Company_Name'];
$title = $row['Title'];
$address_1 = $row['Address_Line_1'];
$address_2 = $row['Address_Line_2'];
$address_3 = $row['Address_Line_3'];
$address_4 = $row['Address_Line_4'];
$post_Code = $row['Post_Code'];
$forename = $row['Forename'];
$surname = $row['Surname'];
$fullAddress = $address_1 . " " . $address_2 . " " . $address_3 . " " . $address_4 . " " . $post_Code;
$partAddress_1 = $address_1 . " " . $address_2;
$partAddress_2 = $address_3 . " " . $address_4;
$infoText = "<h4>" . $title . "</h4>" .
'<b>' . "Company name:" . '</b>' . " " . $companyName .
"</br>" .
"<b>" . "Employee name:" . '</b>' . " " . $forename . " " . $surname . '</br>' .
"<b>" . "Company address:" . "</b>" . " " . $partAddress_1 . '</br>' .
$partAddress_2 . '</br>' . $post_Code;
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
</SCRIPT>'";
}
}
Javascript
function geocodeAddress (address,infoText) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
loadMarker(results[0].geometry.location,infoText,address);
latlngArray.push(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " +" "+ status);
}
});
}re
Now I know that the two work fine individually but whenever I try to call the javascript in side the PHP and pass it the variables I ge the following error in firebug:
Invalid value for property address:
I've been trying to make this happen all day, Ive used regexs plane echos and the json_encode() function and nothings worked can anyone help?
Thanks in advance
You have nested <?php ?> inside PHP code, which will not be interpreted as PHP. Rather, it just prints into your output as a string.
// Instead of
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(<?php echo json_encode($fullAddress);?>,<?php echo json_encode($infoText);?>);
</SCRIPT>'";
// Change to
echo "<SCRIPT LANGUAGE='javascript'>
geocodeAddress(" . json_encode($fullAddress) . "," . json_encode($infoText) . ");
</SCRIPT>'";
At least in my case, I have noticed that
<?php ...foo... ?>
segments in the HTML body, get php-evaluated only if the file extension is .php.
That is, I had to rename:
mv foo.html foo.php
to have php-values passed into javascript variables.

Categories