0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, so I still save several path in 1 field?How to get? or my MySQL table is not properly?
Your data in the field might look like "/path/to/image1.jpg,/path/to/image2.jpg". Ten you just use PHP explode() to convert this string of paths into an array of paths after retrieving from the database.

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.