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

[eluser]Jbeasley6651[/eluser]
$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);
}
#2

[eluser]xwero[/eluser]
Code:
foreach ($updateRecordsArray as $listingCounter=>$recordIDValue) {
    $this->db->set('recordListingID',$listingCounter+1)->where('recordID',$recordIDValue)->update();
}
This is if you use the AR class, php5 and the $updateRecordsArray has consecutive keys starting from 0.
#3

[eluser]Jbeasley6651[/eluser]
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?
#4

[eluser]Jbeasley6651[/eluser]
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
#5

[eluser]Dam1an[/eluser]
You can set it as a paramter for the update function, see the user guide for more info on updateing (and AR in general)
#6

[eluser]xwero[/eluser]
oops i forgot that, i'm sorry
#7

[eluser]Dam1an[/eluser]
Wow, even the likes of xwero make mistakes
#8

[eluser]TheFuzzy0ne[/eluser]
I blame the government.
#9

[eluser]Jbeasley6651[/eluser]
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>

Controller:
Code:
// Boxes
    $this->db->order_by('boxListingID','ASC');
    $data['getbox'] = $this->db->get('boxes');
Code:
function updateBOX()
    {
        if (empty($_POST))  { return false; }
        $updateRecordsArray = $_POST['recordsArray'];
        $listingCounter = 1;
        foreach ($updateRecordsArray as $listingCounter=>$recordIDValue) {
            $this->db->set('boxListingID',$listingCounter+1)->where('boxID',$recordIDValue)->update('boxes');
        }
        
    }

CSS:
Code:
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;
    background-color:#00CCCC;
    border: #CCCCCC solid 1px;
    color:#fff;
}


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

Code:
[removed]
$(document).ready(function(){
                          
    $(function() {
        $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable("serialize");
            $.post("home/updateBOX", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                  
        });
    });

});    
[removed]

View:
Code:
<div id="contentLeft">
    <ul>
    &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;
    </ul>
</div>


mysql dump:
Code:
CREATE TABLE `boxes` (
  `boxID` int(11) NOT NULL auto_increment,
  `boxText` varchar(255) default NULL,
  `boxListingID` int(11) default NULL,
  PRIMARY KEY  (`boxID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- 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.

Thanks,

Joel Beasley
#10

[eluser]xwero[/eluser]
i blame the weather and a whole day of ASP.NET coding Smile




Theme © iAndrew 2016 - Forum software by © MyBB