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

avatar dinurwahyu_google
@dinurwahyu_google

2 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

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>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;esp32-cam photo gallery&lt;/title&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;style&gt; .flex-container { display: flex; flex-wrap: wrap; } .flex-container &gt; div { text-align: center; margin: 10px; } &lt;/style&gt; &lt;/head&gt;&lt;body&gt; &lt;h2&gt;esp32-cam photo gallery&lt;/h2&gt; &lt;?php // image extensions $image_extensions = array("png","jpg","jpeg","gif");

// target directory $dir = 'uploads/'; if (is_dir($dir)){ echo '&lt;div class="flex-container"&gt;'; $count = 1; $files = scandir($dir); rsort($files);

foreach ($files as $file) {
  if ($file != &#039;.&#039; &amp;amp;&amp;amp; $file != &#039;..&#039;) {?&amp;gt;
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;&amp;lt;?php echo $file; ?&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;a href=&quot;&amp;lt;?php echo $dir . $file; ?&amp;gt;&quot;&amp;gt;
        &amp;lt;img src=&quot;&amp;lt;?php echo $dir . $file; ?&amp;gt;&quot; style=&quot;width: 350px;&quot; /&amp;gt;
      &amp;lt;/a&amp;gt;
   &amp;lt;/div&amp;gt;

&lt;?php $count++; } } } if($count==1) { echo "&lt;p&gt;no images found&lt;/p&gt;"; } ?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><div><br>Diubah menjadi :<br><br></div><pre>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;ESP32-CAM Photo Gallery&lt;/title&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;style&gt; .flex-container { display: flex; flex-wrap: wrap; } .flex-container &gt; div { text-align: center; margin: 10px; } &lt;/style&gt; &lt;/head&gt;&lt;body&gt; &lt;h2&gt;ESP32-CAM Photo Gallery&lt;/h2&gt; &lt;?php // Image extensions $image_extensions = array("png","jpg","jpeg","gif"); $count = 1;

// Target directory $dir = 'uploads/'; if (is_dir($dir)){ echo '&lt;div class="flex-container"&gt;'; $files = scandir($dir); rsort($files);

foreach ($files as $file) {
  if ($file != &#039;.&#039; &amp;amp;&amp;amp; $file != &#039;..&#039;) {?&amp;gt;
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;&amp;lt;?php echo $file; ?&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;a href=&quot;&amp;lt;?php echo $dir . $file; ?&amp;gt;&quot;&amp;gt;
        &amp;lt;img src=&quot;&amp;lt;?php echo $dir . $file; ?&amp;gt;&quot; style=&quot;width: 350px;&quot; /&amp;gt;
      &amp;lt;/a&amp;gt;
   &amp;lt;/div&amp;gt;

&lt;?php $count++; } } } if($count==1) { echo "&lt;p&gt;No images found&lt;/p&gt;"; } ?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban