Ste Hughes
Ste Hughes

Reputation: 15

Need to output array from function :(

I am creating a 'buy and sell' website as a beginner project in PHP.

I have a 'advert_images', it is 3 columns.

ID | advert_id | path_to_image _|___|_______

the id column is AI and is not used. When I put the data in the database I took the advert_id from when it was posted and just recorded the image paths next to it.

Now on the advert pages I want to output these images... but I can't get my head around writing the function for this. I've been at it a few hours now trying and adapting things I've seen on this site but it won't click...

    function get_images($adverts_id){
$query = mysql_query("SELECT * FROM `advert_images` WHERE `advert_id` = " .$adverts_id . "");
while (($row = mysql_fetch_assoc($query)) !== false){
    $row['image_path'];
    echo ($row['image_path']);
    }
}

This is just echoing this:

images/adverts/52f0f5f8cc2c099d0c.jpgimages/adverts/9f1579b69deb751161852.jpgimages/adverts/1d0d4ff0b2c40834c7aa.jpgimages/adverts/8ea518b393eebfd0cd0.jpgimages/adverts/3566957b0d3dfdfa3c.jpg

so it's not a million miles away from working but I want it so I can call each image individually...

Each advert can have anything from 0-5 images saved for it in advert_images

Any help would be greatly appreciated. Like I said I am just stuck and fresh out of ideas :(

Upvotes: 0

Views: 103

Answers (4)

If it's plain HTML and you don't need tables or div tags, this is the corrected function:

function get_images($adverts_id) {
    if ($query = mysql_query("SELECT * FROM `advert_images` WHERE `advert_id` = '{$adverts_id}'")) {
        if (mysql_num_rows($query) > 0) {
            while ($row = mysql_fetch_assoc($query)) {
                $output[] = $row["image_path"];
            }

            return $output;
        }
    }
}

Then, you could do something like:

foreach (get_images($adverts_id) as $image) {
    print "<img src=\"{$image}\" alt=\"\">";
}

Hope that helps ;)

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173542

First of all, you shouldn't select all columns if you're only end up using a single column. Secondly, I would suggest switching to PDO and prepared statements:

function get_images(PDO $db, $adverts_id)
{
    $stmt = $db->prepare('SELECT image_path 
        FROM advert_images 
        WHERE advert_id = :advert'
    );
    $stmt->execute(array(':advert' => $adverts_id));

    return $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
}

And then, calling it:

echo join('', array_map(function($imagePath) {
    return sprintf('<img src="%s" />', htmlspecialchars($imagePath));
}, get_images($db, 123);

Upvotes: 0

Orangepill
Orangepill

Reputation: 24645

This should get your output as an array.

<?php
function get_images($adverts_id){
    $query = mysql_query("SELECT * FROM `advert_images` WHERE `advert_id` = " .$adverts_id . "");
    $return = array();
    while (($row = mysql_fetch_assoc($query)) !== false){
        $return[] = $row['image_path'];
    }
    return $return;
}

If you want to create images tags from this you can do something like this:

$images = get_images($ad_id);
foreach($images as $image){
    echo '<img src="'.htmlspecialchars($image).'"/>';
}

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33511

Something like this perhaps?

 echo ("<img src='".$row['image_path']."'>");

Upvotes: 0

Related Questions