Rails to PHP每个sql查询

I am new to PHP and am in the process of creating a blog. I was implementing a slider and I was just wondering how to have a Rails-like .each method.

In Rails, the following is possible

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

How would I be to translate this from Ruby code to PHP, where <div class="item"> [...] </div> is looped based on the number of posts in the Database. Thanks

foreach is on php. you just need a data structure to iterate. That's all, no magic there.

Use foreach

Your code in PHP would look something like the following:

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