why the while loop runs once?

232
January 13, 2018, at 07:54 AM

the following code is only running once , while the number of times it should run is 4 , any help ?

PHP::

<?php
header("Content-Type: application/json");
require_once("config.php");
if(isset($_GET["m"])) {
    $dirname = "images/main/";
    $arr = array();
    $conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
    if(!$conn) {
        echo "Error connecting to database";
        exit();
    }
    if($stmt = $conn->prepare("SELECT name_ FROM projects")) {
        $stmt->execute();
        $stmt->bind_result($n);
        //$stmt->store_result();
        $result = $stmt->get_result();
        if($result->num_rows == 0) {
            echo "No Projects";
            $stmt->close();
            $conn->close();
            exit();
        }else {
            while ($row = $result->fetch_assoc()) {
                $dirname = $dirname . $row["name_"] . "/";
                $images = glob($dirname . "*.*", GLOB_BRACE);
                foreach($images as $image) {
                    echo $row["name_"];
                    echo$result->num_rows;  // returns 4 !!!!
                    $image = base64_encode($image);
                    //$arr[] = $image;
                    array_push($arr, $image);
                    $image = "";
                }
            }
            echo json_encode($arr);  // returns 1 json row oonly
        }
    }
    $stmt->close();
    $conn->close();
    exit();
}
?>

num rows return 4 so why it runs or loops for one time only ?

I am trying to get images from images folder to echo it back

FIX::

according to jhilgeman's answer I added this part to the end of foreach:

$dirname = "images/main/";
Answer 1

If I had to guess, I'd say that it's looping correctly, but the problem is this line:

$dirname = $dirname . $row["name_"] . "/";

Each time you loop, you're APPENDING the $row["name"] value to whatever $dirname is. So let's say that you get 4 rows back like this:

name
----
houses
boats
computers
animals

At the beginning of the loop, let's say $dirname is just "/images/". So the first loop would change $dirname to be:

/images/houses/

Then the second loop would change it to be:

/images/houses/boats/

The third loop would then make it:

/images/houses/boats/computers/

And finally the fourth loop:

/images/houses/boats/computers/animals/

So unless you're expecting the $dirname to be appended that way, you probably want to instead REPLACE $dirname instead of appending to it each time.

Try this instead for your loop:

while ($row = $result->fetch_assoc()) {
  $images_dirname = $dirname . $row["name_"] . "/";
  $images = glob($images_dirname . "*.*", GLOB_BRACE);
  foreach($images as $image) {
    ...etc...
  }
}
Rent Charter Buses Company
READ ALSO
Trying to get property of non-object Notice in custom avatar function for Wordpress

Trying to get property of non-object Notice in custom avatar function for Wordpress

The following custom avatar functions works fine, but I get the following notice

241
Illegal string PDO fetch() vs fetchAll()

Illegal string PDO fetch() vs fetchAll()

When I try to call fetch() after executing a PDO statement, I get the following warning:

421
WordPress and WooCommerce custom search for &ldquo;Search in my store&rdquo; for Vedor pages

WordPress and WooCommerce custom search for “Search in my store” for Vedor pages

I have a woocommerce site that I have spent months customizingI have tried combining so many plugins to get the features I need and eliminated so many

149
How to Use Impression Beacons to Read Server Call Data (Pixel)

How to Use Impression Beacons to Read Server Call Data (Pixel)

I need to get some sort of understanding on sending server to server information using pixelsWe use validclick and they say their beacon must be sent to a 1x1 transparent image

174