fetching data from a database in realtime, with ajax? - php

I am fetching data from the following database:
I have arduino-box that send that data.
And display the data with this CSS & HTML code:
<div class="event">
<img src="http://dummyimage.com/80x70/f00/fff.png" alt="picture" />
<p>Room 2</p>
<p class="patient-name">Jon Harris</p>
<p class="event-text">This is a pixel. A flying pixel!</p>
<p class="event-timestamp">feb 2 2011 - 23:01</p>
</div>
.event {
display:block;
background: #ececec;
width:380px;
padding:10px;
margin:10px;
overflow:hidden;
text-align: left;
}
.event img {
display:block;
float:left;
margin-right:10px;
}
.event p {
font-weight: bold;
}
.event img + p {
display:inline;
}
.patient-name {
display:inline;
color: #999999;
font-size: 9px;
line-height:inherit;
padding-left: 5px;
}
.event-text{
color: #999999;
font-size: 12px;
padding-left: 5px;
}
.event-timestamp{
color: #000;
padding-left: 5px;
font-size: 9px;
}
Here is my PHP code:
<!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>
<title>DASHBOARD - Arduino 3</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
$con = mysql_connect("localhost","*****","***");
if(!con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("arduino_db",$con);
$result = mysql_query("SELECT * FROM events");
//Start container
echo " <div id='background_container'> ";
while($row = mysql_fetch_array($result))
{
echo "<div class='event'>";
echo "<img src='img/ev_img/red.jpg' alt='picture' />";
echo "<p>" . $row['inneboende'] . "</p>";
echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
echo "</div>";
}
//end container
echo "</div>"
mysql_close($con);
?>
</body>
</html>
The arduino box is sending data to the database.. lets say every 3sec. I dont want to press F5 every 5sec to get the new data. Should I use AJAX for this? I have read some AJAX on the net, but I haven´t find any good tuts for it.

Put this in a separate php file, e.g. getEvents.php:
<?php
$con = mysql_connect("localhost","*****","***");
if(!con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("arduino_db",$con);
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result))
{
echo "<div class='event'>";
echo "<img src='img/ev_img/red.jpg' alt='picture' />";
echo "<p>" . $row['inneboende'] . "</p>";
echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
echo "</div>";
}
mysql_close($con);
Then, load jquery and put the following in the head of the page:
<script type="text/javascript">
$(document).ready(function() {
$.get('getEvents.php', function (data) {
$('#background_container').html(data);
});
});
</script>
Note: This is not the exact code you should use, take a look at it and change it so it works correctly for you.

If you are just trying to refresh the page every 5 seconds, you should use javascript and look at setTimeout:
setTimeout("location.reload(true);",5000);
5000 is equal to 5 seconds.

Related

How to make an id stick when trying to leave a post comment

