简体   繁体   中英

PHP MySQL multiple image URL fields array

In building a website for a friend the database has a row with 39 fields for images. In the field is the name of the image (eg "my_image.jpg") not the image itself (BLOB). ie: image_01, image_02, image_03 and so forth.

I have PHP generating the while loop and getting the information without problems. I'm trying to get all the images into one array so I can display the pictures from that one row as a gallery.

I hope someone can offer me a way forward as I've tried without success.

from while loop:

$MEDIA_IMAGE_00 = $row["MEDIA_IMAGE_00"];
$MEDIA_IMAGE_01 = $row["MEDIA_IMAGE_01"];
$MEDIA_IMAGE_02 = $row["MEDIA_IMAGE_02"];

I need to echo out as

["propimages/$MEDIA_IMAGE_00", "", "", "$MEDIA_IMAGE_TEXT_00"],
["propimages/$MEDIA_IMAGE_01", "", "", "$MEDIA_IMAGE_TEXT_01"],
["propimages/$MEDIA_IMAGE_02", "", "", "$MEDIA_IMAGE_TEXT_02"]

for them to display in a gallery.

EDIT:

while($row = mysql_fetch_array($sqlSearch)){ 
   $propid = $row["propid"];
   $MEDIA_IMAGE_00 = $row["MEDIA_IMAGE_00"];
   $MEDIA_IMAGE_01 = $row["MEDIA_IMAGE_01"];
   $MEDIA_IMAGE_02 = $row["MEDIA_IMAGE_02"];

   $MEDIA_IMAGE_33 = $row["MEDIA_IMAGE_33"];
   $MEDIA_IMAGE_34 = $row["MEDIA_IMAGE_34"];
   $MEDIA_IMAGE_35 = $row["MEDIA_IMAGE_35"];
}

I'm assuming that $propid is what identifies the row itself and that 'MEDIA_IMAGE_TEXT' is available in the same row:

$properties = array();
while ($row = mysql_fetch_array($sqlSearch)) {
    $propid = $row["propid"];
    $images = array();
    for ($i = 0; $i <= 35; ++$i) {
        $imageId = "MEDIA_IMAGE_" . str_pad($i, 2, '0', STR_PAD_LEFT);
        if ($row[$imageId]) {
            $images[] = array(
                $row[$imageId],
                '',
                '',
                $row["MEDIA_IMAGE_TEXT_" . str_pad($i, 2, '0', STR_PAD_LEFT)],
            );
        }
    }
    $properties[] = array(
        'id' => $propid,
        'images' => $images,
    );

    echo json_encode($properties);

It generates a list of properties, each having an id and an array of images; each image comprises the location (I guess) and the title / description.

Why don't you build an array of images in your while loop ?

$images = array()

$i = 0;

While(...) {
$images[] = $row["MEDIA_IMAGE_0$i"];
$i++;
[...]
}

The you get an array that you ca use in a foreach and display your row(s). On the principle that should work, i think ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM