简体   繁体   English

一个do-while循环中的Simplexml计数

[英]Simplexml count inside a do-while loop

I am trying to do a simplexml count inside a do-while loop, yes its simple but the count is not correct as it is inside 2 loops. 我正在尝试在do-while循环内进行simplexml计数,是的,它很简单,但是计数不正确,因为它在2个循环内。 Is there a easier way to do the count? 有没有更简单的方法来计数?

$query_fl = "SELECT * FROM table";
$fl = mysql_query($query_fl, $rf) or die(mysql_error());
$row_fl = mysql_fetch_assoc($fl);

do {
    $xml = $row_fl['fu'];
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($xml);

    $rss = simplexml_load_file($xml);
    $items = $rss->channel->item;

    foreach($items as $item){
        echo $rss->channel->title;
        echo $feed_count."<BR>";
        $feed_count++;
    }

    $feed_count=1;

} while ($row_fl = mysql_fetch_assoc($fl)); 

------------- The output is like this ------------- 输出是这样的

Feed 1 - Count-1
Feed 1 - Count-2
Feed 1 - Count-3
Feed 1 - Count-4
Feed 1 - Count-5

Feed 2 - Count- 1
Feed 2 - Count- 2
Feed 2 - Count- 3
...
..

--------------- I want the output as --------------- 我希望输出为

   Feed 1 - Count-5
   Feed 2 - Count-3

You should be using mysqli instead of mysql_* as that is now depricated, but moving on... You have a couple of options, either: 您应该使用mysqli而不是mysql_ *,因为现在已经描述了它,但是请继续...有两种选择,或者:

1) move $feed_count=1; 1)移动$ feed_count = 1; to before the do loop for a single count of all items. 到do循环之前,以仅计数所有项目。

or 要么

2) make an array of counts for the foreach loop, incrementing the array index each time you exit the foreach loop. 2)为foreach循环创建一个计数数组,每次退出foreach循环时都会递增数组索引。 This will give you an array of counts for your items. 这将为您提供一系列商品计数。 If you use a key derived from an item variable eg its name, then you can count the number of occurences of an item type, even if they are not all together in your xml. 如果您使用从项目变量派生的键(例如其名称),则可以计算项目类型的出现次数,即使它们在您的xml中不是全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM