Solved

PHP - Sort data and display

Posted on 2013-05-30
10
450 Views
Last Modified: 2013-05-30
(*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
Comment
Question by:mar2195
  • 6
  • 3
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39207485
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
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 39207599
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39207625
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:mar2195
ID: 39207655
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39207679
Here is the data in a file to give the best simulation.
mar2195.txt
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39207687
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
 

Author Comment

by:mar2195
ID: 39207716
!!! 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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39207731
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
 

Author Comment

by:mar2195
ID: 39207760
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39207775
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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