C-V ส่งข้อมูลจาก Controller ไปยัง views/scripts Zend FrameWork

หมวดสำหรับแบ่งบันความ รู้ต่างๆ จะมีหมวดย่อยๆ ในหมวดนี้ เช่น php, SQL, XML, CSS

Moderator: mindphp, ผู้ดูแลกระดาน

zend_framework
PHP Full Member
PHP Full Member
โพสต์: 41
ลงทะเบียนเมื่อ: 08/03/2011 11:22 pm

C-V ส่งข้อมูลจาก Controller ไปยัง views/scripts Zend FrameWork

โพสต์ที่ยังไม่ได้อ่าน โดย zend_framework »

C-V ส่งข้อมูลจาก Controller ไปยัง views/scripts
ตัวอย่างจากนี้ไปเป็นการส่งข้อมูลจาก Controller ไปยัง views/scripts ต่างๆ เพื่อแสดงให้เห็นการแยกส่วนของ coding กับ HTML page ทำให้ง่ายต่อการแก้ไขเพิ่มเติมภายหลัง

1. สร้างไฟล์ /srv/www/application/controllers/IndexController.php มีข้อมูลดังนี้

โค้ด: เลือกทั้งหมด

<?php

class IndexController extends Zend_Controller_Action
{

    public function indexAction()
    {
        //$this->initView();
        $this->view->assign('title', 'Hello World!');
        $this->view->assign('body', 'ยินดีต้อนรับสู่ พะยูนลีนุกซ์');
        $this->view->assign('greeting', 'ยินดีต้อนรับสู่ Zend Frame Work'); 

    //Show Array
    $data = array(
            array('author' => 'Hernando de Soto', 'title' => 'The Mystery of Capitalism' ),
            array('author' => 'Henry Hazlitt', 'title' => 'Economics in One Lesson'),
            array('author' => 'Milton Friedman','title' => 'Free to Choose')
        );
    $this->view->assign('books',$data);
    }

}

?>
2. สร้างไฟล์ /srv/www/htdocs/application/views/scripts/index/index.phtml มีข้อมูลดังนี้

โค้ด: เลือกทั้งหมด

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=tis-620" />
    <title><?php echo $this->escape($this->title);?></title> 
</head>
<body>

    <h1><?php echo $this->title;?></h1>
    <h1><?php echo $this->body;?></h1>
    <h1><?php echo $this->book;?></h1>
    <h1><?php echo $this->greeting;?></h1>

<?php if ($this->books): ?>

    <!-- A table of some books. -->
    <table border="1">
        <tr>
            <th>No</th>
            <th>Author</th>
            <th>Title</th>
        </tr>

        <?php foreach ($this->books as $key => $val): ?>
        <tr>
            <td><?php echo $this->escape($key)+1 ."." ?></td>
            <td><?php echo $this->escape($val['author']) ?></td>
            <td><?php echo $this->escape($val['title']) ?></td>
        </tr>
        <?php endforeach; ?>

    </table>

<?php else: ?>

    <p>There are no books to display.</p>

<?php endif; ?>

</body>
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 82