mySQL blob images printout? - php

I know this has been asked before, and I know you can do it via making a seprate page for each image. But thats not ideal for what I want.
I want to do that age old thing of displaying multiple images from a db on the same page:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo "<td>" . $row['user_image'] . "</td>";
echo "<td>" . $row['user_thumb'] . "</td>";
echo "</tr>";
}
echo "</table>";
user_image and user_thumb are blob images, is there someway of showing them all on that page, perhaps setting them to a php variable and then converting to javascript or something along those lines? Rather than:
header('Content-type: image/jpg');
echo $thumb;
In a seperate file?

You have basically two problems here:
As $thumb contains the binary data of the image, the browser will not understand it unless you tell the browser what data it is (e.g. image/jpg).
You need to tell the browser where the data is.
Let's say you want to create an image displaying the thumb in that page:
<td><img src="..." alt="thumb"></td>
The src attribute tells the browser where it can find the data of the image. So it is used to solve problem 2. It expects an Uniform Resource Locator (URI).
So how to get the $thumb into an URI? There are multiple ways to do that, including the one linked in a comment.
However, if the image is not very large and you don't need to have it cached specifically (e.g. the HTML should be cached, but not the thumb image), you can make use of a data: URI Scheme­Wikipedia:
$thumbSrc = 'data:image/jpg;base64,'.base64_encode($thumb);
You then can output that variable as the src attribute's value:
<td><img src="<?php echo $thumbSrc; ?>" alt="thumb"></td>
Hope this is helpful.
Complete answer:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_thumb']), '" alt='thumb'></td>';
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_image']), '" alt='image'></td>';
echo "</tr>";
}
echo "</table>";

You may use Data URI Scheme. But note that not all browsers support this type of URI.
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo "<td>" . $row['user_image'] . "</td>";
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['user_thumb']) . "' alt='' /></td>";
echo "</tr>";
}
echo "</table>";

Related

Background color <td> + INSERT Mysql

I am playing around with PHP, however I am stuck on this part. I created a table which inserts all the information from the MySQL database, this is working perfectly, however: I want if the 'type' in the MySQL database is 1 that the entire of the table has a red background and if it's something else, it should be a green background.
Could anybody give me advise, where to start?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] ."</td>";
echo "<td>" . $row['type'] ."</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['transaction'] . "</td>";
echo "</td>";
}
echo "</table>";
?>
You need to mark the rows you want to effect somehow. You could do this with a data-type attribute, but if you are just starting and want to keep things simple, a class is the most straightforward.
while($row = mysqli_fetch_array($result))
{
echo "<tr class=\"type" . $row['type'] . "\">";
echo "<td>" . $row['date'] ."</td>";
echo "<td>" . $row['type'] ."</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['transaction'] . "</td>";
echo "</td>";
}
echo "</table>";
?>
Then within a stylesheet or <style> tag you can add
.type1 {
background-color: red;
}
You can edit your code to place a condition in the 'type' 'td' :
echo "<td";
if($row['type'] === 1){
echo "class=\"redbg\";
else
echo "class=\"otherbg\";
echo >" . $row['type'] ."</td>";
Or, simply :
echo "<td";
if($row['type'] === 1){
echo "class=\"redbg\";
echo >" . $row['type'] ."</td>";
Then make a standard rule in your css for td:not(redbg).

How to echo text to cell, based on the value of another cell

I have a PHP code, showing me raw data from a MySQL database in a table.
I want to add some text to one of the existing cells, depending on a value in another column which is not displayed in the table.
My code looks like this:
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn'] . "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";
This outputs a table consisting of Name, job, workplace etc.
In the cell displaying the name, I would like to add some text if a column in my MySQL DB has the value 1 in the row.
What to do? I've tried using if, as seen below - but that doesn't seem to work.
echo "<td class='blaa'>" . $row['Navn'] .
if ($row['Formand'] == 1) {
echo "(Formand)";
} "</td>";
You have to do multiple echos :
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo "(Formand)";
}
echo "</td>";
Or, with ternary operator :
echo "<td class='blaa'>" . $row['Navn'] . ($row['Formand'] == 1 ? "(Formand)" : "") . "</td>";
update like this.
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo " (Formand)";
}
echo "</td>";
or you can use short PHP tag in HTML code.
?>
<td class="blaa">
<?php echo $row['Navn']?><?php echo ($row['Formand'] == 1)?' (Formand)':'';?>
</td>
<?php
Try below code.
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn']." ".($row['Formand'] == 1 ? "(Formand)" : ""). "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";

Dynamic link inside a dynamic PHP table

So I have a dynamic PHP table where I am trying echo one of the table columns as a dynamically generated link. Presently, the $url variable i have declared is returning null. $row['username'] is returning what it should, so I suspect I have the syntax off for that. I also am having trouble with the syntax on the following line. How do I insert the $row['username'] as the link text?
echo "<td>" . "<a href='".$url."'> . $row['username'] . </a>" . "</td>";
this present example returns an error, but when I replace it with:
echo "<td>" . "<a href='".$url."'> blahblahfiller </a>" . "</td>";
the page runs, so I know it's my syntax there.
Here is my full code for the table:
<?php
while ($row = mysql_fetch_array($query)) {
$url = "profilepage.php?name=" + $row['username'];
echo "<tr>";
echo "<td>" . '<input type="checkbox" name="checkbox"/>' . "</td>";
echo "<td>" . "<a href='".$url."'> . $row['username'] . </a>" . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $url . "</td>";
echo "</tr>";
}
?>
Sincere thanks for any and all help! It means a lot.
You are using + instead of . for to concatenate. It should be -
$url = "profilepage.php?name=". $row['username'];

Adding php variable into a href link into a table

I am working on uploading a file along with a description and table and then displaying it in a table format. My problem is that I'm not sure how to link my the path for the uploaded file into the table so the user can click on the link in the table and it will download.
Code:
This is what I'm attempting to use>
<?php
include 'connect.php';
$result = mysqli_query($con,"SELECT DocDate, Description, DocFile FROM Documents");
echo "<table border='0' width='100%'>
<col width='50'>
<col width='100'>
<tr>
<th>Date</th>
<th>Description</th>
<th>File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" $row['DocFile'] "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
If you feel it to be usefull I'm happy to add the code where I upload the file to my server.
EdiT Sorry I put in the wrong variable thing into my table, I don't think it changes it too much
Is this what you're looking for?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td> " . $row['DocFile'] . " </td>";
echo "</tr>";
}
This is how you can do
echo '<td>'.$name.'</td>';
I prefer to use ' ' and within it have the HTML since HTML will contain a lot of "" so no need to use escape them and then separate PHP and HTML with concatenation.
you have syntax error in this line in while loop:
echo "<td>" $name "</td>";
should be :
echo "<td> $name </td>";
In this line you have an error:
echo "<td>" $row['DocFile'] "</td>";
You need to scape the " character:
echo "<td> " . $row['DocFile'] . " </td>";
With your syntax you are creating an error because you are ending the string after the td tag.
If you are using a web url which uses a php variable in the url and want to open a new tab when you click on the hyper link, use this
echo "<td><a target='_blank' href=\"http://view.php?Id=".$row['Id']."\">". $row['Id'] ."</a></td>";
This is helpful when you have to use a php variable in the hyperlink and is being used in a table. Clicking on the Id will open the page related to that Id in a new tab in this case.

Displaying MySQL data in PHP Table Not working

Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.
echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";

Categories