Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

25 Experts available now in Live!

Get 1:1 Help Now