0

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 :(

1
  • 1
    Please don't use mysql_ functions anymore, because they're deprecated; check out PDO or mysqli for the alternative. Commented Jul 19, 2013 at 8:04

4 Answers 4

1

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 ;)

Sign up to request clarification or add additional context in comments.

4 Comments

Please, do not render HTML with print.
It's irrelevant here whether it's more correct, more accurate, or faster to use echo or print as a method for sending HTML output.
Please, do not render HTML with print or echo! :) Specially not for a novice, they'll vomit.
I would suggest returning an empty array instead of an implicit null if the query fails or has no resulting rows.
0

Something like this perhaps?

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

Comments

0

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).'"/>';
}

Comments

0

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);

1 Comment

@SteHughes That's fine. I'm leaving this answer here for others in the meantime :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.