Solved

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

Posted on 2010-09-15
3
279 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 30

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 30

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now