Monday, 8 June 2015

Zend Framework How to delete a table row from foreach loop

Zend Framework How to delete a table row from foreach loop

Scenario: I have list of messages for each user. I want to delete all the messages for an particular user. Here important part is, I don't to do something with deleting each message of user.

Problem: I don't want to use the delete function which creating new query.
As I want to do something with deleting the messages, so i can't delete all record in single query.

Solution: When we list any data from model, there is inbuilt delete function which will delete the current message from foreach loop.

See Example Below
Controller Part here we write the logic
$messageObj = new Application_Model_Messages();
$id=5;
$conds = array('userId=?'=>$id, 'status=?'=>'1', );
$fields = array('id','text');
$messages=$messageObj->getMessages($conds,$fields);
foreach($messages as $message){
    $isDeleted=$message->delete(); //This will delete the current row
    if($isDeleted){
        //you can write here code when each message deleted successfully.
    }
}

here delete(), will delete the current message.

  Model Function which will list of messages of user where each message is an object (Include this function in model).
function getMessages($conds = array(),$fields=array()) {
    try {
        $select = $this->select();
        $select->from(array('m' => 'user_messages'), $fields);
        foreach ($conds as $conIndex => $condValue) {
            $select->where($conIndex, $condValue);
        } 
        $result = $this->fetchAll($select);
        if ($result) {
            return $result;
        } else {
            return FALSE;
        }
    } catch (Exception $e) {
        echo $e->getMessage();
        return FALSE;
    }
}
In this model, you can add condition and  fields dynamically. When calling you can add custom conditions and fields.

Question: How to delete one OR Multiple Record in ZF1.12?
     function deleteRecord($tourId=0) {
         return $this->delete(array('tour_id=?' => $tourId));
    }





Sunday, 7 June 2015

Load multiple javascript files asynchronously

load multiple javascript files asynchronously



In Every website, We have to include the JavaScript. We need to include multiple JavaScript file in single web page.

It's always prefer to include the javascript at the bottom of the page so that loading of content remain fast. So we should add at the bottom of the page.

We should also load the JavaScript files asynchronously, means load JavaScript file parallel.


There are two ways to load JavaScript file. Newer browsers have async attribute which load JavaScript asynchronously.
<scritp async="true" src="yourscript.js" type="text/javascript"></scritp>

Use custom JavaScript function to load javascript-file asynchronously.


<script type="text/javascript">
function loadScript (scriptpath){
    var s=document.getElementsByTagName('script')[0];
    var ss=document.createElement('script');
    ss.type='text/javascript';
    ss.async=true;
    ss.src= scriptpath;
    s.parentNode.insertBefore(ss,s);
}
</script>

Just call the loadScript function to load javascript file asynchronously. For Example.
<script type="text/javascript">
loadScript('/js/jQueryJSFIle1.js');
loadScript('/js/jQueryJSFIle2.js');
loadScript('/js/jQueryJSFIle3.js');
</script>