Solved

how to unsort output text lines from a sorted input in php?

Posted on 2010-09-15
3
286 Views
Last Modified: 2012-05-10
The following code below reads sorted text lines from a file, around 30 lines and output them in sorted lines on the computer screen. But what I would like is to output the input sorted text lines unsorted without repeating the same text lines..


while(!feof($file))
{
$myfilevalue = fgets($file);
               echo '<font size="6" color="' . random_color() . '">' . $myfilevalue . '</font>';
}

function random_color()
{
    mt_srand((double)microtime()*1000000);
    $c = '';
    while(strlen($c)<6)
    {
        $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return $c;
}
fclose($file);
?>
</body>
</html>
 
0
Comment
Question by:areyouready344
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 33681886
This should work

while(!feof($file))
{
$myfilevalue[] = fgets($file);
}

if (shuffle($myfileValue)){
  foreach ($myfileValue as $v){
    echo '<font size="6" color="' . random_color() . '">' . $v . '</font>';
  }
}

Cheers

0
 

Author Comment

by:areyouready344
ID: 33682301
thanks Marqus, also, if i wanted to display ten lines instead of all 20 lines from the shuffle, i would do this..

$display_ten_lines = 10;
$count_0 = 0;

while(!feof($file))
{
$myfilevalue[] = fgets($file);
}

if (shuffle($myfileValue)){
  foreach ($myfileValue as $v){
   if($count_0 < $display_ten_lines)
  {
    echo '<font size="6" color="' . random_color() . '">' . $v . '</font>';
    $count_0++  
}  
}
}
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33686752
Awesome! Thanks for points.
Cheers
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question