How would you do this…

Soldato
Joined
18 Oct 2002
Posts
14,844
I've just wasted two days trying to get this working with CodeIgniter and I'm pulling my hair out. Anyone got any ideas for an east way of getting this to work?

I'm trying to set up a simple form that allows a user to edit various bits of text without needing a login and preferably without needing to go to a different screen to edit the content.

So you'd have:

==

Title 1

Content relating to title 1

Title 2

Content relating to title 2

Title 3

Content relating to title 3

==

Both the titles and the content areas need to be editable. I tried using HTML contenteditable=true but I could only get it to store the information for one title and content. I also couldn't figure out how to store the changes in a database.

I then moved on to CodeIgniter and got as far as storing the information in the database but as soon as I tried to call two $data strings into the same view I kept getting php errors.

==

I have a feeling I'm over complicating what should be a really simple operation. There may even be something out there already (open source or otherwise).

Has anyone got any bright ideas?

Thanks for any help you can give me.
 
Associate
Joined
26 Sep 2007
Posts
1,252
Location
Amsterdam
Ensure your data is stored in an array in your controller and pass that variable down to your view. Place the markup in your view for editing the text in a for loop (with your contentEditable attribute). Eg:
foreach($data as $row) {
<< Title Markup Here >>
<< Content Markup Here >>
}

You'll want to make sure you store the ID to these objects in a hidden input field somewhere in the loop.

If you want to avoid a page reload, use AJAX. Write an AJAX call in your JavaScript file triggered by a click event on a button in your view to an endpoint in your controller which updates the database using the array you supply in the data property on the AJAX call. Use a PUT method for this.

You'll need to loop through each of the elements in your view to pull the ID of the row and it's title and content text, you can place these into a serializable JSON object/regular JS array which you can turn into an associative PHP array using JSON_decode in your controller. I use Laravel but I'm sure CodeIgnitor will have a batch update function which takes an array of objects as a parameter and updates the corresponding rows in the database.

In your success/fail callback for your AJAX call you can display some message to the user about whether their request was successful or not (JSON_encode your response in your controller before returning)

Hope that's clear enough!
 
Back
Top Bottom