Click to See Complete Forum and Search --> : [Resolved] Sort By Date Brute Force Stylee
cyberlew15
03-02-2005, 10:26 AM
Don't hate me cause I'm messy Hate me cause this peice of code is absoluteley laughable I know there must be many ways to improve it but I just can't see the wood through the trees sometimes.
<?php
$monthsentries = file_get_contents("./entries/march.txt");
$eachentry = explode ("\r\n", $monthsentries);
foreach ($eachentry as $entry){
$entrydata = explode("|", $entry);
$date = $entrydata[2];
$sortdate = explode("-", $entrydata[2]);
if ($sortdate[0] == "01"){
$firstdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "02"){
$seconddata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "03"){
$thirddata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "04"){
$fourthdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "05"){
$fifthdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "06"){
$sixthdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "07"){
$seventhdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "08"){
$eighthdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "09"){
$ninethdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
if ($sortdate[0] == "10"){
$tenthdata .= "<hr><br><b>".$entrydata[0]."</b><br>".$entrydata[1]."<br>".$entrydata[2]."<br><br>";
}
// I think you know where this is going
}
// sort Ascending
echo $firstdata;
echo $seconddata;
echo $thirddata;
echo $fourthdata;
echo $fifthdata;
echo $sixthdata;
echo $seventhdata;
echo $eighthdata;
echo $ninethdata;
echo $tenthdata;
// Once again you knew where that was going
?>
Just to clarify this is not a joke although I wish it was:D :D
ShawnK
03-02-2005, 07:27 PM
Hey, can you post the text file so I can see how it's structured. So, I can help :)
cyberlew15
03-02-2005, 07:42 PM
Sure the text file is structured like this
put in directory calledd entries and name march.txt
entry1|entry2|01-03-05
entry1|entry2|01-03-05
entry1|entry2|07-03-05
entry1|entry2|01-03-05
entry1|entry2|01-03-05
entry1|entry2|10-03-05
entry1|entry2|01-03-05
simple eh but it seems to work now I just need to optimise the code. I'm workin on it also but I recognise that even my neat code may seem sloppy to you guys.:evilgrin:
Shrike
03-03-2005, 07:03 AM
Why not use PHP's (extensive) sorting functions?
<?php
$monthsentries = file_get_contents("entries.txt");
$eachentry = explode ("\r\n", $monthsentries);
$i = 0;
foreach ($eachentry as $entry)
{
$temp_arr = explode("|", $entry);
$sortable_arr[$i]['entry1'] = $temp_arr[0];
$sortable_arr[$i]['entry2'] = $temp_arr[1];
$sortable_arr[$i]['date'] = $temp_arr[2];
$i++;
}
uksort($sortable_arr, "mySort");
print_r($sortable_arr);
function mySort($a, $b)
{
// Reverse sorting method
// Have to make this global to find the data to sort on
global $sortable_arr;
$a = strtotime($sortable_arr[$a]['date']);
$b = strtotime($sortable_arr[$b]['date']);
if($a == $b) return 0;
if($a > $b) return -1;
if($a < $b) return 1;
}
?>
cyberlew15
03-03-2005, 03:36 PM
This is the method I figured out last night which I think is quite concise. Thanks for the input guys and this works on a P90 with 16MB RAM (Old laptop I had lying around) How Kool Is That
<?php
if (isset($_GET['month'])){
$monthsentries = file_get_contents("./entries/".$_GET['month'].date("y").".dat");
}
if (!isset($_GET['month'])){
$monthsentries = file_get_contents("./entries/".date("Fy").".dat");
}
$searchentry = explode("\r\n", $monthsentries);
foreach ($searchentry as $entry){
$getentrydata = explode("|", $entry);
$entrydom = $getentrydata[7];
${"entry".$entrydom /1} .= $entry."<br><br>";
}
for ($i=0;$i<31;$i++){
echo ${"entry".$i};
}
?>
and to change the sort from ascending to descending replace the for statement with
for($i=31;$i>0;$i--){
echo ${"entry".$i};
}
So whatdayya think of the new code is it more optimised, worse(lol) or any other comments are welcome.:evilgrin:
bubblenut
03-04-2005, 03:42 PM
${"entry".$entrydom /1} .= $entry."<br><br>";
That's not very clean. You're dividing a string (something like '12-01-05') by one to convert it to a number. Take a look at strtotime().
Also, think about using an array rather than variable variables, that way you'll be able to have a slightly more dynamic loop at the end by using count().
This all said, Shrike said it in the first reply, PHP already has functions to handle this kind of thing. Why not use them?
Weedpacket
03-04-2005, 05:12 PM
Originally posted by bubblenut
This all said, Shrike said it in the first reply, PHP already has functions to handle this kind of thing. Why not use them? A more general principle is: If you find yourself doing the same thing over and over again, ask yourself why - since these days there are machines to do that sort of thing.
cyberlew15
03-04-2005, 07:01 PM
about the string I'm dividing it's no longer a full date just the day within the month. This is because the file is not named the month and the year so why include the information for each entry and the reason I must divide by 1 is because php date seems to output 01 instead of 1 so to clarify the entrydom is now formatted 01 instead of 01-02-05 does this still make a huge problem. the code does run quite smoothly on a 166 with only 32mb of memory how bad could it be? I know i've asked for you guys help and you don't need to answer this if you don't want to I'm just curious about the advantages of changing from the way i've done it. Also I would very much like to know if all of the built in php functions are all that clean sometimes when running through php3 I can use longer code to execute a function that is built into php5 and it is faster. Any explanations dudes
Weedpacket
03-05-2005, 12:01 AM
Originally posted by cyberlew15
I'm just curious about the advantages of changing from the way i've done it.Faster? By how much? Is the amount of time saved more than the time you spent writing the longer version?
cyberlew15
03-05-2005, 08:01 AM
Look I'm very grateful for your input it's great all I was saying is that my new version is such small code, smaller than any of the replies.
BUT I would very much like to know why the other new post of working code is faster. or more optimised. I'm looking at this from a learning perspective and am curious why php built in functions nesecarily means that it will be more efficient.
My method of thinking is that for dealing with large amounts of data arrays are always more efficient than strings however there will never be more than 31 days in the year or at least such a change would be outside of my lifetime so variables are more efficient for smaller amounts of data generally speaking. All I would like to know is why is that code more efficient than the rewrite I did and if it is more optimised by which I mean runs with the lowest requirements out of the three posted scripts. I am interested in this because the less resources the script takes up the better it will be does this make sense guys I hope you do not take these questions offensively as that is not the nature of them I'd just like to learn why:p
bubblenut
03-05-2005, 10:04 AM
The main thing I feel uncomfortable about with your implementation is that you're relying on the odities of PHP's type handling. You're taking a string, dividing it by one and PHP just happens to return the first part of that string. It could just as easily return 1, 0, false, true or fail with a fatal error and it may well do in future releases. How PHP handles sloppy type juggling is very deffinately not set in stone. Also, I don't know why you think variables are going to be more efficient for storing small amounts of data. For small amounts of data there isn't going to be a difference. Now, let's go onto the subject you seem to be most concerned about with this code. Optimization.
There are a number of things you could change to make it quicker. Firstly, file_get_contents reads the entire contents of the file into memory and then you handle it as a whole. This means that a. you're iterating the file contents twice, once to read from the file and write to memory and once to read from memory and process b. you're using considerably more memory because you have to load the entire file into memory rather than just a bit at a time. So, first step would be to use fopen and it's related functions. Now, the second is your dodgy way of handling the date part of each line in the file. To get the day part of the date with the least overhead use substr. Then rather than creating variable variables I'd use arrays something like this (also, notice I'm putting them into an array rather than catenating them as a string. You will see why this is important later)
$entry[$day][] = $entry;
If you use an array method like this you have much more flexibility over how you can manage/maintan your code and data in the future. Think, for example, what would you do if you wanted to change how the page was displayed later down the line? You'd have to dive right into the logic of the program just to change the display. Also, think what happens if you later decide you want to order each day's entries alphabetically by one of the other fields, you'd have to rip everything up and start from scratch.
What we're trying to do in this forum is not just teach you how to fix the problem at hand (we've got the General and Coding forums for that) but also give you some insight into the principles of design. Help you to think about other considerations which need to be taken into account. An important design principle which describes the reason for what I've gone into in the previous paragraph is "Seperate what can change independently". What this principle means is that if we have two parts of a program which are likely to change independently of each other we should try to keep them as seperate from each other as possible. The most common application of this principle is the seperation of logic and display. In placing the results into an array this is exactly what we're doing.
cyberlew15
03-05-2005, 10:18 AM
Thank you very much your argument is very understandable and I will get to work on this right away You have answered my question exactly how I would like thank you all very much for your help with this thread. If I can ever help you guys out someday I will thanks again to everyone elsee that has responded in this thread.:cool:
Shrike
03-05-2005, 04:41 PM
Fewer lines do not mean faster code. I could reduce my code down to fewer lines than yours but that would result in slower code. But...thread is resolved so I won't go any further.
Weedpacket
03-05-2005, 10:00 PM
Besides; I was serious about development time.
Ah, here's that quote I was looking for:
Modularity (http://www.catb.org/~esr/writings/taoup/html/modularitychapter.html)
Dennis Ritchie encouraged modularity by telling all and sundry that function calls were really, really cheap in C. Everybody started writing small functions and modularizing. Years later we found out that function calls were still expensive on the PDP-11, and VAX code was often spending 50% of its time in the CALLS instruction. Dennis had lied to us! But it was too late; we were all hooked...
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.