Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

735 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