Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
Please don't use mysql_ functions anymore, because they're deprecated; check out PDO or mysqli for the alternative. –  Ja͢ck Jul 19 '13 at 8:04

4 Answers 4

up vote 1 down vote accepted

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

share|improve this answer
    
Please, do not render HTML with print. –  Dyin Jul 19 '13 at 7:39
    
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. –  Julio Meca Hansen Jul 19 '13 at 7:48
    
Please, do not render HTML with print or echo! :) Specially not for a novice, they'll vomit. –  Dyin Jul 19 '13 at 7:50
1  
perfect!!! thank you :) –  Ste Hughes Jul 19 '13 at 7:55
2  
I would suggest returning an empty array instead of an implicit null if the query fails or has no resulting rows. –  Ja͢ck Jul 19 '13 at 8:03

Something like this perhaps?

 echo ("<img src='".$row['image_path']."'>");
share|improve this answer

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).'"/>';
}
share|improve this answer
    
works great! thank you :) –  Ste Hughes Jul 19 '13 at 7:55

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);
share|improve this answer
    
I started PHP a week ago lol I'll learn PDO soon. –  Ste Hughes Jul 19 '13 at 8:12
    
@SteHughes That's fine. I'm leaving this answer here for others in the meantime :) –  Ja͢ck Jul 19 '13 at 8:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.