使用CakePHP 2.x获取和显示数据(解决) [英] fetching and displaying data with CakePHP 2.x (Solved)

查看:297
本文介绍了使用CakePHP 2.x获取和显示数据(解决)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:2.我已解决问题!感谢支持人。要查看最终版本,看看这个pastebin,看看它和StackOverflow上我的代码之间的区别。



http://pastebin.com/yWqs86Zt



编辑。我现在可以在屏幕上打印数据,但我看到所有的星球数据,当我只想要一行每ctp。 (earth.ctp等上的地球行)。



新的蛋糕,非常困惑。我看过的文件,但仍然在亏损。



在所有我正在开发一个应用程序,显示太阳系中每个行星和月亮的数据,传递varchars的数据从数据库到whateverplanet.ctp。



我试图显示一行每ctp。如Planets / earth.ctp上的地球信息,通过调用PlanetsController earth()函数中的planet_name / info / content列。



我有一个有两个表的数据库




  • 行星表

    • planet_id(int)

    • planet_name(varchar)

    • planet_info(varchar)

    • planet_content(varchar)

    li>


和一个月球表,只有月亮而不是行星。



Planet Model看起来像

  ); 
class Planet extends AppModel {

public $ name ='Planet';
public $ useTable ='planets';
}

?>

控制器是这样(注释掉的尝试)


$ b b

 <?php 

类PlanetsController扩展AppController
{
public $ name ='Planets';
public $ helpers = array('Html','Form');

public function earth(){


$ this-> set('planets',$ this-> Planet-> find '));


}

}
?>

earth.ctp看起来像这样

 <?php echo $ this-> Html-> image('earth1.jpg'); ?> 

< h2>事实文件< / h2>
<?php foreach($ planets as $ planet):?>

< h3><?php echo $ planet ['Planet'] ['planet_name']; ?>< / h3>

< p> <?php echo $ planet ['Planet'] ['planet_info']; ?>< / p>


<?php endforeach; ?>
<! - < p> orem ipsum dolor sit amet,consectetur adipiscing elit。 Phasellus elementum mattis turpis id aliquam。 Duis vitae magna在justo varius dapibus。 Donec mattis nunc tellus,in sollicitudin risus rut​​rum in。Curabitur vehicula et iipsum vel fringilla。 Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;前庭Vestibulum venenatis是什么意思estigida是什么意思nibh elefend egestas。 Quisque vel libero convallis,vulputate sem nec,sollicitudin nisl。 < / p> - >< / p>
< / div><! - /.col-xs-12 main - >


解决方案

  $ earth = $ this-> Planet-> find('first',array('conditions'=> array('planet_name'=>'Earth'))); 
$ this-> set('earth',$ earth);

也可以使用find('all'),如果你期望更多行。 >

你应该命名'planet_id'列'id';详情请参阅 http:// book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions


EDIT: 2. I have resolved the issue! Thanks for the support guys. To see the final version, look at this pastebin and see the difference between it and my code here on StackOverflow.

http://pastebin.com/yWqs86Zt

EDIT. I now am able to print the data on screen but I am seeing all the planet data when I only want a row per ctp. (earth row on the earth.ctp etc).

New to cake and very much confused. I have looked over the documents but am still at a loss. Big props to anyone who can help a student developer out.

In all I am working on an application which shows data on each planet and moon in the solar system, passing varchars of data from the Database to the whateverplanet.ctp.

I am trying to display a row per ctp. Such as earth info on Planets/earth.ctp by calling the planet_name/info/content columns in the PlanetsController earth() function.

I have a database with two tables

  • planets table
    • planet_id (int)
    • planet_name (varchar)
    • planet_info (varchar)
    • planet_content (varchar)

And a moons table with the same set up just with moons instead of planets.

Planet Model looks like

<?php
App::uses('AppModel', 'Model'); 
class Planet extends AppModel {

        public $name = 'Planet';
        public $useTable = 'planets';
} 

?>

Controller is as this (with commented out attempts)

<?php

class PlanetsController extends AppController
{
    public $name = 'Planets';
    public $helpers = array('Html', 'Form');

    public function earth() {


         $this->set('planets', $this->Planet->find('all'));


}

}
?>

earth.ctp looks like this

    <?php echo $this->Html->image('earth1.jpg'); ?>

    <h2> Fact File </h2>
    <?php foreach ($planets as $planet): ?>

            <h3><?php echo $planet['Planet']['planet_name']; ?></h3>

            <p> <?php echo $planet['Planet']['planet_info']; ?></p>


            <?php endforeach; ?>
    <!--<p>orem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus elementum mattis turpis id aliquam. Duis vitae magna in justo varius dapibus. Donec mattis nunc tellus, at sollicitudin risus rutrum in. Curabitur vehicula et ipsum vel fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum venenatis eros gravida nibh eleifend egestas. Quisque vel libero convallis, vulputate sem nec, sollicitudin nisl. Nullam egestas orci eget velit hendrerit rhoncus.</p>-->
</div><!-- /.col-xs-12 main -->

解决方案

$earth = $this->Planet->find('first', array('conditions' => array('planet_name' => 'Earth')));
$this->set('earth', $earth);

Or you can use find('all') as well if you expect more rows.

Btw you should name the 'planet_id' column 'id'; read more at http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

这篇关于使用CakePHP 2.x获取和显示数据(解决)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