0

i am new to ZF i want to create ajax link that will go to "task" controller and "ajax" action do something like this

$registry = Zend_Registry::getInstance();  
$DB = $registry['DB'];
$sql = "SELECT * FROM task ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);

than put the result in this div

<div id="container">container</div>

this is my view where i am doing this

<?php echo $this->jQuery()->enable(); ?>
<?php echo $this->jQuery()->uiEnable(); ?>
<div id="container">container</div>
<?php  
echo $this->ajaxLink("Bring All Task","task/ajax",array('update' => '#container'));
?>

i dont know the syntax how i will do this , retouch my code if i am wrong i searched alot but all in vain plz explain me thanking you all in anticipation also refer me some nice links of zendx_jquery tutorial

1 Answer 1

1

This should work:

class IndexController extends Zend_Controller_Action 
{
    /**
     * Homepage - display result of ajaxRequest
     */
    public function indexAction()
    {
    }

    /**
     * Print result of database query 
     */
    public function ajaxAction()
    {   
        // disable rendering of view and layout
        $this->_helper->layout()->disableLayout();


        $registry = Zend_Registry::getInstance();  
        $db =  $registry['DB'];

        // get select object to build query 
        $select = $db->select();
        $select->from('task')->order('task_name ASC');

        // echo result or what ever..   
        $this->view->tasks = $db->fetchAll($select);
    }
}    



// index.phtml (view)
<?php 

    echo $this->jQuery()->enable(); 
    echo $this->jQuery()->uiEnable();

    // create link to ajaxAction
    $url = $this->url(array(
        'controller' => 'index',
        'action'     => 'ajax',
    ));
?>

<div id="container">container</div>

<?php 
    echo $this->ajaxLink(
        "Bring All Task", $url, array('update' => '#container')
    ); 
?>

and in your ajax.phtml

<?php  if ($this->tasks): ?>
  <table>
    <tr>
         <th>task ID</th>
         <th>task Name</th>
    </tr>
  <?php foreach($this->tasks as $task) : ?>
    <tr>
       <td><?php echo $task['task_id']; /* depending on your column names */ ?>
       </td>
       <td><?php echo $this->escape($task['task_name']); /* to replace " with &quot; and so on */ ?>
       </td>
    </tr>
  <?php endforeach; ?>
  </table>
<?php else: ?>
  No tasks in table.
<?php endif; ?>

regarding db you have to setup it first somewhere earlier in your code, for example front controller index.php or bootstrap.php, for example:

$db = Zend_Db::factory('Pdo_Mysql', array(
  'host'     => '127.0.0.1',
  'username' => 'webuser',
  'password' => 'xxxxxxxx',
  'dbname'   => 'test'
));
Zend_Registry::set('DB', $db);
Sign up to request clarification or add additional context in comments.

4 Comments

this will print an Array ?? if i want put put it in HTML where will i write html code in view ???.plz dont mind my awkward questions i am new to ZF
Depends on your columns. Simple way: Print the html from the Controller action.
Is $db will be accessible in php that is call over AJAX i am trying so but not working
maybe something wrong with you registry? Hard to help you sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.