Welcome Guest, Not a member yet? Register   Sign In
Please help me convert this php to CI code

$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {

$query = "UPDATE records SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
mysql_query($query) or die('Error, insert query failed');
$listingCounter = $listingCounter + 1;

I'm use to CAKE as a framework and i'm still learning CI have stand alone php/ajax code that works fine and i really need help converting it to code igniter.

I'm placing this inside a controller (i'm opting out of models at the moment).

For cake it would be something like...

foreach ($updateRecordsArray as $recordIDValue) {
$this->Record->id = $recordIdValue;
$this->Record->saveField('recordListingID', $listingCounter);

foreach ($updateRecordsArray as $listingCounter=>$recordIDValue) {
This is if you use the AR class, php5 and the $updateRecordsArray has consecutive keys starting from 0.

i'm getting the error in my firebug console -

A Database Error Occurred
You must set the database table to be used with your query.

Any Ideas?

How in your code would i set the table name where the data is being sent?

Also, thanks for the fast response, i appreciate it alot. CI is amazing

You can set it as a paramter for the update function, see the user guide for more info on updateing (and AR in general)

oops i forgot that, i'm sorry

Wow, even the likes of xwero make mistakes

I blame the government.

Got it! Thanks! To show how thankful I am heres a little treat for you.

Have you ever wanted to drag and drop sort a list item?

If anyone wants to do Ajax sortable list item (so you can drag and drop the list items to change order) and have it save to a database automatically upon dropping without refreshing the page in Code Igniter you can download the following...

Screen Shots

Before Drag
<a href="http://img20.imageshack.us/img20/7101/picture1lfe.png">http://img20.imageshack.us/img20/7101/picture1lfe.png</a>
During Drag
<a href="http://img33.imageshack.us/img33/8241/picture2pxe.png">http://img33.imageshack.us/img33/8241/picture2pxe.png</a>
After Drop (notice console data in firebug)
<a href="http://img30.imageshack.us/img30/1791/picture3wxi.png">http://img30.imageshack.us/img30/1791/picture3wxi.png</a>

// Boxes
    $data['getbox'] = $this->db->get('boxes');
function updateBOX()
        if (empty($_POST))  { return false; }
        $updateRecordsArray = $_POST['recordsArray'];
        $listingCounter = 1;
        foreach ($updateRecordsArray as $listingCounter=>$recordIDValue) {

ul {
    margin: 0;

#contentWrap {
    width: 700px;
    margin: 0 auto;
    height: auto;
    overflow: hidden;

#contentTop {
    width: 600px;
    padding: 10px;
    margin-left: 30px;

#contentLeft {
    float: left;
    width: 400px;

#contentLeft li {
    list-style: none;
    margin: 0 0 4px 0;
    padding: 10px;
    border: #CCCCCC solid 1px;

JS/ajax- (you will need jquery draggable, droppable, sortable, selectable)

    $(function() {
        $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable("serialize");
            $.post("home/updateBOX", order, function(theResponse){


<div id="contentLeft">
    &lt;?php foreach($getbox->result() as $box) :?&gt;
            <li id="recordsArray_&lt;?php echo $box->boxID ?&gt;">&lt;?php echo $box->boxID . ". " . $box->boxID . $box->boxText ?&gt;</li>
        &lt;?php endforeach; ?&gt;

mysql dump:
CREATE TABLE `boxes` (
  `boxID` int(11) NOT NULL auto_increment,
  `boxText` varchar(255) default NULL,
  `boxListingID` int(11) default NULL,
  PRIMARY KEY  (`boxID`)

-- Dumping data for table `boxes`

INSERT INTO `boxes` VALUES(1, 'Once dropped, an Ajax query is activated', 3);
INSERT INTO `boxes` VALUES(2, 'Dragging changes the opacity of the item', 2);
INSERT INTO `boxes` VALUES(3, 'Returned array can be found at the right', 1);
INSERT INTO `boxes` VALUES(4, 'It is very very easy', 4);

Please let me know if this was helpful.


Joel Beasley

i blame the weather and a whole day of ASP.NET coding Smile

Theme © iAndrew 2016 - Forum software by © MyBB