Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Undefined variable PHP
<!DOCTYPE html>
<html>
<head>
<title>ESP32-CAM Photo Gallery</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container > div {
text-align: center;
margin: 10px;
}
</style>
</head><body>
<h2>ESP32-CAM Photo Gallery</h2>
<?php
// Image extensions
$image_extensions = array("png","jpg","jpeg","gif");
// Target directory
$dir = 'uploads/';
if (is_dir($dir)){
echo '<div class="flex-container">';
$count = 1;
$files = scandir($dir);
rsort($files);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {?>
<div>
<p><?php echo $file; ?></p>
<a href="<?php echo $dir . $file; ?>">
<img src="<?php echo $dir . $file; ?>" style="width: 350px;" />
</a>
</div>
<?php
$count++;
}
}
}
if($count==1) { echo "<p>No images found</p>"; }
?>
</div>
</body>
</html>
saya mau tanya ini kenapa ya
error massage nya :
Message: Undefined variable: count
Filename: user/galeri.php
Line Number: 55
Backtrace:
File: C:\xampp\htdocs\aeronomos\application\views\user\galeri.php
Line: 55
Function: _error_handler
File: C:\xampp\htdocs\aeronomos\application\controllers\User.php
Line: 222
Function: view
File: C:\xampp\htdocs\aeronomos\index.php
Line: 315
Function: require_once
1 Jawaban:
<div>Harusnya $count di taruh di luar if yg pertama aja, biar pas di panggil di if yg bawah masih bisa,<br>Sebelum :</div><pre><!doctype html> <html> <head> <title>esp32-cam photo gallery</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .flex-container { display: flex; flex-wrap: wrap; } .flex-container > div { text-align: center; margin: 10px; } </style> </head><body> <h2>esp32-cam photo gallery</h2> <?php // image extensions $image_extensions = array("png","jpg","jpeg","gif");
// target directory $dir = 'uploads/'; if (is_dir($dir)){ echo '<div class="flex-container">'; $count = 1; $files = scandir($dir); rsort($files);
foreach ($files as $file) {
if ($file != '.' &amp;&amp; $file != '..') {?&gt;
&lt;div&gt;
&lt;p&gt;&lt;?php echo $file; ?&gt;&lt;/p&gt;
&lt;a href="&lt;?php echo $dir . $file; ?&gt;"&gt;
&lt;img src="&lt;?php echo $dir . $file; ?&gt;" style="width: 350px;" /&gt;
&lt;/a&gt;
&lt;/div&gt;
<?php $count++; } } } if($count==1) { echo "<p>no images found</p>"; } ?> </div> </body> </html></pre><div><br>Diubah menjadi :<br><br></div><pre><!DOCTYPE html> <html> <head> <title>ESP32-CAM Photo Gallery</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .flex-container { display: flex; flex-wrap: wrap; } .flex-container > div { text-align: center; margin: 10px; } </style> </head><body> <h2>ESP32-CAM Photo Gallery</h2> <?php // Image extensions $image_extensions = array("png","jpg","jpeg","gif"); $count = 1;
// Target directory $dir = 'uploads/'; if (is_dir($dir)){ echo '<div class="flex-container">'; $files = scandir($dir); rsort($files);
foreach ($files as $file) {
if ($file != '.' &amp;&amp; $file != '..') {?&gt;
&lt;div&gt;
&lt;p&gt;&lt;?php echo $file; ?&gt;&lt;/p&gt;
&lt;a href="&lt;?php echo $dir . $file; ?&gt;"&gt;
&lt;img src="&lt;?php echo $dir . $file; ?&gt;" style="width: 350px;" /&gt;
&lt;/a&gt;
&lt;/div&gt;
<?php $count++; } } } if($count==1) { echo "<p>No images found</p>"; } ?> </div> </body> </html></pre>