Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

PHP - Sort data and display

(*I really have tried to get this working on my own - without any success.)

Here's what I'm trying to do...
I'm trying to sort data alphabetically by the COMPANY NAME from a flat file. I was thinking that a simple sort would handle this, but I was wrong. I now think that I need to use usort and cmp ??? and then create another temp array ??? Or ???

How can I accomplish this?   ... and please ... include the output/echo of these results.

As always, thank you so much.


This is my current code.

$data_file = fopen($data_file_url, "r");
fgets($data_file); // IGNORE FIRST LINE IN FLATFILE - column names
while (!feof($data_file) ) {
    $data_lines = fgets($data_file);
    $data_ele = explode('|', $data_lines);

    $company_name = $data_ele[0];
    $section = $data_ele[1];
    $category = $data_ele[2];
    $service = $data_ele[3];
    $service = substr_replace($service,"",-1); // trim last char
}

Open in new window

0
mar2195
Asked:
mar2195
  • 6
  • 3
1 Solution
 
Ray PaseurCommented:
We don't need the code that is not working.  It's a good reference point, but what we really need instead is the test data set.  Please post that here, and show us how you want the output to be sorted.  I'll be glad to show you a tested-and-working example once I have some test data!  You can upload the test file as an attachment to your comment here, and I can read it directly from the EE web site, so the "simulation" will be as close to real-world as is practically possible!  

This is a useful concept: SSCCE.  Best regards, ~Ray
0
 
Julian HansenCommented:
This should get you going in the right direction
$data_file_url = 'lines.csv';
// Read all the lines into a variable
$data = file_get_contents($data_file_url);
// break them up into an array of lines
$lines = explode("\r\n", $data);
// remove the first line
unset($lines[0]);
// run a standard sort on the array - the lines will be in the right order after this
sort($lines);
// Dump to check
print_r($lines);
// no loop through the sorted array exploding each line as you go to do with what you need inside
foreach($lines as $line) {
	$temp = explode('|', $line);
}

Open in new window


It was not clear from your post what you want to do with the data - following on from what your code does it means you could have duplicate company / section / category combinations - repeated for each service.

If you can give us more detail on what the next step is with respect to what you want to do with the data then we can take it from there.
0
 
Ray PaseurCommented:
Or maybe just this.  And once we have the test data we can actually test -- what a novel idea ;-)

<?php // RAY_temp_mar2195.php
error_reporting(E_ALL);

// LOCATION OF THE DATA <-- THIS IS WHAT WE REALLY NEED FROM THE AUTHOR OF THE QUESTION
$url = '?';

// READ THE DATA INTO AN ARRAY
$arr = file($url);

// IGNORE FIRST LINE
unset($arr[0]);

// SORT THE ARRAY
natcasesort($arr);

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($arr);

Open in new window

Best to all, ~Ray
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mar2195Author Commented:
I sincerely apologize for not including sample data.  I thought that the $data array listing/assignments would allow others to 'see' what the data would be.

DATA:
company_name|section|category|service
20Company New|Find A Company|Tech Company|Modems
Agile Company, Inc.|Find A Company|Tech Company|Adapters
Fire, Inc.|Find A Company|Tech Company|Phones
Zarn Active|Find A Company|Tech Company|Cases
DSC Solutions|Find A Different Company|Low Tech Company|Boards


** I have tested a number of examples.  For some reason, I've yet to be able to 'echo' the data correctly.
0
 
Ray PaseurCommented:
Here is the data in a file to give the best simulation.
mar2195.txt
0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_mar2195.php

<?php // RAY_temp_mar2195.php
error_reporting(E_ALL);

// LOCATION OF THE DATA
$url = 'http://filedb.experts-exchange.com/incoming/2013/05_w22/657278/mar2195.txt';

// READ THE DATA INTO AN ARRAY
$arr = file($url);

// IGNORE FIRST LINE
unset($arr[0]);

// SORT THE ARRAY
natcasesort($arr);

// SHOW THE WORK PRODUCT
foreach ($arr as $str)
{
    echo $str . '<br>' . PHP_EOL;
}

Open in new window

HTH, and please post back with any questions, ~Ray
0
 
mar2195Author Commented:
!!! AWESOME - Thanks Ray.  I realize that I post some newbie stuff.  But that's why I pay EE.  So thanks for your patience.  It's greatly appreciated.

@julianH - - Thank you also.
0
 
Ray PaseurCommented:
Thanks for the points!

Highly recommended for any one getting started in PHP.  I believe that a new edition of the book is scheduled to be out soon.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/

A semi-related article showing the value of test data to the software development process.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

All the best, ~Ray
0
 
mar2195Author Commented:
Ray...

One last question...

If I need to access each individual element in a record in the $arr array, how do I 'get' to it?  I was able to 'get' to it using my original code.  With your new method, I don't know how to access (for example) the  'company_name'.


Thanks.
0
 
Ray PaseurCommented:
Easy.  You use explode() to break each line into component parts.  Your original script seemed to be good in this regard.

<?php // RAY_temp_mar2195.php
error_reporting(E_ALL);

// LOCATION OF THE DATA
$url = 'http://filedb.experts-exchange.com/incoming/2013/05_w22/657278/mar2195.txt';

// THE DELIMITER IN THE ROWS
$dlm = '|';

// READ THE DATA INTO AN ARRAY
$arr = file($url);

// IGNORE FIRST LINE
unset($arr[0]);

// SORT THE ARRAY
natcasesort($arr);

// SHOW THE WORK PRODUCT
echo '<pre>';
foreach ($arr as $str)
{
    $new = explode($dlm, $str);
    print_r($new);

    // ISOLATE THE COMPANY NAME
    echo $new[0] . ' IS COMPANY NAME';
    echo PHP_EOL;
    echo PHP_EOL;
}

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now