tags:

views:

30

answers:

1

hi there,

i am using implode to store my image paths. with the special symbol '~' . and hence in my database i store a single string which holds the path and filename of the multiple pictures and then i retrieve it. the string which is stored in the database is similar to this.

uploads/adv/adfront_07October2010_13531.jpg~
uploads/adv/adfront_07October2010_37472.jpg~
uploads/adv/adfront_07October2010_43637.jpg~
uploads/adv/adfront_07October2010_39959.jpg

i store the above code using the implode function. and then i retrieve it using the following code.

$adbox_query = "SELECT advertisements.id ,
                       advertisements.pic_brief,
                       advertisements.pos 
                       FROM advertisements WHERE pos BETWEEN 1 AND 8";
$adbox_res = mysql_query($adbox_query) or trigger_error(mysql_error(). " in " .$query);
$adbox = array_fill(1, 8, null);
while ($row = mysql_fetch_array($adbox_res)) {
  $adbox[$row['pos']] = $row;
}

and using the foreach i fetch the result like this.

if( $adbox[1]) 
    {
    $id_1 = $adbox[1]['id'];
    echo "<a href = 'advertisement.php?id=$id_1'>";
    $ad1 = $adbox[1]['pic_brief'];
    $ad1 = explode('~', $ad1);

    foreach($ad1 as $adbox1){
            echo "<img src = 'admin-login/$adbox1' alt = 'advertisement'/>";
            }
    }
    else {
         echo "i do not exist";
         }

the above code works perfectly fine however i want to achieve something more with this.

i want the first string which prints the html element according to the queries in the database to have a class defined as active. to be more precise.

if my database holds the 4 image paths, it will print the following html code.

<img src = 'admin-login/uploads/adv/adfront_07October2010_13531.jpg' alt = 'advertisement'/>
<img src = 'admin-login/uploads/adv/adfront_07October2010_37472.jpg' alt = 'advertisement'/>
<img src = 'admin-login/uploads/adv/adfront_07October2010_43637.jpg' alt = 'advertisement'/>
<img src = 'admin-login/uploads/adv/adfront_07October2010_39959.jpg' alt = 'advertisement'/>

now i want to add the class active to the first html image tag for example instead of the above code i want to achieve it like this.

<img class= 'active' src = 'admin-login/uploads/adv/adfront_07October2010_13531.jpg' alt = 'advertisement'/>
    <img src = 'admin-login/uploads/adv/adfront_07October2010_37472.jpg' alt = 'advertisement'/>
    <img src = 'admin-login/uploads/adv/adfront_07October2010_43637.jpg' alt = 'advertisement'/>
    <img src = 'admin-login/uploads/adv/adfront_07October2010_39959.jpg' alt = 'advertisement'/>

i am confused on what i should be doing to achieve it. how do i twist my code to achieve the result.?

thank you

A: 

Erm... keep a flag to detect the first item in the loop.

$isFirst = true;
foreach($ad1 as $adbox1){
    // whatever you want to do here //
    $isFirst = false;
}

Also keeping all your images in one field violates database normalization rules.

To normalize you have to create another table just for images with the fields: id_image (autoincrement), id_advert, image_path, and maybe active. When retrieving LEFT JOIN with the images table. You can use GROUP_CONCAT() to merge them in a single string if you want.

I also suggest taking a look into joins and aggregate functions if you're not familiar with them.

Alin Purcaru
okay, if this is not the way , then what should i be doing because, i want to store multiple images(the number is not fixed) within a single primary key id.?
Ibrahim Azhar Armar