user1775888
user1775888

Reputation: 3303

Select img tag from PHP MySQL output in jQuery

I'm creating a basic blog CMS using PHP MySQL jQuery, Below is my MySQL table:

id              : mediumint(6)      :AUTO_INCREMENT
content         : longtext
content_pic     : text

1 content_pic column save several img src: < img src="http://">< img src="http://">...

& PHP:

$query=mysql_query($sql) or die(mysql_error());
while($list=mysql_fetch_array($query)){
    print"
        <div class=\"content\">$list[content]</div>
        <div class=\"contentpic\">$list[content_pic]</div>
    ";
}


There're several img src tag in 1 $list[content_pic] output, how do i select each img in jQuery?
Should I named every img, add class name when i upload string to mysql?


How to select each picture from PHP output usually? for example: http://www.asos.com/Vans/Vans-Authentic-Plimsolls/Prod/pgeproduct.aspx?iid=691064&abi=1&clr=Black
How to give each img unique id then i can select in jQuery?


Any suggestion will be appreciated, Thank you so much!

Upvotes: 0

Views: 587

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

I would suggest not putting the whole HTML source in the database, but rather just the path to the image files. For the content pics, you could have a comma-separated list stored in the field.

When you are about to output the HTML source simply explode the content picture string from the database into an array on the commas. You can then iterate through each one giving it a unique ID that you can use as a handle in jquery.

Doing this will decouple your data storage in MySQL form your display logic in PHP, so if you decide to change the HTML structure, there would not be any need to change the data stored in the database.

Generally, unless you have some really compelling reason to do so, you shouldn't be storing any HTML fragments that you are going to output within your database.

Upvotes: 2

Related Questions