English 中文(简体)
Php: How to Add odd/even loop to my unordered list
原标题:Php : How to add odd/even loop to my unordered list
  • 时间:2010-08-10 03:05:14
  •  标签:
  • php
  • css
  • xhtml

这方面的一个例子是我的言辞。 我愿增加以下几类:<代码>的最后一个;><li>。

页: 1

<ul class="tabs">
 <?php
  global $post;
  $myposts = get_posts( numberposts=3 );
  foreach($myposts as $post) :
  setup_postdata($post);
  ?>
 <li><a href="#"><?php the_title(); ?></a></li>
 <?php endforeach; ?>         
</ul>

我想取得的结果如下:

<ul>
 <li>Title 1</li>
 <li>Title 1</li>
 <li class= lastli >Title 1</li>
<ul>

任何最后一份无序名单都将是<代码><li等级=最后值;。 让我知道如何做到这一点?

最佳回答

a. 使用 lo

<ul class="tabs">
 <?php
  global $post;
  $myposts = get_posts( numberposts=3 );
  $nposts = count($myposts);
  for($i=0;$i<$nposts;$i++):
    $post = $myposts[$i];
    setup_postdata($post);
  ?>
 <li<?php if ($i==$nposts-1):?> class= lastli <?php endif;?>><a href="#"><?php the_title(); ?></a></li>
 <?php endfor; ?>         
</ul>

说明:在休息时间之前计及你的阵容大小是良好做法,否则,php将在每轮休息时间评价。

问题回答
<ul class="tabs">
 <?php
  global $post;
  $myposts = get_posts( numberposts=3 );
  $nposts = count($myposts);
  $odd_even_class = array( odd_class ,  even_class );

  for($i=0;$i<$nposts-1;$i++):
    $post = $myposts[$i];
    setup_postdata($post);
  ?>
 <li <?php echo $odd_even_class[($i+1)%2];?>><a href="#"><?php the_title(); ?></a></li>
 <?php 
endfor; 
 $post = $myposts[$i];
 setup_postdata($post);

 <li class= lastli ><a href="#"><?php the_title();?></a></li>         
</ul>

www.un.org/Depts/DGACM/index_spanish.htm 页: 1

<ul class="tabs">
 <?php
  global $post;
  $myposts = get_posts( numberposts=3 );
  $i = 0;
  for ($i = 0; $i < count($myposts); $i++) {
  $post = $myposts[$i];
  setup_postdata($post);
  ?>
 <li <?= ($i==count($myposts)-1)?"class= lastli ":"" ?>><a href="#"><?php the_title(); ?></a></li>
 <?php } ?>         
</ul>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...