Php 从数组中为每个元素递增数字

Php 从数组中为每个元素递增数字,php,jquery,arrays,count,Php,Jquery,Arrays,Count,我昨天没有得到这个问题的答案 所以我尝试了一个更简单的解决方案。我想更改此代码: $news_countpp = 6; $result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC LIMIT ".($news_page * $news_countpp).", $news_countpp"); while($row = mysqli_fetch_array($result)) { echo "<div><

我昨天没有得到这个问题的答案

所以我尝试了一个更简单的解决方案。我想更改此代码:

$news_countpp = 6;
$result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC LIMIT ".($news_page * $news_countpp).", $news_countpp");
while($row = mysqli_fetch_array($result))
{
echo "<div><a href='#".$row['id']."' class='cross-link'><img src='/assets/images/news/news_".$row['id']."_slider_thumb.jpg' alt='".$row['news_img_alt']."'  class='nav-thumb' alt='temp-thumb' style='width:60px; height:40px;'></a></div>";
}
$news\u countpp=6;
$result=mysqli\u query($con,“按id描述限制从tbl\u新闻顺序中选择*”。($news\u页面*$news\u countpp)。“,$news\u countpp”);
while($row=mysqli\u fetch\u数组($result))
{
回声“;
}

部分
我猜您在
while
循环的每次迭代中都会遍历整个数组。相反,在
while
循环之前,将数组和计数器变量初始化为
0
,并且每次迭代只访问一个元素(这样做时计数器会增加一个)。

$news\u countpp=6;
$result=mysqli\u query($con,“按id描述限制从tbl\u新闻顺序中选择*”。($news\u页面*$news\u countpp)。“,$news\u countpp”);
$i=1
while($row=mysqli\u fetch\u数组($result))
{
回声“;
$i++;
}

像这样的东西怎么样

$i = 0;
while($row = mysqli_fetch_array($result)) {
  $i++;
  echo 
    "<div>",
      "<a href='#".$row['id']."' class='cross-link'>",
        "<img src='/assets/images/news/news_", $row[$i], "_slider_thumb.jpg'
          alt='".$row['news_img_alt']."'  
          class='nav-thumb' alt='temp-thumb' 
          style='width:60px; height:40px;'>",
      "</a>",
    "</div>";
}
$i=0;
while($row=mysqli\u fetch\u数组($result)){
$i++;
回音
"",
"",
"";
}

如果您的问题因需要重写而无法获得答案,则建议您重写现有问题。除非你的问题得到的答案导致了后续问题,否则继续发布附加问题通常被认为是不好的形式。@GordonM这不是该问题的重新发布,所以我不确定将其标记为重复是否有意义。是的,他们本可以完全改变另一个问题,因为它没有答案,但提出一个新问题也是有效的,因为它是另一个问题。你想实现这样的目标吗:@GordonM:我已经重新表述了最后一个问题,但仍然没有得到答案。正如Anthony Grist已经说过的,我在这里尝试了一个完全不同的解决方案,现在得到了我需要的答案
和第三个
$i--
$news_countpp = 6;
$result = mysqli_query($con,"SELECT * FROM tbl_news ORDER BY id DESC LIMIT ".($news_page * $news_countpp).", $news_countpp");
$i = 1
while($row = mysqli_fetch_array($result))
{
echo "<div><a href='#".$i."' class='cross-link'><img src='/assets/images/news/news_".$row['id']."_slider_thumb.jpg' alt='".$row['news_img_alt']."'  class='nav-thumb' alt='temp-thumb' style='width:60px; height:40px;'></a></div>";
$i++;    
}
$i = 0;
while($row = mysqli_fetch_array($result)) {
  $i++;
  echo 
    "<div>",
      "<a href='#".$row['id']."' class='cross-link'>",
        "<img src='/assets/images/news/news_", $row[$i], "_slider_thumb.jpg'
          alt='".$row['news_img_alt']."'  
          class='nav-thumb' alt='temp-thumb' 
          style='width:60px; height:40px;'>",
      "</a>",
    "</div>";
}