Jump to content

sorting by lastname from flat file database


bentwiener

Recommended Posts

it is displaying the lastnames in the order they are in the profile but do not know  :shrug: how to get it to sort by lastname - please help :'(

 

<?php

$lines = file('database/profile.txt');

foreach($lines as $thisline){list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{

$names []= $p_last.', '.$p_first.' '.$p_middle.'.';

foreach($names as $value);

{echo ''.$value.'<br>';}}}

?>

Link to comment
Share on other sites

Why not use a database there free......

(just asking?)

 

i find flat file programming so old hat surprised to see it still programmed.

 

what you mean sort via last name?

 

do you mean, you want all profile sorted via the last name, of the user, in alphabetical order.

 

you need to use  sort()

http://php.net/manual/en/function.sort.php

Link to comment
Share on other sites

im using flatfile only because i feel it will work better for the application im using.

 

yes - i would like to sort by last name in alphabetical order.

 

http://php.net/manual/en/function.sort.php

 

been there and inserted sort() in every way i could think

the error states - looking for array only could find string

 

i try to never ask for help. i have been searching the net for 3 weeks and found lots of sort() functions

i have tried every sort() function that i find and only get errors.

Link to comment
Share on other sites

<?php

$lines = file('database/profile.txt');

foreach($lines as $thisline){list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{

$names = array();

$names []= $p_last.', '.$p_first.' '.$p_middle.'.';

foreach($names as $value);

{sort($value);echo ''.$value.'<br>';}

}}?>

 

the code above gives me error:

Warning: sort() expects parameter 1 to be array, string given

Link to comment
Share on other sites

<?php

$lines = file('database/profile.txt');

foreach($lines as $thisline){list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{

$names = array();

$names []= $p_last.', '.$p_first.' '.$p_middle.'.';

sort ($names);

foreach($names as $value);

{echo ''.$value.'<br>';}

}}?>

 

this only list data in order placed in databse

Link to comment
Share on other sites

I didn't spend much time on it because I believe that an actual database is not only safer but the better option in most circumstances. However, the code below works.

 

<?php 
$lines = file('database/profile.txt');

$data = array();
foreach($lines as $thisline){
	list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);
	$data[$p_last.", ".$p_first." ".$p_middle][] = trim($thisline);
}

sort($data);

// Rebuild the array.
$res = array();
foreach($data as $row){
	foreach($row as $line){
		$res[] = $line;
	}
}

var_dump($res);
?>

Link to comment
Share on other sites

"the code above gives me error:

Warning: sort() expects parameter 1 to be array, string given "

 

Of course it does, you're passing a single variable as a parameter.

 

 

<?php
$lines = array('bFirst Name | bMiddle Name | bLast Name', 'aFirst Name | aMiddle Name | aLast Name', 'cFirst Name | cMiddle Name | cLast Name');

$data = $sorted = array();
foreach ($lines AS $thisline)
{
	$thisline = explode(' | ', $thisline);
	$data[] = $thisline[2];

	// only if you want to rebuild old array with new array order
	$sorted["$thisline[2]"] = $thisline[0] . ' | ' . $thisline[1] . ' | ' . $thisline[2];
}
sort($data);

echo '<pre>';
// return array of last names sorted
var_dump($data);

// rebuilding old array data into new array
$newdata = array();
foreach ($data AS $thisdata)
{
	$newdata[] = $sorted["$thisdata"];
}

var_dump($newdata);
echo '</pre>';
?>

 

I'm assuming $lines is an array of course, I didn't bother with actually using a file since I'm assuming it'll produce the same results. This might not be the prettiest way to do it, but it's fast and gets the job done.

Link to comment
Share on other sites

the code above just explodes the entire database

 

Yes, it does - this is because of the following line:

 

var_dump($res);

 

You're then free to do what you wish with the $res variable. For example..

 

foreach($res as $thisline){
list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);
echo $p_last.", ".$p_first." ".$p_middle."<br/>";
}

 

The code I've provided shows how you how to sort your database, the rest is up to you.

 

Also, you could use Pandemikk's method but it's basically what I've done but a bloated version of it.

Link to comment
Share on other sites

Indeed it is. I'm a bit ashamed, actually, since a much more simpler and seemingly obvious solution came to me.

 

$lines = array('bFirst Name | bMiddle Name | bLast Name', 'aFirst Name | aMiddle Name | aLast Name', 'cFirst Name | cMiddle Name | cLast Name');

$data = array();
foreach ($lines AS $item)
{
	$item = explode(' | ', $item);
	$data[] = $item[2] . ' | ' . $item[1] . ' | ' .  $item[0];
}
sort($data);

 

Although Mike's may be a tad bit more efficient if sort() parses the entire string. I'd assume it'd only evaluate the first character, though, so it shouldn't matter in that case. Mike's certainly would require less editing on your part, though.

 

Anyway, just wanted to post a better solution than the one I had up, since it's shamefully bloated.

 

 

p.s: These anti-spam measures are annoying. Really deter me from being active here.

Link to comment
Share on other sites

Pandemikk - there is going to be over 500 profiles and 300 catagories, i have condensed the catagories from 300 to 8 to make it smaller to post, + i would need to add to the code everytime i add to the profile. making more work for me.

 

