gpt4 book ai didi

php - 使用 CakePHP 2.x 获取和显示数据

转载 作者:行者123 更新时间:2023-11-30 00:40:22 25 4
gpt4 key购买 nike

编辑:2.我已经解决了这个问题!要查看最终版本,请查看此 Pastebin,看看它与 StackOverflow 上我的代码之间的区别。

http://pastebin.com/yWqs86Zt

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

刚接触蛋糕,非常困惑。我已经查看了文件,但仍然不知所措。对于任何能够帮助学生开发者走出困境的人来说都是巨大的支持。

总而言之,我正在开发一个应用程序,该应用程序显示太阳系中每个行星和卫星的数据,将数据的 varchars 从数据库传递到whateverplanet.ctp。

我正在尝试为每个 ctp 显示一行。例如通过调用 PlanetsController Earth() 函数中的planet_name/info/content 列来获取 Planets/earth.ctp 上的地球信息。

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

  • 行星表
    • planet_id(整数)
    • planet_name (varchar)
    • planet_info (varchar)
    • planet_content (varchar)

还有一个具有相同设置的卫星表,只是用卫星而不是行星。

行星模型看起来像

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

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

?>

Controller 是这样的(带有注释掉的尝试)

<?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 看起来像这样

    <?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);

或者,如果您需要更多行,也可以使用 find('all')。

顺便说一句,您应该将“planet_id”列命名为“id”;欲了解更多信息,请访问 http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

关于php - 使用 CakePHP 2.x 获取和显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21819451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com