Jump to content

[SOLVED] Reverse Count and Output


Bricktop

Recommended Posts

Hi all,

 

I have the following code (thank you rhodesa ;) ) which reads a text file line by line, then outputs the results in reverse.  I would like each line number to be output also, so have added the count function but not sure how to get each line numebr to output next to the relevant item.  The code I have now is:

 

function listquotes()
{
global $settings;

$quotesfile = $settings['quotesfile'];
  	$number = count(file($quotesfile));
  	
$content = "";
  foreach(array_reverse(file($quotesfile)) as $line)
  {
    list($quote,$author) = explode("||", $line, 2);
     //Display the quote and the author
$content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />';
$content .= '<cite class="authortext">'.$author.'</cite>';
}
echo $content;
}

 

Obviously I need to output the $number variable in reverse next to each line number so I can output as:

 

$content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />';
$content .= '<cite class="authortext">'.$author.'</cite>';
$content .= '<cite class="linenumber">'.$linenumber.'</cite>';

 

Anyone have any ideas how I can achieve this?

 

Thanks very much

Link to comment
https://forums.phpfreaks.com/topic/166950-solved-reverse-count-and-output/
Share on other sites

<?php
function listquotes() {
global $settings;
$quotesfile = $settings['quotesfile'];
$lines = array_reverse(file($quotesfile));
$linecount = count($lines);
$content = '';
foreach($lines as $line) {
	list($quote, $author) = explode('||', $line, 2);
	//Display the quote, author and line number
	$content .= '<blockquote class="quotetext">' . $quote . '</blockquote><br /><br />';
	$content .= '<cite class="authortext">' . $author . '</cite>';
	$content .= '<cite class="linenumber">' . $linecount-- . '</cite>';
}
echo $content;
}
?>

 

Note: $linecount-- returns $linecount and then decreases it by 1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.