I'm having trouble on having my id stick when I'm trying to make a comment on a post on my site. So far when I click on the comment it has its id (ex. id=4), however, when I click on 'leave comment' the id doesn't stick and just says (id=) and not the number so I can't leave a comment because it doesn't know where to leave the comment.
I've tried defining a variable $comments = $_GET['id']; and putting it in the form action but that didn't seem to work. I've tried putting "?id= . $_GET['id']; in the form action as well, but that also didn't seem to work.
I have put starts next to what I think is the important part of my code
<?php
session_start();
?>
<html>
<!-- this includes the header and mysqli pages-->
<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>
<head>
</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
margin: auto;
}
</style>
<!-- have access to posting special characters and will trim unnecessary spaces-->
<?php
$userid = mysqli_real_escape_string($dbc, trim($_SESSION['user_id']));
$blogid = mysqli_real_escape_string($dbc, trim($_GET['id']));
$comment = mysqli_real_escape_string($dbc, trim($_POST['comment']));
***$comments = $_GET['id'];***
#This adds in the comment based on their ID, email,fname,lname and date they made the comment
if (($title != NULL) && ($post != NULL)){
$query = "INSERT INTO comments (comid, userid, blogid, comment, comdate) VALUES (NULL, '$userid', '$blogid', '$comment', NOW());";
$result = mysqli_query($dbc, $query);
#This displays an error if there was an error in making a comment
#EDIT: The "There was an error" never will display since my form validates if criteria was filled out or not, if it's not it makes the guest fill out that area
if ($result) {
echo "<center><strong>Thank you, the comment has been added.</strong></center><br /><br />";
$title = NULL;
$title = NULL;
}
else {
echo "<strong>There was an error! </strong>" . mysqli_error($dbc);
}
}
?>
<form action="<?php echo basename(__FILE__) . ***"?id=" . $comments;*** ?>" method="post">
<?php
if(($_POST['comment'] == NULL) && ($_SERVER['REQUEST_METHOD'] == 'POST')){
echo "<strong><center>Please fill in a comment!</center></strong><br />";
}
?>
<p><center><label><b>Leave comments here: </p></center></label></b>
<p align="center"><textarea name="comment" cols="40" rows="5">
<?php echo $_POST['comment'];
?>
</textarea>
</p>
<br />
<p align="center"><input type="submit" name="submit" value="Submit" /></p>
</form>
<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>
</body>
</html>
comments.php code:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<!-- this styles the css for my form to make it look nice -->
<style type="text/css">
body{
font-family: Arial, Gadget, sans-serif;
background-color: #FFD9D9;
}
strong{
color: red;
font-weight: bold;
}
center{
border-left: .17em dashed;
border-top: .17em solid;
border-right: .17em dashed;
border-bottom: .17em solid;
padding-left:25px;
padding-bottom: 20px;
width: 1000px;
background-color: #E1A0A0;
border-color: black;
margin: auto;
text-align: left;
}
h3 {
text-align: center;
color: red;
}
</style>
<!-- this includes the header and mysqli pages-->
<?php
include('includes/header.html');
include('includes/mysqli_connect.php');
?>
<?php
$blogid = $_GET['id'];
?>
<?php
$query = "SELECT blogid, title, post, DATE_FORMAT (postdate, '%M, %d, %Y') AS date, post FROM blogposts WHERE blogid=$blogid";
$result = mysqli_query($dbc, $query);
?>
<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($rowb = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<center><h2>" . $rowb['title'] . "</h2>" . "<b>Posted On: </b>" . $rowb['date'] . "<br/>" . '<br/>Leave Post Comment' . "\n";
if($_SESSION['user_id'] == 3) {
echo '| <a href="update.php?id=' . $row['blogid'] . '" >Update Blog Post</a> | <a href="' . basename(__FILE__) . '?id=' . $row['blogid'] . '" >Delete Blog Post</a>';
}
echo "</center>";
}
?>
<div align="center">
<h2>
Comments
</h2>
</div>
<?php
//define query
$q = "SELECT * FROM comments JOIN users USING (userid) WHERE $blogid";
$r = mysqli_query ($dbc, $q); //run query
?>
<!-- this will display the ID, fname, lname, email, comment, and date posted gathered from the MYSQL database-->
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo "<center><br/>" . "<b>Comment: </b>" . $row['comment'] . "<br /><br/>" . "<b>ID: </b>" . $row['comid'] . "<br />" . "<b>First Name: </b>" . $row['fname'] . "<br />" . "<b>Last Name: </b>" . $row['lname'] . "<br />" . "<b>Email: </b>" . $row['email'] . "<br />" . "<b>Posted At: </b>" . $row['comdate'] . "<br/></center>\n";
}
?>
<!--this includes the footer page-->
<?php
include('includes/footer.html');
?>
</body>
</html>
I would like the id to stick so I can put in a comment and it will show up on my site.
Yep, its all ok, but you have bad prompt in your code:
<a href="leavecomments.php?id=' . $row['blogid'] . '" not use $row but $rowb

how to select and echo an image from mysql

I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>

Load Google Maps on webpage using Javascript and PHP through drop down form