MikeDean89 - your code only sorts by the $p_id, whitch i can do by just adding sort($lines); to the code that i already have like this.

 

<?php

$lines = file('database/profile.txt');

sort($lines);

foreach($lines as $thisline){list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{

$names []= $p_last.', '.$p_first.' '.$p_middle.$p_id.'.';

foreach($names as $value);

{echo ''.$value.'<br>';}}}

?>

 

the code is going to be used in a drop down box like this:

 

Person <select name="person">

<option value=""></option>

<?php

$lines = file('database/profile.txt');

foreach($lines as $thisline){list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{

$names []= $p_last.', '.$p_first.' '.$p_middle.$p_id.'.';

foreach($names as $value);

{echo '<option>'.$value.'</option>';}

}}?>

</select>

 

your help is not unappreciated, i thank you both for your help. but the code i have works great, i just need it to sort by $p_last.

with over 500 profiles it would take allot of time to find a name in a drop box if they are not in alphabetical order.

 

 

Link to comment
Share on other sites

<?php 

$db = array(
'Tom | Michael | Johnson',
'Joe | William | Adams',
'Doug | David | Saunders',
'Joe | Thomas | Adams',
);

/*
Advantage
	* Maintains set order that you seem to prefer (easy to foreach through)
Disadvantage
	* Requires redundancy, a separate array for each column you want to sort by (costs memory)
*/

$data = array(); $first = array(); $middle = array(); $last = array();
foreach( $db as $key => $row ) {
$cols = explode( ' | ', $row );
$first[$key] = $cols[0]; $middle[$key] = $cols[1]; $last[$key] = $cols[2];
$data[$key] = array( 'fname' => $cols[0], 'mname' => $cols[1], 'lname' => $cols[2] );
}

array_multisort( $last, $first, $middle, $data );
unset( $last, $first, $middle ); // clear the memory

print_r( $data ); echo "\n";

?>

 

If you don't care about sorting by middle or first, feel free to take them out.

 

If you database gets big enough, it might be smarter (memory wise) to rebuild the file when a new user is added, placing him in the proper spot alphabetically. This will save you from having to sort on request (happens often) and instead you sort on new user creation (happens less often). If you need to sort by some other value more often then last name, I'd suggest sorting the file by that value instead.

Link to comment
Share on other sites

your help is not unappreciated, i thank you both for your help. but the code i have works great, i just need it to sort by $p_last.

 

And what do you think our provided code is doing? It's up to you to then use the sorted data yourself how you want it! In order for it to sort by last name, you will need the last name to be the first thing in the string. After its sorted you can do reformat the string in whatever order you want. If you're looking for help, you can start by actually using it. Just because the code looks different than yours, doesn't mean it won't work. You can't sort $lines by last name without first altering each string or altering your database to have last name first, this is the drawback of using files as a database. So the code you posted does not work fine for what you're asking. We're showing you how to sort the data, I'd advise you to listen.

 

Here's an adjusted code to help walk you through it.

<?php
// This is simple a sample of what you would be grabbing from the file
$lines = array(
'bId|bStatus|bFileStart|bFileModified|bFirst|bMiddle|bLast|bNick',
'cId|cStatus|cFileStart|cFileModified|cFirst|cMiddle|cLast|cNick',
'zId|zStatus|zFileStart|zFileModified|zFirst|zMiddle|aLast|zNick'
);

foreach ($lines AS $key => $item)
{
$item = explode('|', $item);
  	// formatting each line to be sorted by last name first.
$lines["$key"] = $item[6];
// add rest of the array values to the formatted line
unset($item[6]);
$lines["$key"] .= '|' . implode('|', $item);
}
sort($lines);

// now you can use your "Great" code to do what you want
foreach($lines as $thisline) {list($p_id,$p_status,$p_filestart,$p_filemodified,$p_first,$p_middle,$p_last,$p_nick) = explode('|',$thisline);{
$names[] = $p_last.', '.$p_first.' '.$p_middle.$p_id.'.';
foreach($names as $value);
{echo ''.$value.'<br>';}}}
?>

 

This does EXACTLY what you've been asking for. If it doesn't meet your expectations, try being a bit clearer.

Link to comment
Share on other sites

<?php 
//$lines = file('database/profile.txt');

$db = array( // Pinched from xyph.
	'Tom | Michael | Johnson',
	'Joe | William | Adams',
	'Doug | David | Saunders',
	'Joe | Thomas | Adams'
);

$data = array();
foreach($db as $row){
	$columns = explode('|', $row);
	$data[$columns[2].", ".$columns[0]." ".$columns[1]][] = $columns;
}

ksort($data);

// Rebuild the array.
$res = array();
foreach($data as $row){
	foreach($row as $line){
		$res[] = $line;
	}
}
?>

Person <select name="person">
<option value=""></option>
<?php
	foreach($res as $columns){
		$value = $columns[2].", ".$columns[0]." ".$columns[1];
		echo '<option value="'.$value.'">'.$value.'</option>';
	}
?>
</select>

 

You have multiple solutions to choose from - each of them doing exactly what you've asked of them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.