在排名表中创建计数器


Create a Counter in a ranking table

我有一个文章表,按访问次数排列,访问次数最多的文章位于顶部,即它的Rank为1。我试着在表中添加一个秩列,但不能让它工作。Rank列在数据库中不存在,所以这应该是一个增量计数器,从1开始,以表中列出的文章数结束。这是我的代码,它不能工作

  <table class="resultsTable" width="100%">
   <tr>
     <th style="width:10%">Rank</th>
     <th style="width:50%">Post Title</th>
     <th style="width:20%">Post Type</th>
     <th style="width:20%">Hits</th>
   </tr>

   <?php foreach($popular_articles as $article) { 
            //create array for most popular
          ?>
//set counter variable
$counter = 1; 
   while($row = mysql_fetch_array($result))
   <tr>
      echo "<td style="width:10%">".$counter."</td>";
     <td style="width:50%"><?php echo $article->art_title; ?></td>
     <td style="width:20%"><?php echo $article->art_type; ?></td>
     <td style="width:20%"><?php if($article->art_hit_count){ echo '<b>'.$article->art_hit_count.'</b>'; }else { echo '<b>0</b>'; } ?> times.</td>
   </tr>
     $counter++; //increment counter by 1 on every pass 
 echo  <?php } ?>
  </table>

增加计数器必须在PHP中完成。目前这一行在HTML:

$counter++; //increment counter by 1 on every pass 

我还删除了while,因为它似乎没有做任何事情,好像它被意外地粘贴在那里。循环应该是这样的:

<?php 
  foreach($popular_articles as $article) { 
  //set counter variable
  $counter = 1; 
   <tr>
     <td style="width:10%"><?php echo $counter; ?></td>";
     <td style="width:50%"><?php echo $article->art_title; ?></td>
     <td style="width:20%"><?php echo $article->art_type; ?></td>
     <td style="width:20%"><?php 
  if($article->art_hit_count){ 
    echo '<b>'.$article->art_hit_count.'</b>'; 
  }else { 
    echo '<b>0</b>'; 
  } ?> times.</td>
   </tr>
<?php
   $counter++; //increment counter by 1 on every pass 
} ?>

或者,您可以显示计数器并一次更新它:

<td style="width:10%"><?php echo $counter++; ?></td>";
     ...
<?php
   // $counter++; // You won't be needing this anymore.
} ?>

我想你错过了在循环之前初始化计数器(在注释中,我可以看到$counter = 1,但它确实被执行了。

最后,你尝试增加一个变量,但你不是在PHP脚本中,所以它不能工作。试试我的更正版,看看是否有效。

<table class="resultsTable" width="100%">
    <tr>
        <th style="width:10%">Rank</th>
        <th style="width:50%">Post Title</th>
        <th style="width:20%">Post Type</th>
        <th style="width:20%">Hits</th>
    </tr>
    <?php 
        $counter = 1;
        foreach($popular_articles as $article) { 
        //create array for most popular 
    ?>
    <tr>
        <td style="width:10%"> <?php echo $counter; ?></td>;
        <td style="width:50%"> <?php echo $article->art_title; ?></td>
        <td style="width:20%"> <?php echo $article->art_type; ?></td>
        <td style="width:20%"> 
            <?php if($article->art_hit_count){ 
                    echo '<b>'.$article->art_hit_count.'</b>'; 
                } else { 
                    echo '<b>0</b>'; 
                } ?> times. </td>
    </tr>
    <?php 
        $counter++; //increment counter by 1 on every pass 
        } //the end of FOR loop ?>
</table>

编辑

如果你想为art_title。只需替换这一行:

<td style="width:50%"> <?php echo $article->art_title; ?></td>

By this:

<td style="width:50%"><a href="your-link.com/page-you-wanna-show.php"> <?php echo $article->art_title; ?></a></td>