I am having two drop down lists on a html page. The data is coming from a mysql database and contains information like latitude, longitude and address. The user selects one item from the drop down and clicks on submit.
At this stage, I want to display a google map and put a marker at the latitude and longitude. Then, when the user selects the option from second drop down, I want to just add a marker on that map.
Currently, I am able to load the map once he clicks the submit from first drop down but all the options I tried to drop the pins are not working.
Here is the code I have achieved till now:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once('auth.php');
include ('LoginConfig.php');
include ('FetchAgentDetails.php');
include ('FetchDeliveryDetails.php');
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Delivery Management System</title>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA0Rm5aK0BYu1f_TzhjkG97cchHHlQfrQY&sensor=false">
</script>
<style type="text/css">
html {height:100%}
body {height:100%;margin:0;padding:0}
#googleMap {height:100%}
</style>
<script>
function initialize()
{
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap")
,mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<style type="text/css">
<!--
.style1 {
font-size: 20px;
font-weight: bold;
}
-->
</style>
<style type="text/css">
table.collection {width:250px;border:2px solid black;border-style: outset;border-collapse:collapse;}
table.collection tr {background-color:#fff; border-bottom: 1px #99b solid;padding:10px;}
table.collection tr:hover {background-color:#ffe;}
table.collection td {display:table-cell;border-bottom: 1px #99b solid; padding:10px;}
table.collection td a {text-decoration:none; display:table-row; padding:0px; height:100%;}
</style>
</head>
<body bgcolor="#8E8E38"
<div style="clear: right;">
<p align="left" class="style1">Welcome Delivery Manager! </p>
<img style="position: absolute; top: 0; right: 0;" src="./Images/logo.jpg" alt="Company Logo" width="90" height="60" align="middle"></img>
</div>
<p align="left">Home</p>
<hr></hr>
<!-- START Main Wrap -->
<form method="post">
<fieldset>
<div style="clear: left;float:left;">
<label for="deliveryList">Delivery Items:</label>
<select name="deliveryList" id="deliveryList">
<option value="Select delivery item" selected="selected">Select delivery item</option>
<?php
$deliveryHandler = new FetchDeliveryDetails();
$itemNameArray = $deliveryHandler->getItemNames();
foreach ($itemNameArray as $innerArray) {
if (is_array($innerArray)) {
$value = $innerArray['itemName'];
echo "<option value=\"$value\"";
if (isset($_POST['deliveryList']) && $_POST['deliveryList'] == $value)
echo 'selected';
echo ">" . $value . "</option>\n";
}
}
?>
</select>
<input type="submit" name="submit" id="submit" value="Submit"/>
</div>
<div style="clear: right;float:right;">
<label for="agentList">Avaliable Agent:</label>
<select name="agentList" id="agentList">
<option value="" selected="selected">Select agent to assign</option>
<?php
$agentHandler = new FetchAgentDetails();
$agentNameArray = $agentHandler->getAgentNames();
foreach ($agentNameArray as $innerArray) {
if (is_array($innerArray)) {
$agentId = $innerArray['agentId'];
$firstNameValue = $innerArray['firstname'];
$lastNameValue = $innerArray['lastname'];
$fullName = $firstNameValue . ' ' . $lastNameValue;
echo "<option value=\"$agentId\">$fullName</option>\n";
}
}
?>
</select>
<input type="submit" name="agentSubmit" id="agentSubmit" value="Check Location"/>
</div>
</fieldset>
</form>
<?php
if (isset($_POST['deliveryList'])) {
$selectedItemName = $_POST['deliveryList'];
$deliveryHander = new FetchDeliveryDetails();
$itemDetailsArray = $deliveryHander->getAllDeliveryDetails($selectedItemName);
foreach ($itemDetailsArray as $valuesArray) {
$itemNameValue = $valuesArray['itemName'];
$itemDescriptionValue = $valuesArray['itemDescription'];
$ownerFirstname = $valuesArray['firstName'];
$ownerLastname = $valuesArray['lastName'];
$dateAdded = $valuesArray['dateAdded'];
$deliveryDate = $valuesArray['deliveryDate'];
$deliveryAddress = $valuesArray['deliveryAddress'];
$deliveryLatitude = $valuesArray['deliveryLatitude'];
$deliveryLongitude = $valuesArray['deliveryLongitude'];
$assignedAgent = $valuesArray['assignedAgentId'];
if ($assignedAgent == 0) {
$assignedAgent = "-";
}
echo "<table border=\"1\" align=\"left\" class =\"collection\">\n";
echo "<tr>\n";
echo "<td >Item Name:<b>$itemNameValue</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Item Description: <b>$itemDescriptionValue</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Owner Name: <b>$ownerFirstname $ownerLastname</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>Date Added: <b>$dateAdded</td>\n";
echo "</tr>\n";
echo "<tr>";
echo "<td>Delivery Date: <b>$deliveryDate</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Delivery Address: <b>$deliveryAddress</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Assigned Agent: <b>$assignedAgent</td>";
echo "</tr>";
echo "</table>";
echo "<div id=\"googleMap\" style=\"width:500px;height:380px;\"></div>";
}
}
if (isset($_POST['agentList'])) {
}
?>
</body>
</html>
I almost forgot, this is my first PHP application, in fact my first web application. So please go easy on me. Point out other errors also, but please stick to the question.
Ok got it working by using iframe :) and a bit of php
Reference:
http://www.youtube.com/watch?v=HTm-3Cduafw
echo "<iframe style =\"display: block;
width: 800px;
padding-top: 2px;
height: 400px;
margin: 0 auto;
border: 0;\" width=\"425\" height=\"350\" align=\"center\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\"
src=\"https://maps.google.com/maps?f=d&source=s_d&saddr=$agent_map_url&
daddr=$map_url&hl=en&z=10&t=m&mra=ls&ie=UTF8&output=embed\"></iframe><br/>";

How to print PHP file contents in PHP? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Since I am new to PHP I want to know how to print a file content in PHP?
Actually my project consist of form from which data is choosed to print on a report but I want a print button on my form to print the report contents directly. Can anyone help please?
Here's my code .. I have used CSS, html and PHP and it has two buttons view and print
<?php
if ($_POST["submit"] == "View") {
?>
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<style type="text/css" media="all">
* {
margin:0px;
padding:0;
}
body {
margin:0px;
padding:0px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
span {
font-size:12px;
}
th, td {
width: 100mm;
height: 33.5mm;
}
table {
border-collapse:collapse;
}
</style>
<title>Hotel Label - <?php echo $_SESSION["tourname"]; ?></title>
</head>
<body>
<div style="margin:0px; padding:0; width:210mm; margin-top:12mm; margin-bottom:12mm; margin-left:0.5cm; margin-right:0cm; ">
<?php
//echo $getno;
/* } // View Tag Close
else {
ini_set("memory_limit", "50M");
ob_start();*/
?>
<style type="text/css" media="all">
<!--
body {
margin:0px;
font-family:Verdana, Arial, helvetica, sans-serif;
font-size:12px;
}
span {
font-size:12px;
}
td {
width: 100mm;
max-width: 100mm;
height: 33.5mm;
max-height:33.5mm;
top:0mm;
}
table {
border-collapse:collapse;
}
-->
</style>
<page backtop="13mm" backbottom="1mm" backleft="5mm" backright="5mm" style="font-size: 10px; font-family: Verdana;">
<?php
?>
<?php
$ctr16 = 0;
$ctr16_2 = 0;
$nctr = 0;
$ctr = 0;
$mctr = 0;
$lblctr = 0;
while ($lbl = mysql_fetch_assoc($get)) {
$ctr16++;
$ctr16_2++;
$nctr++;
$ctr++;
$lblctr++;
$getno--;
if ($ctr == 1) {
echo "<table border=\"0\" align=\"left\" style =\"width: 210mm;\">";
echo "\n <tr> \n ";
echo "\n <tr> \n ";
echo "\n <td style=\"width: 100mm; max-width: 100mm; height:33.5mm; padding-left:1mm;\" valign=\"top\"> \n";
if ($lblctr > 16) {
echo "<br>";
echo "<br>";
$lblctr = 1;
}
if ($ctr16 > 64) {
echo "<br>";
echo "<br>";
$ctr16 = 1;
}
}
else {
echo "\n <td style=\"width: 100mm; max-width: 100mm; height:33.5mm; padding-left:15mm;\" valign=\"top\"> \n";
if ($nctr > 16) {
echo "<br>";
echo "<br>";
$nctr = 1;
}
if ($ctr16_2 > 64) {
echo "<br>";
echo "<br>";
$ctr16_2 = 1;
}
}
echo "<br />". "<span><font size='3px'><b>$lbl[hotelname]</b></font>";
// echo "<font size='2px'>$lbl[hotelname]";
echo "<br />"."<br />"."<font size='2px'>Haji No.-$lbl[hajino]</font>";
echo "<br />"."$lbl[suffix] $lbl[surname] $lbl[name]$lbl[midname] <br />";
echo "<br />"."<font size='2px'><b>Room No - $lbl[roomno] Bus No - $lbl[busname]</font></b>";
echo "<br />";
echo "<br />";
echo "<br />";
echo "</span> ";
// echo "<br />";
echo "</td>";
if ($ctr == 2 || $getno == 0) {
echo '<br />';
$ctr = 0;
echo "</table>";
}
}
mysql_free_result($get);
?>
</div>
</body>
</html>
<?php
} else($_POST["submit"]== "Print"){
$filecontents = file_get_contents("hotel_label_print.php");
print $filecontents;
} ?>
You can use file_get_contents()
(http://php.net/manual/en/function.file-get-contents.php)
<?php
$homepage = file_get_contents('filename.txt');
echo $homepage;
?>

can´t fetch data from db and print it within css and html

I am trying to fetch data from the following database data:
And display the data with this CSS & HTML code:
<div class="event">
<img src="http://dummyimage.com/80x70/f00/fff.png" alt="picture" />
<p>Room 2</p>
<p class="patient-name">Jon Harris</p>
<p class="event-text">This is a pixel. A flying pixel!</p>
<p class="event-timestamp">feb 2 2011 - 23:01</p>
</div>
.event {
display:block;
background: #ececec;
width:380px;
padding:10px;
margin:10px;
overflow:hidden;
text-align: left;
}
.event img {
display:block;
float:left;
margin-right:10px;
}
.event p {
font-weight: bold;
}
.event img + p {
display:inline;
}
.patient-name {
display:inline;
color: #999999;
font-size: 9px;
line-height:inherit;
padding-left: 5px;
}
.event-text{
color: #999999;
font-size: 12px;
padding-left: 5px;
}
.event-timestamp{
color: #000;
padding-left: 5px;
font-size: 9px;
}
Here is my PHP code:
<!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>
<title>DASHBOARD - Arduino 3</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
$con = mysql_connect("localhost","*****","***");
if(!con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("arduino_db",$con);
$result = mysql_query("SELECT * FROM events");
//Start container
echo " <div id='background_container'> ";
while($row = mysql_fetch_array($result))
{
echo "<div class='event'>";
echo "<img src='img/ev_img/red.jpg' alt='picture' />";
echo "<p>" . $row['inneboende'] . "</p>";
echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
echo "</div>";
}
//end container
echo "</div>"
mysql_close($con);
?>
</body>
</html>
All I get is a blank page, and I cant understand why.
You're missing a semicolon here (I added it):
//end container
echo "</div>";
You should remove the quotes around the $row array variables on these lines, like this:
echo "<p class='patient-name'>" . $row['overvakare'] . "</p>";
echo "<p class='event-text'>" . $row['handelse'] . "</p>";
echo "<p class='event-timestamp'>" . $row['tid'] . "</p>";
With these changes, it works (I tested it).
I suppose that's the one upside of you having posted your actual connection details :)
As mentioned in the comments, you should now definitely change your password.
Also, during development, you should make sure error reporting is enabled, see this answer for various ways to do this.
row['overvakare'], $row['handelse'], and $row['tid'] should not be enclosed in double quotes. Since they are the only variable in that string, just remove the enclosing double quotes.
echo "</div>" needs a ; on the end.
The script executes (and displays data) after fixing those errors.
A few things:
If mysql_query fails your loop won't be entered
you're using mysql_fetch_array but trying to access as an associative array. You want mysql_fetch_assoc
From a security standpoint, you shouldn't echo out database values as is if they are obtained from user input. This can lead to nasty things like xss injection. Make sure to use things like strip_tags and other filtering functions to be safe.

Categories