Solved

PHP - Sort data and display

Posted on 2013-05-30
10
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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
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!

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

751 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