Solved

PHP - Sort data and display

Posted on 2013-05-30
10
444 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 108

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 52

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 108

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
 

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 108

Expert Comment

by:Ray Paseur
ID: 39207679
Here is the data in a file to give the best simulation.
mar2195.txt
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 108

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 108

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 108

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

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

867 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

17 Experts available now in Live!

Get 1:1 Help Now