Solved

PHP Flatfile Sorting Causing A Warning

Posted on 2014-12-04
3
174 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
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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This article discusses four methods for overlaying images in a container on a web page
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

809 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