Rails到PHP的每个sql查询

Rails到PHP的每个sql查询,php,sql,ruby-on-rails,ruby,each,Php,Sql,Ruby On Rails,Ruby,Each,我是PHP新手,正在创建博客。 我正在实现一个滑块,我只是想知道如何拥有像。each方法这样的Rails 在Rails中,以下是可能的 <div class="main-slider section"> <% @posts.each do |post| %> <div class="item"> <a href="<%= post.the_permalink %>" >

我是PHP新手,正在创建博客。 我正在实现一个滑块,我只是想知道如何拥有像
。each
方法这样的Rails

在Rails中,以下是可能的

<div class="main-slider section">
  <% @posts.each do |post| %>                       
    <div class="item">
      <a href="<%= post.the_permalink %>" >
        <%= post.the_post_thumbnail %>
      </a>
    <div class="post-info">
       <div class="title">
          <h2><a href="<%= post.the_permalink %>"><%= post.title %></a></h2>
        <div class="sep"></div>
       </div>                
             <div class="post-excerpt">
                    <% if post.excerpt.length > 150 %>
                      <%= truncate(post.excerpt, length: 150) %>
                    <%= link_to_function 'Read more', "$(this).parent().html('#{escape_javascript post.excerpt}')" %>
                    <% else %>
                      <%= post.excerpt %>
                      <% end %>         
                  </div>
                  <div class="read-more">
                    <a href="<%= post.the_permalink %>">Read more</a>
                  </div>
                </div>  
              </div>    
</div>

150 %>
我将如何将这一点从Ruby代码转换为PHP,在PHP中,
[…]
根据数据库中的帖子数量进行循环。
谢谢你,foreach在php上。您只需要一个数据结构来进行迭代。仅此而已,这里没有魔法。

使用
foreach

您的PHP代码如下所示:

<div class="main-slider section">
  <?php foreach($posts as $post): ?>                       
    <div class="item">
      <a href="<?php echo $post->the_permalink ?>" >
        <?php echo $post->the_post_thumbnail ?>
      </a>
    <div class="post-info">
       <div class="title">
          <h2><a href="<?php $post->the_permalink ?>"><?php $post->title ?></a></h2>
        <div class="sep"></div>
       </div>                
             <div class="post-excerpt">
                    <?php if($post->excerpt->length > 150): ?>
                      <?php substr($post->excerpt, 0, 150) ?>
                    <a href='#' onclick="$(this).parent().html($post->excerpt)" >Read more</a>
                    <?php else: ?>
                      <?php $post->excerpt ?>
                      <?php endif; ?>         
                  </div>
                  <div class="read-more">
                    <a href="<?php $post->the_permalink ?>">Read more</a>
                  </div>
                </div>  
              </div> 
  <?php endforeach;?>   
</div>

使用