how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
Related
Thank you all in advance....
I am doing a project in which I have a field to store the image. In that form, the image uploaded is completed with croppie plugin. and I stored the data in base 64 encodings. But I cannot fetch it to a page called view.php but the images are fetched to the form after cropping.
Please help me to figure it out the mistake that I have done
<tbody>
<?php
$no = 1;
$data = mysqli_query($con, "SELECT * FROM `register` WHERE app_registration IS NULL ORDER BY `app_id` DESC ");
while ($row = mysqli_fetch_assoc($data)) {
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<?php if (!empty($row['image_reference_id'])) {
$data1 = mysqli_query($con, "SELECT * FROM `photo_table` WHERE image_unique_id = '" . $row['image_reference_id'] . "'");
$row1 = mysqli_fetch_assoc($data1)
?>
<td><img src="data:image/jpg;base64,'.base64_encode($row['images']).'"/></td>
<?php } else {
?>
<td><img src="../images/<?php echo $row['app_image'] ?>" style="width: 100px; height: 100px;"></td>
<?php } ?>
<td><?php echo $row['app_name'] ?></td>
<td><?php echo $row['app_mobile_no_1'] ?></td>
</tr>
<?php } ?>
</tbody>
Note: In db the images are in Long Blob
You will need to change the following:
<img src="data:image/jpg;base64,'.base64_encode($row['images']).'" />
to
<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>" />
Im trying to align the image that I fetched from my database with a text that is also fetched from database. The text seems ok but the image just stick to the left
<div align="center">
<p>
<?php
$vid =$_REQUEST[#id];
include 'conn.php';
$sql = mysql_query("SELECT * FROM product_car where Id = '$vid'");
$vid = 'Id';
$vnamaproduk = 'NamaProduk';
$vharga = 'Harga';
$vpenerangan = 'Penerangan';
$vgambar = 'Gambar';
?>
<table width="1000" border="0" align="center">
<?php
while($row= mysql_fetch_assoc($sql)){
?>
<tr>
<td>
<img src="gambar/car/<?php echo $row[$vgambar];?>"width="500" height="400"/>
</td>
<td>
<?php
echo "<br>Product Id : ".$row[$vid];
echo "<br>Product Name : ".$row[$vnamaproduk];
$harganew =sprintf('%0.2f',$row[$vharga]);
echo "<br>Price : RM".$harganew;
echo "<br> <br>".$row[$vpenerangan];
echo "<br>";
}
?>
ADD TO CART
</td>
</tr>
</table>
</div>
is the image displayed as a block?
img {
display:block;
margin:auto;
}
If you want to center the image, you could try this
img {
margin: auto 0;
}
I have a problem with my first attempt at making a foreach loop.
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
It's being displayed like so on the page:
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK
Hope you can help me:)
<?php
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$counter=0;
while($row= mysql_fetch_assoc($result))
{
$assoc_query[$counter] = $row;
$counter++;
}
foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $value['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $value['name']; ?> </div>
</td>
</tr>
<?php } ?>
Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
//Do your thing
}
Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.
this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless
instead you need a while loop like this
<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
You should do it like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.
#Nerdlyist
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
I have a html table where i want to echo content out of a database table:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
</td>
<td>
<?php
$ergebnis = mysql_query("SELECT Test2 FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test2;
}
</td>
</tr>
</tbody>
</table>
Is there a way to cut short the php code, because it´s a bit too long to wirte in every
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
If your example really is what you are doing you might want to rather get all data in a single query:
<?php
$ergebnis = mysql_query("SELECT Test, Test2 FROM testtable");
$recordset = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
?>
<table style="width: 100%;">
<tbody>
<tr>
<?php foreach (array('Test', 'Test2') as $column) { ?>
<td>
<?php foreach ($recordset as $record) { ?>
<?php echo htmlspecialchars($record[$column], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<td>
<?php } ?>
</tr>
</tbody>
</table>
Also note that the mysql_* functions has been deprecated and will be removed from the language soon. If you want to make sure your code doesn't start throwing notices or even will stop functioning in the future you might want to start using either mysqli_* or PDO.
http://php.net/manual/en/mysqlinfo.api.choosing.php
Also note that I have added htmlspecialchars() to prevent possible XSS attacks.
Well, you can simply do it by having a function:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
and calling it like this:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$a = 'Test';
query($a);
?>
</td>
<td>
<?php
$a = 'Test2';
query($a);
?>
</td>
</tr>
</tbody>
</table>
Create a function out of it. And use wherever you want.
You can make a function like this:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template