bentwiener Posted September 10, 2011 Share Posted September 10, 2011 it is displaying the lastnames in the order they are in the profile but do not know 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>';}}} ?> Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/ Share on other sites More sharing options...
redarrow Posted September 10, 2011 Share Posted September 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267634 Share on other sites More sharing options...
bentwiener Posted September 10, 2011 Author Share Posted September 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267812 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 can you post the code that uses sort(). which variable are you sorting? you should be sorting $names Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267817 Share on other sites More sharing options...
bentwiener Posted September 10, 2011 Author Share Posted September 10, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267821 Share on other sites More sharing options...
bentwiener Posted September 10, 2011 Author Share Posted September 10, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267823 Share on other sites More sharing options...
MikeDean89 Posted September 10, 2011 Share Posted September 10, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267854 Share on other sites More sharing options...
bentwiener Posted September 11, 2011 Author Share Posted September 11, 2011 the code above just explodes the entire database Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267925 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 "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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267939 Share on other sites More sharing options...
MikeDean89 Posted September 11, 2011 Share Posted September 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267949 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1267961 Share on other sites More sharing options...
bentwiener Posted September 11, 2011 Author Share Posted September 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268090 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268095 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268096 Share on other sites More sharing options...
MikeDean89 Posted September 11, 2011 Share Posted September 11, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268102 Share on other sites More sharing options...
bentwiener Posted September 12, 2011 Author Share Posted September 12, 2011 to everybody that helped me. a great big thank you , i was so focused on it and not listening, i could not see what was in front of my face or what was being said to me. Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268174 Share on other sites More sharing options...
meltingpoint Posted September 12, 2011 Share Posted September 12, 2011 bentweiner- All very good suggestions. I kinda migrate towards this one myself; http://www.designdetector.com/tips/flat-file-database-demo2.php Chris Hester put together a series of 5 demo/tutorials on flat files that are really good. using array_unshift() for sorting made my life much easier. Cheers- Quote Link to comment https://forums.phpfreaks.com/topic/246836-sorting-by-lastname-from-flat-file-database/#findComment-1268182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.