|
|
 |
 |
 |
 |
PHP sorting csv array output
Hello all, I have a question concerning .CSV array sorting. I have tried googling for an answer and have tried different techniques, but nothing seems to works as I would like it to work. Here is my situation: Test file is located here: http://veanndesign.com/test.php I would like to be able to sort the output by the Time Zone (or any other fields). Here is how my code looks like: http://veanndesign.com/test.html I believe that I need to get all the data from the .csv file dumped into 1 array, and I guess I am struggling at that point. I have tried using foreach inside of the while loop, but it doesn't seem to work. So what is the best and/or right way to sort this type of data? Hopefully, this email makes sense. Thanks in advance. -- Anna Vester Web Designer http://www.veanndesign.com
On May 10, 3:18 pm, ase @gmail.com ("Anna Vester") wrote:
> Hello all, > I have a question concerning .CSV array sorting. I have tried googling > for an answer and have tried different techniques, but nothing seems > to works as I would like it to work. Here is my situation: > Test file is located here:http://veanndesign.com/test.php > I would like to be able to sort the output by the Time Zone (or any > other fields). Here is how my code looks like:http://veanndesign.com/test.html > I believe that I need to get all the data from the .csv file dumped > into 1 array, and I guess I am struggling at that point. I have tried > using foreach inside of the while loop, but it doesn't seem to work. > So what is the best and/or right way to sort this type of data? > Hopefully, this email makes sense. > Thanks in advance. > -- > Anna Vester > Web Designerhttp://www.veanndesign.com
Without looking at your code (so if I'm way off base, ignore this), you're looking for array_multisort http://us.php.net/manual/en/function.array-multisort.php Specifically, example 256. You'll have to load the csv into an array, one element per row, and explode out the individual fields into their own elements within that row A little snippit I use often is: $orderbyField = 'fieldname'; //name of the array field you want to sort by $arrSort = array(); foreach ($arrData as $k => $v){ //$arrData is the array with your csv data $arrSort[$k] = $v[$orderbyField]; }
array_multisort($arrSort, SORT_ASC, $arrData); //$arrData is now sorted by $orderbyField, ascending hope that helps
-----------------------------------------------Reply-----------------------------------------------
One place to start reading, Anna, would be the PHP manual for the fgetcsv() function, which is specifically for CSV parsing. http://www.php.net/fgetcsv On 5/10/07, Anna Vester <ase@gmail.com> wrote:
> Hello all, > I have a question concerning .CSV array sorting. I have tried googling > for an answer and have tried different techniques, but nothing seems > to works as I would like it to work. Here is my situation: > Test file is located here: http://veanndesign.com/test.php > I would like to be able to sort the output by the Time Zone (or any > other fields). Here is how my code looks like: > http://veanndesign.com/test.html > I believe that I need to get all the data from the .csv file dumped > into 1 array, and I guess I am struggling at that point. I have tried > using foreach inside of the while loop, but it doesn't seem to work. > So what is the best and/or right way to sort this type of data? > Hopefully, this email makes sense. > Thanks in advance. > -- > Anna Vester > Web Designer > http://www.veanndesign.com > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php
-- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107 -----------------------------------------------Reply-----------------------------------------------
On 5/10/07, Daniel Brown <paras @gmail.com> wrote: > One place to start reading, Anna, would be the PHP manual for the > fgetcsv() function, which is specifically for CSV parsing. > http://www.php.net/fgetcsv
Thanks for your quick reply Daniel. Yes I've seen that function before and i am using it to parse the file, which displays fine. I just need to be able to sort that data (by Time Zone) prior to displaying. Sorry If I wasn't clear. Thanks. -- Anna Vester Web Designer http://www.veanndesign.com
-----------------------------------------------Reply-----------------------------------------------
When you load your data into your array, you can use the timezone (or whatever field you're using to sort) as the array key, then use ksort(). Check the "see also" for a bunch of other types of sorting you can do. When it comes to the multisorts and user defined sorts, I'm at a bit of a loss..hah.. just never had to use them so they're still a bit mystical to me. -TG http://us2.php.net/manual/en/function.ksort.php = = = Original message = = = On 5/10/07, Daniel Brown <paras@gmail.com> wrote: > One place to start reading, Anna, would be the PHP manual for the > fgetcsv() function, which is specifically for CSV parsing. > http://www.php.net/fgetcsv
Thanks for your quick reply Daniel. Yes I've seen that function before and i am using it to parse the file, which displays fine. I just need to be able to sort that data (by Time Zone) prior to displaying. Sorry If I wasn't clear. Thanks. -- Anna Vester Web Designer http://www.veanndesign.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ___________________________________________________________ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com.
-----------------------------------------------Reply-----------------------------------------------
Hello, I have a loop on an sqlite query which checks $domain until the status of $domain changes and then it deletes $domain. However when all the $domain items have been deleted I need the script to stop.. regardless of how many querys the loop still has to run. So I want to detect when its empty and then stop. I have this... which does not work. <? $db = sqlite_open("whois.sqlite"); $x = 0; while ($x < 9000) { $result = sqlite_query($db, "SELECT * FROM whois ORDER BY id"); // if (!$result) { if (empty($result)) { echo "No data"; } else {
while ($row = sqlite_fetch_array($result)) { $domain = $row['name']; echo "$domain"; $x++; usleep(400000); } } }
sqlite_close($db); ?> Thanks Chris
-----------------------------------------------Reply-----------------------------------------------
If you are going to sort it by various fields, I'd just throw it into a database... That said, http://php.net/usort should be able to do whatever you want.
On Thu, May 10, 2007 2:18 pm, Anna Vester wrote: > Hello all, > I have a question concerning .CSV array sorting. I have tried googling > for an answer and have tried different techniques, but nothing seems > to works as I would like it to work. Here is my situation: > Test file is located here: http://veanndesign.com/test.php > I would like to be able to sort the output by the Time Zone (or any > other fields). Here is how my code looks like: > http://veanndesign.com/test.html > I believe that I need to get all the data from the .csv file dumped > into 1 array, and I guess I am struggling at that point. I have tried > using foreach inside of the while loop, but it doesn't seem to work. > So what is the best and/or right way to sort this type of data? > Hopefully, this email makes sense. > Thanks in advance. > -- > Anna Vester > Web Designer > http://www.veanndesign.com > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -----------------------------------------------Reply-----------------------------------------------
On Thu, May 10, 2007 2:55 pm, c @aquanuke.com wrote: > Hello, > I have a loop on an sqlite query which checks $domain until the status > of > $domain changes and then it deletes $domain. > However when all the $domain items have been deleted I need the script > to > stop.. regardless of how many querys the loop > still has to run. So I want to detect when its empty and then stop. > I have this... which does not work. > <? > $db = sqlite_open("whois.sqlite"); > $x = 0; > while ($x < 9000) { > $result = sqlite_query($db, "SELECT * FROM whois ORDER BY id"); > // if (!$result) { > if (empty($result)) { > echo "No data";
I dunno for sure what empty() is gonna do with $result, but I suspect that this message should say "query failed" rather than "no data" No data returned is not an error, it's just an empty set, which is very common. > } else { > while ($row = sqlite_fetch_array($result)) { > $domain = $row['name']; > echo "$domain";
The quotes is kinda silly here... > $x++; > usleep(400000); > } > } > } > sqlite_close($db); > ?>
You said you were deleting things...? Where? What are you actually trying to do? "delete from whois" springs to mind as a MUCH better way from what you described, but not what you coded... -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?
-----------------------------------------------Reply-----------------------------------------------
Yep, that would be the perfect solution, but, unfortunately, database is not an option for this project. Thanks for looking! I did get a solution though from another list. Here is a working version: http://veanndesign.com/sorting.php compare it to the not working one: http://veanndesign.com/test.php Anyways, thanks again! Anna On 5/13/07, Richard Lynch <c@l-i-e.com> wrote:
> If you are going to sort it by various fields, I'd just throw it into > a database... > That said, http://php.net/usort should be able to do whatever you want. > On Thu, May 10, 2007 2:18 pm, Anna Vester wrote: > > Hello all, > > I have a question concerning .CSV array sorting. I have tried googling > > for an answer and have tried different techniques, but nothing seems > > to works as I would like it to work. Here is my situation: > > Test file is located here: http://veanndesign.com/test.php > > I would like to be able to sort the output by the Time Zone (or any > > other fields). Here is how my code looks like: > > http://veanndesign.com/test.html > > I believe that I need to get all the data from the .csv file dumped > > into 1 array, and I guess I am struggling at that point. I have tried > > using foreach inside of the while loop, but it doesn't seem to work. > > So what is the best and/or right way to sort this type of data? > > Hopefully, this email makes sense. > > Thanks in advance. > > -- > > Anna Vester > > Web Designer > > http://www.veanndesign.com > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > -- > Some people have a "gift" link here. > Know what I want? > I want you to buy a CD from some indie artist. > http://cdbaby.com/browse/from/lynch > Yeah, I get a buck. So?
-- Anna Vester Web Designer http://www.veanndesign.com
|
 |
 |
 |
 |
|