0

I have the following table with images for a blog post:

+----+---------+------------------------+-----------+-----------+-------+
| id | id_post | imagine                | descriere | tip       | sters |
+----+---------+------------------------+-----------+-----------+-------+
|  1 |       4 | asdasdsadasdasdasd.jpg | NULL      | thumbnail |     0 |
|  2 |       4 | asdasdsadasdasdasd.jpg | NULL      | full      |     0 |
|  3 |       4 | asdasdsadasdasdasd.jpg | NULL      | thumbnail |     0 |
|  4 |       4 | asdasdsadasdasdasd.jpg | NULL      | full      |     0 |
+----+---------+------------------------+-----------+-----------+-------+

In this table there's a column named "tip" (or type, in english) which represents the image type: full screen, or thumbnail. When someone uploads an image, there will always be a pair of "full" and "thumbnail" entries.

What I'm trying to do is to get all the images for a post and store them into an array, but with a single entry for each image like this: Array ( [0] => Array ( [thumbnail] => "1.jpg", [full] => "11.jpg")...

1
  • 1
    how do you know that image X is the thumb image Y (so that you can group them) ? Commented Aug 6, 2012 at 11:20

3 Answers 3

1

Well, 1st, you need some kind of variable, that binds the full image with the thumbnail image. Currenty there is no way telling witch thumbnail goes to witch image. Add a field like full_id or just combine the entries into one line, like:

+----+---------+--------------+---------------+-----------+-------+
| id | id_post | imagine_full | imagine_thumb | descriere | sters |
+----+---------+--------------+---------------+-----------+-------+

After that, use a loop to go through your values like @voodoo417 showed:

$items = array();

while ( $row = mysql_fetch_array( $result ) ) {
    $items[] = array( 
                 'thumbnail' => $row['imagine_thumb'],
                 'full' => $row['imagine_full']
            );
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 As there is always a thumb and full image, storing them together is a good idea. And watch out for the witches LOL
1

Select your table and follow this code,

$array[]= array();
while($row = mysql_fetch_assoc($result))
{
    //using 2-d array
    $array[$row[id]][$row[tip]]= $row[imagine];
}

Now when you want to retrieve images for specific post, you can use

$array[$post_id]['full']
$array[$post_id]['thumbnail']

Comments

0

try like this

$items = array();
while ($row=mysql_fetch_array(RESOURSE)){
 $items[] = array( 'thumbnail'=>$row['thumbnail'], 'full' =>$row['full'] );
}

1 Comment

thumbnail and full are on separate rows! so this will not work

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.