In an effort to learn more about php and mysql, I have been working through a number of different tutorials and posts on building a simple blog site. I have a database that holds the necessary, usual suspects (ID, title, author, date, and post). In my php, I query the db, load all the suspects into the van and then echo each one when and where I need them – works brilliantly.
Now I want to go further. I would like to have the ability to add some images to the blog posts which, right now, are simply typed into the text table of the db. How do I go about building something like that? I have been looking everywhere for some kind of hint – basically Googling every combo of “MySQL PHP Blog insert images into post”, I can think of. There are lots of different software options, but I want to understand how to build it from scratch – No WordPress, Blogger, etc.
I am not trying to allow users to upload images. I just want to include images in the actual post, so that when I query the db to load the various php divs (title, author, date, and post) the post will include any pictures that I want displayed. [i.e. “Here is some sample text that I have been working with and a picture” (picture) “as you can see, the picture above is an example that I have placed in the actual blog post”.
How do I get the mysql query to load the post and add the pictures in the appropriate place? This is not about uploading the images to the server or the database. This is simply about reading the post’s text and including the images for that post in the appropriate spots. I do not want users to be able to upload anything at all, I just want the php page to output the title, the author, the date, and the post (complete with pictures).
A few things to consider:
If your goal is to learn more about PHP, MySQL, and working with images, perhaps something like an image gallery with support for multiple albums may be a more straightforward way to learn what you want to learn. An example of what I am talking about can be found here (Disclosure: this links to the website I am building for my Wife’s photography business, but is relevant).
Hope this helps!
::EDIT::
Based upon the update to your question and the comment to my answer, there are two possible solutions I will recommend to you:
The Easy Solution:
Write some HTML in your post referencing the photo based upon where you uploaded it. tags are easy, and even Blogs such as wordpress will allow you to do this (so it’s not a cop-out) 🙂
The Educational Solution:
In each post, allow for some type of markup indicating where the image needs to be (such as [img]1[/img]… some content …[img]2[/img]… some more content ), and in your database link each photo ID to each post ID, along with the position you would like the photo to fill in the post. For clarity, that might be:
After grabbing the text of the post, perhaps you could run a query such as:
and then loop through your post replacing each occurrence of [img]NUMBER[/img] with an image tag generated based upon the Database result corresponding with the img_position inside the tag. Alternately, you could even loop through the database results and do something along the lines of:
That would probably be enough to get you started.