Filepath from SQL database not looping a BG image - php

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.

Related

How to filter SQL data with a dropdown

I am just starting with SQL, PHP, and HTML and I plan to take some courses, but I need a basic working model before I can move forward. I have an SQL database that has a few fields. I can display this data without a problem. I would like to filter this data with a drop-down box. Currently, I have the drop-down box populated with a Select Distinct command. I can not seem to figure out the syntax to modify my SQL Select command to only show records that match my pull-down box. Ha, I will be the first to admit that I have no clue what I am doing, but my problem is that I might not know the term I need to look up how to solve my problem. Google and I are tight, but we just can't put our finger on this one. Can you point me in the right direction? Thanks!
<!DOCTYPE html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even){
background-color: #dddddd;
}
</style>
<body>
<form action='new1.php' method="get" name='fPIC'>
<select class="form-dropdown validate[required]" style="width:150px" id="input_PIC" name="sPIC">
<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");
mysqli_select_db($link,'pd4poc');
$query = "SELECT DISTINCT PIC FROM dummydata order by PIC asc";
$result = mysqli_query($link,$query);
$menu=" ";
while($row = mysqli_fetch_array($result))
{
$menu .= "<option value=".$row['PIC'].">" .$row['PIC']. "</option>";
}
echo $menu;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['PIC'] . "<td><td>" , $row['Reason'] . "<td><td>", $row['Status'] . "</td></tr>";
}
echo "</table>";
mysqli_close($link);
?>
</select>
<input type="submit" name="nPIC" value="Filter">
</form>
<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");
mysqli_select_db($link,'pd4poc');
$query = "SELECT * FROM dummydata"; //need to be picked from the pull down
$result = mysqli_query($link,$query);
echo "<table>";
echo "<tr>
<th>PIC</th>
<th>Reason</th>
<th>PlanApply</th>
<th>HOT</th>
<th>Status</th>
<th>Comments</th>
</tr>\n";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" .
$row['PIC'] . "<td>",
$row['Reason'] . "<td>",
$row['PlanApply'] . "<td>",
$row['HOT'] . "<td>",
$row['Status'] . "<td>",
$row['Comments'] . "</td>\n</tr>";
}
echo "</table>";
mysqli_close($link);
?>
</body>
</head>

Conditional PHP styling based on variable value

I have some PHP code which executes and selects 10 rows from a SQL table. One column called result can hold the value of won, or loss.
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class = 'logrow'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
}
How could I do something which would echo the values from each row as above but to echo a different statement for each row where the value of result is loss. For example to add an inline styling for the background colour.
So for example, say I have 10 rows found - 9 of these have $row["result"]as won, so they should be echoed as above. But 1 row has the value of $row["result"] as loss, a different echo should be applied. Perhaps with an inline style, or maybe with a variable inserted which hold this style.
I know this is very specific and may not be clear so thanks in advance.
Based on the value in $row["result"] (won/loss) create a class and use it in the echo
css:
.won{background-color::blue}
.loss{background-color::red}
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class = 'logrow ". $row["result"] ."'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
}
Try this by changing class names.
<?php
$status = $row['result'];
$classname = 'won';
if($status == 0){
$classname = 'fail';
}
?>
<div class = 'logrow <?php echo $classname ?> '> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
Now define classes for won and fail under style sheet.
<style type="text/css">
.won{ color: green; }
.fail{ color: red; }
</style>

How to style data from mysql database?

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>
}

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>

How do I position the content of a 'while' loop on the top right corner?

I have a list of names that are fetched from a while loop coded in PHP.
When I try to display the content of the loop which is embedded in a div, the content gets overlapped.
The style I am using for the content is:
<style>
#cap{
position: absolute;
top: 0px;
right: 0px;
}
</style>
Here is the entire code of the loop content along with style of the div in PHP:
<?php
$result2 = mysql_query("SELECT `player_name` FROM `player_data` WHERE `team_id`=$team2");
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "<div id='cap'>";
echo "" . $name2 . "<br/>";
echo "</div>";
}
?>
How do I get this content $name2 not overlapped?
Something like this:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo '<div class="player">';
echo $name2;
echo '</div>';
}
echo '</div>';
Put the div with that CSS outside the loop:
echo '<div id="cap">';
while($row = mysql_fetch_assoc($result2))
{
$name2 = $row['player_name'];
echo "" . $name2 . "<br/>";
}
echo "</div>";

Categories