compjab
02-23-2006, 05:35 PM
Last question for a while....
I have a flat file set up somewhat like the folllowing
$id|$first|$last
$id|$first|$last
$id|$first|$last
When the admin goes to view the list of users, they are populated with a while(); statement - Thus, they show in the order in which they were added to the file..
Is there a way to go through the entire file, alphabetize by $last and then show the info?? I've used the following to show each user:
$asize = count($array);
$x = 0;
while ($x < $asize) {
list ($id,$first,$last) = explode ("|", $array[$x])
echo "$id is this & $first is this & $last is this";
$x++;
}
Maybe a little unconventional? but it works.. So again, I guess what I am trying to do is modify (or do something new altogether) so that the data is displayed in ABC order
Thanks!
compjab
02-23-2006, 07:47 PM
This is good and it works for the first variable in each line of the array... except what if I need to alphabetize based on another variable on the line.. e.g.,
$id|$first|$last >>> I want to alphabetize based on last
I wrote the following code but it alphabetizes based on $id
<?php
$array = file("test.txt");
sort($array);
foreach ($array as $key => $val) {
echo "$val" . "<BR>";
}
?>
Any idea on how to not change the structure of the file yet alphabetize based on $last
realwebsolution
02-23-2006, 07:55 PM
Load the text file to array and use asort() or ksort() PHP functions.
http://php.net/manual/en/function.asort.php
http://php.net/manual/en/function.ksort.php
mitchlrm
02-23-2006, 08:08 PM
This is probably what you're looking for http://us2.php.net/manual/en/function.array-multisort.php
laserlight
02-24-2006, 01:19 AM
In this case, I would use usort(), e.g.
//compare using the 3rd element of the array
function cmpLast($a, $b) {
return strcmp($a[2], $b[2]);
}
//read from file into array
$array = file("test.txt");
$entries = array();
for ($i = 0; $i < count($array); ++$i) {
//rtrim() as file() returns with trailing newline
$entries[] = explode('|', rtrim($array[$i]));
}
//sort based on 3rd element of array
usort($entries, 'cmpLast');
print_r($entries);
compjab
02-24-2006, 01:51 AM
All working well now! very excited!
thrilom
05-15-2009, 12:10 PM
</style>
<head></head>
<body>
<table>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Address</th>
<th>City</th>
<th>Zip-Code</th>
<th>State</th>
<th>Email</th>
<th>Gender</th>
<th>Job</th>
<th>Add_on[]</th>
<th>Comments<span class="style1"></span></th>
</tr>
<?
$gb_file = file( 'show_guests.txt' );
foreach( $gb_file as $line ) {
$bgc = "#cccccc";
if (++$i % 2) {
$bgc = "#9999cc";
}
$line = rtrim( $line );
$field = explode( "\t", $line );
echo( "<tr bgcolor=\"$bgc\">\n" );
echo( "<td>" . $field[0] . "</td>\n" );
echo( "<td>" . $field[1] . "</td>\n" );
echo( "<td>" . $field[2] . "</td>\n" );
echo( "<td>" . $field[3] . "</td>\n" );
echo( "<td>" . $field[4] . "</td>\n" );
echo( "<td>" . $field[5] . "</td>\n" );
echo( "<td><a href=\"mailto:$field[6]\">$field[6]</a></td>" );
echo( "<td>" . $field[7] . "</td>\n" );
echo( "<td>" . $field[8] . "</td>\n" );
echo( "<td>" . $field[9] . "</td>\n" );
echo( "<td>" . $field[10] . "</td>\n" );
echo( "</tr>" );
}
?>
----------------------------------------------------------------
In this case, I would use usort(), e.g.
//compare using the 3rd element of the array
function cmpLast($a, $b) {
return strcmp($a[2], $b[2]);
}
//read from file into array
$array = file("test.txt");
$entries = array();
for ($i = 0; $i < count($array); ++$i) {
//rtrim() as file() returns with trailing newline
$entries[] = explode('|', rtrim($array[$i]));
}
//sort based on 3rd element of array
usort($entries, 'cmpLast');
print_r($entries);
alons
05-17-2009, 12:46 AM
I would recommend that you use a Database like MySQL.
This is because it has inbuilt functions of sorting
The reason MySQL or a database is simply that when you TXT file becomes very big it will start to cause unncecssary load on PHP and your pages will take longer to process. In some cases even die!
Also since its a TXT file anyone can access it.
So you better protect it with a .htaccess
If you still want to do it using file based systems, then I recommend you do the sorting while inserting a RECORD into the file.
So sorting is done only when its inserted and not when someone sees the list
HivelocityDD
05-17-2009, 05:49 AM
I agree with alons. Its always better to use mysql instead of txt file