Solved

PHP Flatfile Sorting Causing A Warning

Posted on 2014-12-04
3
170 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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

746 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

12 Experts available now in Live!

Get 1:1 Help Now