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

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

问题描述

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

2. I have resolved the issue! 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

编辑。现在,我可以在屏幕上打印数据,但是当我仅希望每ctp行时,我可以看到所有的行星数据。 (地球上的地球行。ctp等)。

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.

我正在开发一个应用程序,该应用程序显示太阳系中每个行星和月球的数据,并通过varchars。从数据库到anyplanet.ctp的数据。

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.

我试图显示每ctp行。例如,通过调用PlanetsController earth()函数中的planet_name / info / content列,即可访问Planets / earth.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


  • 行星表


    • planet_id(int)

    • planet_name(varchar)

    • planet_info(varchar)

    • planet_content(varchar)

    • 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.

      行星模型看起来像

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

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

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

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

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

      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天全站免登陆