How to style data from mysql database? - php

I got this PHP code to display comments from database:
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("cannot connect: " . mysql_error());
}
mysql_select_db("strek", $con);
$sql="SELECT * FROM comments";
$result=mysql_query($sql, $con);
while($row=mysql_fetch_array($result)){
$name=$row['name'];
$comment=$row['comment'];
echo $name. "<br>" .$comment;
echo"<br>";
}
?>
I'd like both name and comment to be aligned in the center and I'd like name to be BOLD.
Please help me how to do that.

"I'd like both name and comment to be aligned in the center and I'd like name to be BOLD. Please help me how to do that."
Since the OP already has double quotes in echo $name. "<br>" .$comment; in what I think is already working and needs to have it centered and in bold, the use of my code below is perfectly valid.
Consider the following:
echo "<div align='center'><b>" . $name. "<br>" .$comment . "</b></div>";
A working example:
<?php
$name = "John";
$comment = "This is a comment";
echo "<div align='center'><b>" . $name. "<br>" .$comment . "</b></div>";
?>
You could also make use of a stylesheet with either a DIV ID, and/or a CLASS.
For example:
echo "<div align='center' id='names_comments' class='centered bold_text'>" . $name. "<br>" .$comment . "</div>";
Then using CSS:
#names_comments {
font-family:Georgia;
font-size:12pt;
}
.centered bold_text {
text-align:center;
font-weight:bold;
}
Note: align='center' could be omitted, but it won't hurt to keep it.

while($row=mysql_fetch_array($result)){
<span style="display: block;">
<span style="font-weight: bold; width: 100px; display: inline-block;">
<?php echo htmlspecialchars($result['name'], ENT_QUOTES);?>
</span>
<span style="text-align: center; width: 200px; display: inline-block;">
<?php echo htmlspecialchars($result['comment'], ENT_QUOTES);?>
</span>
</span>
}
Function htmlspecialchars - convert special characters to HTML entities. With flag ENT_QUOTES escape quotes in order to avoid broke HTML code.

Try this.
while($row=mysql_fetch_array($result)){
<span style="text-align:center">
<b>
<?= $row['name']?>
</b>
<?= $row['comment']?>
</span>
}

Related

How to change height of a <br> tag in HTML?

I want to change height for a <br> tag.
This code written inside HTML tag which I have tried:
echo "<br style="padding-top:xxpx;">"
But, it doe not work:
echo " $x" . " Degree"."<br>"."<br>"."<br>"."<br>";
echo " $y" . " percent"."<br>"."<br>"."<br>";
echo " $a" . " percent"."<br>"."<br>"."<br>"."<br>";
echo " $b" . " percent"."<br>"."<br>"."<br>"."<br>";
echo " $c" . " percent"."<br>"."<br>"."<br>"."<br>";
echo " $d" . " percent";
header("Refresh:5");
//}else {
//echo "No Results";
//}
//$conn->close();
?>
</div>
As the 'margin' doesn't work in Chrome, that's why I used 'border' instead.
br {
display: block;
content: "";
border-bottom: 10px solid transparent; // Works in Chrome/Safari
}
#-moz-document url-prefix() {
br {
margin-bottom: 10px; // As 'border-bottom' doesn't work in firefox and 'margin-bottom' doesn't work in Chrome.
}
}
This is a tiny CSS for answering your question:
br
{
display: block;
margin: 10px 0;
}
But, I suggest you use <div> for formatting your layout, instead of using <br> tag.
Changing height of <br> is not appreciated. Place <br> element inside a <p> tag and apply line-height to the <p> element
echo '<p style="line-height:30px;margin:0px;"><br></p>
or simply the best try this
echo"<p style='margin:0px;'line-height:20px;>Your content Here</p>";

Filepath from SQL database not looping a BG image

I have an Image uploaded to a folder with the filepath stored in a table along with other info (title, description). I would like this info looping inside a repeated div. Both other variables loop correctly, however the filepath variable needs to be used in a background URL and at the moment only echos the value of the last row. Thank you very much for your help, there has got to be a simple solution! -Sean
<?php
$result = mysql_query("SELECT * FROM `program`");
$values = mysql_fetch_array($result);
$globals['filepath'] = $row['filepath'];
echo "<div>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<div class=wrapper3 >
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p> </br> </p>";
$globals['bgimage'] = $row['filepath'];
}
echo "</div>";
?>
<style type="text/css">
.wrapper3{
width:85%;
margin:0 auto;
padding:20px;
height:auto;
color:#FFF;
background: url(/SMLC/<?php while ($row = mysql_fetch_array($result)){ echo $globals['bgimage'];} ?>
) no-repeat;
background-size:cover;
color:#000;
height:250px;
text-align:center;
background-color:#fff;
border-radius:6px;
border:1px solid #0FF;
}
</style>
<?php mysql_close();?>
Remove the background from the .wrapper3 in css, and add it to the element as an inline style.
<?php
$result = mysql_query("SELECT * FROM `program`");
echo "<div>"; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop through results
$globals['filepath'] = $row['filepath'];
echo "<div class='wrapper3' style=\"background: url('/SMLC/".$row["filepath"]."');\">
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p></br></p>";
}
echo "</div>";
?>
NOTE: Use mysqli functions or PDO instead of mysql functions, since, mysql functions are deprecated.

Need help displaying images from mysql to webpage

I have stored images in phpmyadmin in a BLOB format. When I try to display them on my webpage, weird symbols and letters show up. The pictures are a mix of jog and png. Please help.
Here is the code that I currently have.
<?php
session_start();
include 'dbConn.php';
if (!isset($_SESSION['username'])){
header("Location: clientLogin.php");
}
$query = "SELECT * FROM ip_games ";
$stmt = $dbConn->query($query);
$result = $stmt->fetchAll();
?>
<style>
body {
background-color: grey;
}
table {
margin: 0 auto;
}
td {
padding-right: 30px;
}
</style>
<html>
<body>
<div>
<form action="welcome.php">
<input type="submit" value="Sign out" />
</form>
</div>
<h1>Happy Face Emoji Games</h1>
<form>
Name: <?php echo $_SESSION['name'] ?> <br />
</form>
<table>
<?
for ($i=0; $i<count($result); $i++)
{
echo "<tr>";
echo "<td>" . $result[$i]["picture"] . "<br />" . $result[$i]["name"]. " <br /> " .$result[$i]["console"] . " <br />" . "$" . $result[$i]["cost"] . "</td>";
//echo "<td>".$result[$i]["console"]."</td>";
//echo "<td>".$result[$i]["description"]."</td>";
//echo "<td>".$result[$i]["cost"]."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
To directly use the binary data as a an image source, you can use the data URI scheme, for example:
$uri = 'data:image/png;base64,'.base64_encode($row['binary-data']);
This URI can then be used directly as the image’s source:
background-image: url(<?php echo $uri; ?>)
<img src="<?php echo $uri; ?>">
not good practice, older browser issues, load issues and catching issues. cheer
You need to wrap your image in an <img> tag.
<img src="<?php echo $result[$i]["picture"]; ?>" />
Give that a try and let me know :).
Edit: I wrote that as if it was in the page, for your question you probably want:
echo "<td><img src='" . $result[$i]["picture"] . "' />" . "<br />"... etc.

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>

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