Solved

PHP Flatfile Sorting Causing A Warning

Posted on 2014-12-04
3
181 Views
Last Modified: 2014-12-04
I've used this code before without any issues.  I must be overlooking something here...  hoping that a few more eyes can help me see what I'm doing wrong.  (Hosting is using PHP Version 4.4.9)

Here is the warning that I'm getting:
Warning: natcasesort() [
function.natcasesort
]: The argument should be an array


I have tried using:
natcasesort($file)
natcasesort($lines)
RESULT:  Same warning.

FYI: This WARNING displays at each items' display.  (Yes, the data is still creating the flatfile output unsorted.)

<?php

/* SAMPLE RECORDS FROM FLATFILE:
Zees|Alan Zees, Ph.D.|http://www.alanzees.com|end_record
Aarons|Ben Aarons, M.F.T.|http://www.benaarons.com|end_record
*/

$file = fopen("includes/related_sites_data.txt", "r");
	if (!$file) { echo 'ERROR: Unable to open file: <strong>'.$file.'</strong>'; }

while (!feof($file) ) {
	$lines = fgets($file);

	natcasesort($file);

	$ele = explode('|', $lines);

	if($ele[0]){
		$db_sort_name = $ele[0];
		$db_display_name = $ele[1];
		$db_site = $ele[2];
		$db_end_record = $ele[3];
	
		$db_end_record = substr_replace($db_end_record,"",-1); // trim last char
?>

					<li><a href="<?php echo $db_site;?>" target="_blank" title="<?php echo $db_display_name;?>"><?php echo $db_display_name;?></a></li>

<?php

	}
}
fclose($file);

?>

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
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40482129
You are passing a string into natcasesort when it expects an array

Why not put the split lines into a multi dimensional array and then sort the array when all lines have been processed.

http://php.net/manual/en/function.fgetcsv.php
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40482184
Hosting is using PHP Version 4.4.9
This puts you about a decade or more behind the times.  Consider upgrading -- you would not want to be driving a car that old -- it's unsafe.

When you do not know what kind of data your PHP script is using, you can (and should) find out with var_dump().
0
 

Author Closing Comment

by:mar2195
ID: 40482194
1.  Gary:  "split lines into a multi dimensional array and then sort the array"  Yep.  That worked.  Thanks.

2.  Ray:  Unfortunately, I do not have control of the installs (ie:  PHP version) on this server.  Of course, I would have advised a PHP upgrade.  I can only imagine the other issues going on with old 'everything'.
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

724 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