Solved

PHP - Sort data and display

Posted on 2013-05-30
10
440 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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Here is the data in a file to give the best simulation.
mar2195.txt
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
!!! 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

10 Experts available now in Live!

Get 1:1 Help Now