Psycho
Moderators-
Posts
12,159 -
Joined
-
Last visited
-
Days Won
130
Everything posted by Psycho
-
What you are trying to do makes no sense. There is no reason to pass the entire array as the checkbox value. One of the reasons for using a database is that you can use references to the primary key for the database records. Set the value of the checkboxes to the ID of the records not the entirety of the records themselves. Also, you should never be running queries in loops as you show above.
-
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
Cynical? I don't think so. Condescending? Perhaps. Critical? Definitely. But, the only statement I made that was even remotely condescending was But, I consider that humor, sorry if you took that personal. I said absolutely nothing about you or your character - only your code. I was in the Army too. But, I don't see your point. -
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
Not upset at all, as I said you are the one that started with the disparaging remarks not me. I may have been criticizing your code, you are the one that decided to make it personal. Yes, exactly. The OPs first request was vague and the reference you stated was misleading. That is why I asked for the OP to provide a clearer definition of the issue - which he did. In the specific example he states he wants the result of 1 week and 2 week days (i.e. he doesn't need a weekend in the results because it is included in the full week). Based upon those refined requirements it is obvious he doesn't need ALL the weekends if they are included in the full weeks (7 consecutive days). If you are charging the customer for full weeks, you wouldn't also charge separately for the weekends included in those full weeks. That being the case, you would only ever have 1 or 2 weekend days that are not included in a full week for any time period. -
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
No my feelings aren't hurt. And you are the first one to make disparaging remarks. The reason your code wasn't calculating right has nothing to do with how close together the dates are. I intentionally didn't state why your code failed to see if you realized the error. In the last code you posted I see you corrected that error using the logic I had originally provided. So, either you "know" why the error occurred and solved it with the same solution or you still have no idea why the error occurred and just solved it by copying and pasting something with no understanding of why it was a problem in the first place. Since you didn't state the reason for the error (which has a simple explanation) I assume the latter. No, my code returned 2 weekend "days" and 1 full week. As per the more detailed requirements in the follow up post from the OP that I requested, this is what he needed, i.e. full weeks and any remaining weekdays/weekends. But, since he admitted that he didn't know what to do if there was only one weekend day (and not a full weekend) the code returned the number of "remaining" weekend days. He can then determine what to do if the value is a 1 (one remaining weekend day) or a 2 (a full weekend). There would only ever be 1 or 2 remaining weekend days. But, it could just have easily been modified to return a 1 for a full weekend of a .5 for a single weekend day. No, I am not offended. I just don't understand your need to "fix" something that is not broken and replace it with something that is flawed when you don't understand the requirements or, more importantly, the dynamics of the intricacies involved (dates and time). -
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
What are you talking about. He stated he wanted full weeks (7 consecutive days) regardless of when that week started. Besides there are several problems with your code. What is this line for $totalWeeks = $totalDays / 7; ' when you never even use that variable before redefining that variable on this line? $totalWeeks = floor($totalDays / 7); // (5.57) 5 That's a minor problem. A bigger issue is that your code is just plain broken. Using the dates Start: March 11, 2011 and End: March 20, 2011 the results are: Array ( [TotalDays] => 9.95833333333 [Range] => 11Mar2011 to 20Mar2011 [TotalWeeks] => 0 [weekends] => 1.47916666667 [weedays] => 6 ) So, what is the rate on 1.47916666667 weekends? Also, the OP stated he needed to do this "...between any 2 dates stored as unix timestamp.". Which your function does not support - requiring the dates to be converted before calling the function. -
[PHP] Problem with function move_uploaded_file()
Psycho replied to GarryOne's topic in PHP Coding Help
Since when is PHP case sensitive? First of all, I assumed the OP changed that line to upper-case to "highlight" it as he stated in his description. Second, PHP is not case sensitive - at least I can run code in alternative cases on my machine with no problems. -
One addition to cssfreakie's explanation. The mysql_real_escape_string(), as it's name implies, is for string values. That is not always the appropriate method based upon the type of values you need to escape. Looking at your code above, it looks like the only potentially user submitted value is $id. I would "assume" that the $id value is expected to be in integer. So, you should use a validation that ensures it is such. You could use is_int() and reject the input if that returns false or, alternatively, you could use "(int) $id" to force the value to be an integer.
-
The error message is an HTML error NOT a PHP error (moved topic to appropriate thread). You need to check line 16 of the HTML source that is generated from the PHP code. Of course, if you are simply dumping your PHP code into a validator or pointing the validator to your page which is hosted on a server that is not parsing the PHP code, then that would explain your problem as well. An HTML validator does not expect PHP code.
-
This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=335591.0
-
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
Here you go. Just pass the function two dates. It will return an array with values for the following: "total weeks", "remaining weekdays" and "remaining weekend days". You can simply divide the weekend days by two to get 2-day weekends. function getDateParts($startDate, $endDate) { //Normalize dates for 12noon to ensure full days calulation $startDate = strtotime(date("d F Y", $startDate) . " 12:00pm"); $endDate = strtotime(date("d F Y", $endDate) . " 12:00pm"); //Initialize array for return variable $return['totalWeeks'] = 0; $return['singeWeekendDays'] = 0; $return['singleWeekdays'] = 0; //Calculate total days, weeks, and remaining days $totalDays = round(($endDate-$startDate) / (60*60*24)) + 1; $return['totalWeeks'] = floor($totalDays/7); $remianingDays = $totalDays - ($return['totalWeeks']*7); //Determine types of remaining days while($remianingDays>0) { if(date("N", $endDate)>5) { $return['singeWeekendDays']++; } else { $return['singleWeekdays']++; } //Change end date to prev date, and reduce remaining days $endDate = strtotime(date("d F Y", $endDate) . " -1 day"); $remianingDays--; } return $return; } Using dates of June 1 and June 12 of this year the result is Array ( [totalWeeks] => 1 [singleWeekdays] => 3 [singeWeekendDays] => 2 ) -
if you check out my proposed code in the console, it works perfectly, I was just noting that the pseudocode proposed by wikipedia doesn't match to yours exactly, and simply asking what the difference is lol I'm not following your first statement. But, your proposed code is wrong. Sure, it works for 2012, but that doesn't mean it is is valid. It fails to correctly identify the year 2000 as a leap year (as it will for 2400, 2800, etc.) As for the pseudo code on the Wiki, it is valid. As is my alternative approach
-
Preserving double quotes in an uploaded text file
Psycho replied to Marchand's topic in PHP Coding Help
Not to my knowledge. But, even if it did there is absolutely no way that the move_uploaded_file() function would know how to convert AM/PM time to 24hour time. That's just ridiculous. In any event you can test it yourself quite simply. Just do what btherl stated and create a test page that does nothing but upload a file without all the other code you have. -
calculate weeks, weekends and days between any 2 dates?
Psycho replied to rich_traff's topic in PHP Coding Help
Can you provide an example of how you expect the results? The problem I see is that a "Full" week includes a weekend. So, if the start date was a Sat. and run through 16 days ending on a Sun. You have two full weeks, but three sets of weekends. Just not sure how you want that returned - or what you consider a "full week". Is it mon-sun, sun-sat (a true week) or any 7 consecutive days. -
Storing data in an array? Is this the best way?
Psycho replied to slaterino's topic in PHP Coding Help
I'm not going to analyze all of your code and try to determine the best approach - there's just too much. But, it looks like you are simply running a query to get some data that you then want to output later. Typically if I only need to process the data once I will generate the output when I do the while() loop on the database records. I will populate those results into a string variable. Then, I will use that variable to put the output into the appropriate location on the page. I don not put the results into an array - that only increases the memory needed to process the script. I will only put the results into an array if there are some array operations I need to perform on the result set - for example, if I need to split the dataset based upon a know list of IDs. Here is a rough example based on your explanation <?php $query = "SELECT eventStart, eventEnd FROM table"; $result = mysql_query($query); //Generate output $htmlOutput = ''; while($row = mysql_fetch_assoc($result)) { $sameDate = ($row['eventStart'] == $row['eventEnd']) ? 'True' : 'False'; $htmlOutput .= "<tr>\n"; $htmlOutput .= "<td>{$row['eventStart']}</td>\n"; $htmlOutput .= "<td>{$row['eventEnd']}</td>\n"; $htmlOutput .= "<td>{$sameDate}</td>\n"; $htmlOutput .= "</tr>\n"; } //more php processing code // . . . // . . . //PHP processing is complete, show HTML ?> <html> <body> Here are the results: <br> <table> <tr> <th>Start Date</th> <th>End Date</th> <th>Same Date?</th> </tr> <?php echo htmlOutput; ?> </table> </body> </html> -
Preserving double quotes in an uploaded text file
Psycho replied to Marchand's topic in PHP Coding Help
This is within a class. You are calling several other functions against the file ($this->checkSize,$this->checkType, $this->checkName). Based upon the names, I doubt those functions should be modifying the file. Plus, since you are passing a reference to the file to this function there is no grantee that the file is not modified before the function is called or afterward. Basically, there is nothing in that code - specifically - that would modify the file. It would either be taking place before the function is called, within one of the function that are called in that function, or after the function is called. -
Well, look at the results for scenario A. Your query is looking for users with username 'ko' and an ID that is NOT 0. And, it found one (ID = 428). Since you were not editing the username that tells me that the ID of the user you were trying to modify is actually the one with ID 428 - not 0. That also explains the error you received in scenario B. You are not passing the correct ID to the function. Look at where you are calling the function and validate the ID.
-
Is the error regarding "User name already exists" correct? I would think so. Based on the query you were trying to modify the record with an ID of 0 and either the name of that record was already 'ko' or you were changing it to 'ko'. In either case there appear to be other records in the database with that username. The mysql_fetch_assoc() error was a typo in the debugging code and has nothing to do with the logic of the code. To, fix that error use this while($row = mysql_fetch_assoc($result))
-
There was nothing wrong with my original solution. But, oh well, your proposed logic would work as well, except you implemented it wrong. You parenthesis are not correct. The above code says if (year is divisible by 400 OR 4) AND NOT divisible by 100) then it is a leap year. The AND clause negates the 400 condition (a year cannot be divisible by 400 and not be divisible by 100). You want something like this: IF ( (Year div by 400) OR ( (Year div by 4) AND (Year NOT Div by 100) ) = leap year ((!(yearVal%400) || (!(yearVal%4) && (yearVal%100))) ? 29 : 28); The logic I used was: If ( (Year div by 4) AND ( (Year NOT Div by 100) OR (Year Div by 400) ) = Leap year Which, if you compare my original code with the code I just corrected for your logic, are both exactly the same number of characters. I just think my solution is easier to read. Besides, using something like !(yearVal%4) to test if the year is divisible by 4 seems counter intuitive since you are using a NOT to return a true condition.
-
Too bad your shortened code produces incorrect results. Using your formula the year 2100 would be a leap year, but it is not. Basically your code will be wrong once every century (on the "100" year marks) 3 out every 4 centuries: 1700, 1800, 1900, (correct on 2000) 2100, 2200, 2300, etc.
-
If a message has been "sent" it would not make sense to update the email addresses used when sending the email after the fact. But, you could reference the current email address for the associated instructor. You don't need to update anything in the messages table. using the table structure I proposed above you could get the message with ID 1 and the current name/email of the instructor as follow SELECT message, name, email FROM messages JOIN instructors USING (instructorID) WHERE id = 1
-
Okay, that doesn't explain why you would want separate tables. In fact, the explanation you made above only reaffirms the fact that you shouldn't be using separate tables for the messages. As I stated you would want one table to store information about the instructors and one table to store the messages. You use a foreign key in the messages table to identify which instructor the messages belong to. Example table structures Table: instructors Table: instructors instructorID | Name | email 1 Bob Smith [email protected] 2 Jane Doe [email protected] 3 Rick Donner [email protected] 4 Bart Johnson [email protected] 5 Davey Jones [email protected] Table: messages ID | instructorID | Message 1 2 Message by Jane Doe 2 3 Message by Rick Donner 3 2 Message by Jane Doe 4 1 Message by Bob Smith 5 5 Message by Davey Jones 6 3 Message by Rick Donner 7 4 Message by Bart Johnson
-
This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=335411.0
-
No offense, but you are doing it wrong. You should have ONE table for all messages regardless of instructors. Then create your query based upon which instructor you want data for. You would jsut create an additional column to identify the instructor and it would probably make sense for that column to be a foreign key to an "instructor" table where you store the details about each instructor.
-
The brackets around the "1" is definitely wrong. But, there are other problems. 1. The path is not enclosed in quotes. It shouldn't be a problem here since the names don't have any spaces, but it is invalid code nonetheless. 2. The border parameter has escaped single quotes. But, since these are created in a double quoted string they wouldn't be escaped and the backslashes would be in the code. That may be causing the problem. You should first create a static image tag to one of the images and ensure the full path works. Then you can make it dynamic. This would be a more appropriate code for the above $images = 10; $path = "afp://path/path/path/path/"; $random = rand(1, $images); echo = "<img src=\"{$path}{$random}.png\" border=\"0\" />"; However, are the only .png images in that folder the ones that you want to display randomly? If so, you can clean up that logic and not even have to name your images 0, 1, 2, etc. $path = "afp://path/path/path/path/"; $images = glob("$path*.png"); //Get array of all PNG images $randImage = $images[array_rand($images)]; //Get random image echo = "<img src=\"{$randImage}\" border=\"0\" />"; What does that mean exactly. You need to be writing a PHP file and be requesting the file through a web-server (which you could be running on your PC). You can't just open the file in your web browser. IE, FF, etc. don't have any clue on how to execute PHP code. That takes place on the web server.
-
You only have ONE session, but you are maintaining two session values. I think the problem is that you have two different forms and based upon which one is submitted you are setting the value for one variable and the other is getting set to the default. You should check to see if the session value was previously set before setting it to the default. For each one I would 1. First check if something was posted (if so use the posted value) 2. Then I would check if the session value was previously set (if not then set to the default) 3. If the session value was already set, then I would do nothing and just use that value if(isset($_POST['sezonaSubmit'])) { $_SESSION['sezona'] = $_POST['sezona']; } elseif(!isset($_SESSION['sezona'])) { $_SESSION['sezona'] = "Letnja Guma"; } } $sezona = $_SESSION['sezona']; if(isset($_POST['poredak'])) { $_SESSION['poredak'] = $_POST['red']; } elseif(!isset($_SESSION['poredak'])) { $_SESSION['poredak'] = " ORDER BY price ASC"; } $order = $_SESSION['poredak'];