I have to following php code where I want to execute the image function if there is an image to show. Which means If the image field is empty/NULL I dont want to show the image, because it will give an error saying 'no image found'.
How can I do this?
Thanks in advance.
<div>
<?php if (!empty($datas[0]['imgProduct1'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct1']);?>" class="img-fluid" alt="Product1" />
<?php } ?>
</div>
<div>
<?php if (!empty($datas[0]['imgProduct2'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct2']);?>" class="img-fluid" alt="Product2" />
<?php } ?>
</div>
<div>
<?php if (!empty($datas[0]['imgProduct3'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct3']);?>" class="img-fluid" alt="Product3" />
<?php } ?>
</div>
<div>
<?php if (!empty($datas[0]['imgProduct4'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct4']);?>" class="img-fluid" alt="Product4" />
<?php } ?>
</div>
(Goes till imgProduct8)
In the Database image, I have my picture data ready. In the last line you see imgProduct1 filled, imgProduct2 filled, imgProduct3 filled. After that imgProduct4 is NULL, so I dont want to execute the image function for that matter.
Database image:
![1]](https://i.stack.imgur.com/hLf7Y.jpg)
In Result from the database picture, you see the result I am getting. So again imgProduct1, imgProduct2 and imgProduct3 are filled with a link, imgProduct4, imgProduct5 etc are not. So I dont want to show the image, thats not there.
Result from database:
![2]](https://i.stack.imgur.com/wLYuc.jpg)
The result I am getting now is 3 pictures in my slide show and after that just blank pictures. So he is still think there are more pictures to show.
my whole code:
<?php
require('../php/connection.php');
$product = $_GET['product'];
$sql = "SELECT * FROM tblproduct WHERE strProductNaam='$product'";
$result = sqlsrv_query($conn,$sql);
if( $result === false ) {
die( print_r( sqlsrv_errors(), true));
}
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$datas[] = $row;
}
echo '<pre>';
print_r($datas);
echo '</pre>';
sqlsrv_free_stmt($result);
sqlsrv_close($conn);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home Gym</title>
<?php
include('../structure/effects.php');
?>
</head>
<body>
<!-- Header -->
<?php
include('../structure/header.php');
?>
<!-- /Header -->
<!-- Main -->
<main>
<br>
<!-- Product Section -->
<section class="single-product">
<div class="container">
<div class="row">
<div class="col-md-5">
<div class="container-fluid p-0">
<div class="site-slider">
<div class="slider-one">
<div>
<?php if (!empty($datas[0]['imgProduct1'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct1']);?>" class="img-fluid" alt="Product1" />
<?php } ?>
</div>
<div>
<?php if (!empty($datas[0]['imgProduct2'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct2']);?>" class="img-fluid" alt="Product2" />
<?php } ?>
</div>
<div>
<?php if (!empty($datas[0]['imgProduct3'])){ ?>
<img src="<?php echo ($datas[0]['imgProduct3']);?>" class="img-fluid" alt="Product3" />
<?php } ?>
</div>
<div>
<?php if (($datas[0]['imgProduct4']) !== NULL){ ?>
<img src="<?php echo ($datas[0]['imgProduct4']);?>" class="img-fluid" alt="Product4" />
<?php } ?>
</div>
<div>
<?php if (($datas[0]['imgProduct4']) !== NULL){ ?>
<img src="<?php echo ($datas[0]['imgProduct5']);?>" class="img-fluid" alt="Product5" />
<?php } ?>
</div>
<div>
<?php if (($datas[0]['imgProduct4']) !== NULL){ ?>
<img src="<?php echo ($datas[0]['imgProduct6']);?>" class="img-fluid" alt="Product6" />
<?php } ?>
</div>
<div>
<?php if (($datas[0]['imgProduct4']) !== NULL){ ?>
<img src="<?php echo ($datas[0]['imgProduct7']);?>" class="img-fluid" alt="Product7" />
<?php } ?>
</div>
<div>
<?php if (($datas[0]['imgProduct4']) !== NULL){ ?>
<img src="<?php echo ($datas[0]['imgProduct8']);?>" class="img-fluid" alt="Product8" />
<?php } ?>
</div>
</div>
<div class="slider-button">
<span class="prev position-top"><i class="fas fa-chevron-left"></i></span>
<span class="next position-top right-0"><i class="fas fa-chevron-right"></i></span>
</div>
</div>
</div>
</div>
<div class="col-md-7">
<p class="new-arrival text-center">NEW</p>
<div class="product-bottom">
<p><i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
</p>
<p><h3><?php echo ($datas[0]['strProductNaam']); ?></h3></p>
<p class="price"><h5><s>€<?php echo ($datas[0]['intPrijs']); ?></s> <b><span class="make_red">€<?php echo ($datas[0]['strSalePrijs']); ?></span> </b></h5></p>
<p><b>Availability: </b>In Stock</p>
</div>
<div class="positive">
<p><i class="fa fa-plus" aria-hidden="true"></i> <?php echo ($datas[0]['strPlusPunt1']); ?></p>
<p><i class="fa fa-plus" aria-hidden="true"></i> <?php echo ($datas[0]['strPlusPunt2']); ?></p>
</div>
<div class="negative">
</div>
<label>Quantity: </label>
<input class="text-center" type="text" value="1"></input>
<button type="button" class="btn btn-primary">Add to Cart</button>
<table class="content-table">
<thead>
<tr>
<th>Product Specifications</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Type</td>
<td><?php echo ($datas[0]['strSpecSoort']); ?></td>
</tr>
<tr class="active-row">
<td>Guarantee</td>
<td><?php echo ($datas[0]['strSpecGarantie']); ?></td>
</tr>
<tr>
<td>Material</td>
<td><?php echo ($datas[0]['strSpecMateriaal']); ?></td>
</tr>
<tr class="active-row">
<td>Weight</td>
<td><?php echo ($datas[0]['strGewicht']); ?></td>
</tr>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
<!-- /Product Section -->
<!-- Product Description -->
<section class="product-description">
<div class="container">
<h6>Product Description</h6>
<p><?php echo ($datas[0]['strBeschrijving']); ?></p>
</div>
</section>
<!-- /Product Descriptionn -->
<br>
<hr class="hr">
<!-- On Sale Product -->
<?php
include('../product-sale.php');
?>
<!-- /On Sale Product -->
</main>
<!-- /Main -->
<!-- Footer -->
<?php
include('../structure/footer.php');
?>
<!-- /Footer -->
<!-- Script: Bootstrap CDN, JS -->
<?php
include('../structure/script.php');
?>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire