wemakenever
wemakenever

Reputation: 3

mysql - design a table with a column that can have multiple values

I have a table with news.

id | title   | text    | date    | image
----------------------------------------------------------
1     HI      R u ok?   5/5/2013   image1.jpg
2   Good bye  Bye Bye   6/6/2013   image2.jpg, image3.jpg

And every piece of new can have more than image, any number of images. I've tried to store in one column several images (like the example), but I don't know how exactly then display it like any news website.

Title
Text
Images
Date

How can I do this with a foreach or while?

Upvotes: 0

Views: 101

Answers (3)

Srikanth Kolli
Srikanth Kolli

Reputation: 912

$My_SQL=mysql_query("select * from tbl_name where id=2");
while($array=mysql_fetch_array($My_SQL)){
$title=$array["title"];
$text=$array["text"];
$date=$array["date"];
$images=$array["images"];
}
$image_arr=explode(',',$images);

Now all you images are available in array elements 

Upvotes: 0

Filippos Karapetis
Filippos Karapetis

Reputation: 4652

Assuming you wish to add all of the images at the same place (and not, say, inlined in the text), you can just fetch all the records you want, and then split the multiple values with PHP's explode() function:

http://php.net/manual/en/function.explode.php

Upvotes: 0

Flipbed
Flipbed

Reputation: 720

The best practice is to use a second table where you use a foreign key to the id of your current table and an image path to the image.

Upvotes: 2

Related Questions