I need help with a function so that I can upload a picture from my custom CMS, then have it shown together with my post. Look at the picture below:
I want a picture in this image placeholder
I'm not so good at PHP or Mysql so please give me a easy answer.
I'll post my code over here at pastebin, easier for you to watch over there! http://pastebin.com/gBVe9sZV
If you'd like to show a set of images with your post, you'll need to store those images in a table along with the post id that they are associated with.
Upon submitting your form you're probably handling the post data via $_POST. Your images will come across under $_FILES. Once you have performed an insert on your post table, you can call mysql_insert_id() to get the new primary key ID for that post.
The post id can be stored in your images table along with references to your various images. I'm assuming here that you aren't generating your own post id's, however if you are you'd use that instead.
See also Handling File Uploads, on php.net.
Whenever you're querying your database to show the post to your readers, perform a JOIN on the images table based upon a common post id, and you'll have the paths to all related images.
See also Understanding JOINs in MySQL and Other Relational Databases, on sitepoint.com.
Related
I'm building a CRM and I have a list of customers.
When I create a customer, I usually insert it as a draft as soon as the user clicks on "Insert button". Why? Because I want the user to be able to insert photos and documents on the insertion form. This way, I have the customer ID already and I can process the upload using that ID.
The problem here is that we can have multiple drafts at a certain moment.
My question is: is there any better method to do this? Without creating the draft? For instance, using a temp folder where the photos and documents are temporarily saved?
EDIT:
the issue woth having multiple drafts is that at certain point will may have a lot of trash.
I mean, this works for me ok? And it's a solution I have, I just wanted to know if there's a better solution instead of using drafts.
It depends on what exactly you are using to save the customer in the database.
If you are using an ORM such as Eloquent or Doctrine you just need to have the correct relationships between Entities and you won't need to save the Customer as draft, you can just flush all of the information at once.
If you aren't using an ORM, you can still do it in one request, just insert the Customer record first and get the last inserted ID , for example if using PDO, you can use PDO::lastInsertId https://www.php.net/manual/en/pdo.lastinsertid.php .
After you have the ID just set it to the other columns where you need it.
I have two entities: Image and Post, that are linked by a ManytoOne relationship. The entity Image is used to handle file upload and store upload relative data (like absolute path...). I am using Symfony2 Cookbook tutorial dealing with file uploads.
I am now building a form that allows a user to:
Enter some post specific informations (like title, content..)
upload with jQuery/AJAX many images for the post.
Send the the whole form by button click.
I am still not finding the right approach to implement this solution regarding data persistence in the database.The problem for me is:
In the entity Image, an attribute ($post_id) is used as Foreign Key and will store the post id. The user will upload many images before the entity post is persisted. All the instances of Image created each time will not contain a value for post_id. In my opinion, performance will be affected if I:
update all the rows in image table after persisting an instance of Post.
Create an empty Post instance first, use its id in the Image instances, then update Post instance.
Any suggestions are highly appreciated.
Why don't you use this way of working with your forms?
http://symfony.com/doc/current/cookbook/form/form_collections.html
And in this way you only upload the images with the entire form.
If you need to preview the photos as the user selects them in the file inputs than you can check this answer
How to preview image before uploading in jquery
But it will not work on older browsers like IE8, IE9.
I am working on a CMS in Codeigniter and it has the usual fields like title, post content, etc (the page is similar to Wordpress in this way).
I have an image uploader on the page that saves the image via ajax to a MySQL table with basic fields such as image_id, path, size.
There is another table that stores the relationship between an item and an image that requires both an item_id and an image_id.
The problem I have is that the item_id is not known until the page is created/saved. So if a user goes to the Create Post page and uploads an image from there, I can't add the relationship since I don't have an item_id yet.
Some things I am considering:
Every time someone goes to the Create Post page, it automatically creates a blank post in the MySQL table with just the item_id and storing it in a hidden field. The problem I have with this is the database could get filled with a lot of empty posts if a person just clicks Create Post but never fills it out.
Saving the images in the images table, but also save an array of all of their ids for when the Create button is clicked. When that button is click, then add the image relationships.
Hide the image uploader until the post title is filled out, which triggers a page save
Wordpress seems to have solved this but I'm not sure what it does?
Anyone have any better suggestions?
I would say to structure your images table so that it doesn't require a reference to a specific post. Put image-post relationships in a separate table. On your "compose post" screen, have the image uploader return the ID of the newly-inserted image, and when you submit the post, add the new post ID and the image ID to the table for image-post relationships. This also gives you more flexibility to do things like link to one image in multiple posts.
-- edit: upon re-reading, it sounds like this is already how you're storing them. If so, then there shouldn't be a problem. Just add the image-post relationship at the time the post is submitted. If you're worried about orphaned images, see below. --
You're right that this could lead to orphan images, if someone uploads an image and then closes the window without making a post. I wouldn't worry about the images table "filling up," though. MySQL tables can have millions of rows and still perform well.
If you're concerned about collecting unused image files, you could have a periodic maintenance job that removes image files (and the associated table entries) if the upload date is older than, say, 1 week, and the image isn't associated with any posts.
Seems simple enough:
Use 1 form
Collect the data posted
Insert article - record insert id
Insert Image - record image id
Use ids to insert into relationship table
EDIT
As you are using ajax to upload the image, you could use a return value to load the inserted image id into a hidden form field
In case this question has been asked before just refer me to the answer and I shall be greatful.. it's a very common problem however I have not been able to find solution form google
I am developing a website in PHP using codeigniter. I have articles /Posts. These posts are located in a database and when ever a user demands to see an Article, all its relevant data is pulled from database using a unique "Content_id" and a page is presented to user dynamically.
However once the data is dispatched to the view and an html page is created from that page I have no way of finding which "Content_id" was used in generating that page. Now in case user comments on that page I dont know which "content_id" to save in the comments table along with this comment so that next time when this article is displayed then all the relevant comments can also be displayed.
some of the solutions that I thought are as follows
Can I use session to store this data? (However I am not using sessions for those users who are not logged in ... So can I use sessions for all users )
Will using a form on the page with hidden fields be a viable option...
How do Wordpress or other CMS are handle this problem?
To achieve your goal, your table needs four columns:
Unique row number
Content
Post number
Post date (timestamp)
The unique row number is the primary key. Auto_increment it.
The content is the posted message.
The post number is the row number of the post that starts the conversation. Make it the same as the row number in the conversation's first post. It is the row number of the post being responded to. All responses to the conversation will have the same post number.
The post date is a timestamp. It permits you to organize the order of the posts in a particularized conversation.
Each conversation, or sub_conversation, follows this routine.
Usually, the post id is passed as a GET variable anyway. If not, a hidden input is fine.
I am Very sory to Know that no one answerd my question except one persone
Any ways here is the solution that I have figured out in 1 sentence **
The pages are not required to be numbered But the content needs to be numbered... thus each content (Article, post etc) has a content id and that id needs to be considered.
**
I am using swf-upload to allow users to upload images quickly. I need them to be able to select which galleries they want them to appear in so I have added extra controls to the form - but they are never passed in GET or POST to the upload.php catcher.
I think 'addPostParam' is what I need to set, but am not entirely sure where or how and I can't find any guide that is clear on the issue.
Also is there any problems using PHP style array names for the tags such as tag[1] tag[2] etc?
Flash can't yet send multiple data at once in easy way. Send image in RAW data then you can give it name. Change that name to name+GET. Sample: image.jpg?property=value;