ManiacDan
Staff Alumni-
Posts
2,604 -
Joined
-
Last visited
-
Days Won
10
Everything posted by ManiacDan
-
foreach only analyses array at the start?
ManiacDan replied to freelance84's topic in PHP Coding Help
Yes, the array the loop operates on is not the same array as what's available inside the loop. I wrote an article on this subject a few years ago. You're basically going to empty this array regardless of its contents. What are you trying to do with this loop? From what I can see, no matter what the initial settings of the array are, it will be empty when you're done. Seems like a lost of wasted cycles to do that task. Also, the output you claim you're getting isn't correct, there should be 3 items in the first array. -Dan -
<< and >> are bitwise operators. They perform binary operations on integers. In particular, this line takes $c, multiplies it by $j, and then multiplies that result by 8. It's slightly faster to say $something << 3; than $something * 8; It's leftover from C programmers who used it a lot. -Dan
-
Which part are you having trouble with? I described the tables and the functionality of the system as it should be implemented. There are 3 tables: 1) The user table you already have. 2) A "messages" table (you needed one anyway) 3) A table that links them together. You can remove some of the more complex bits of my plan, like the "dismissal count" and stuff like that. Keep it basic. If there's a link between the user and the message, don't show it. Otherwise, show it. When they click the X, insert the record that links the user to a message. -Dan
-
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=343985.0
-
First of all, Java and JavaScript are two entirely separate languages that are unrelated. They're not even the same language paradigm. Now, as for your problem: You have a function called addMarker() Inside that function, you use the variable icon The variable icon represents the already-colored icon for the pin. What you need to do is: - Remove the initial declaration of icon. - In your while loop, when you call addMarker(), include the color (or the filename for the color) of the marker you wish to use when you call addMarker() - Add the color as an argument to the addMarker() function - Inside of addMarker(), create a new icon object every time with the color specified. -Dan
-
so you're saying there's a wordpress FUNCTION called get_header() that uses the CODE inside header.php? The wordpress function obviously include()s the header.php file inside itself. This is bad practice (because, shockingly, it makes scope very confusing) but wordpress is full of bad practices. If you really need to include a variable from one page into the header.php there must be a way to inject it into that function in some way. Can you pass it an array or something? Failing that, use this syntax inside header.php: global $myvariable; Note that it's wrong to do that, but you're working within the restrictions of a relatively terrible piece of software.
-
You need two database tables: 1) The table that holds the message, the dates it should appear, and (maybe) the number of times it needs to be dismissed before it dies forever. 2) A table that contains the userID, the messageID, the date it was last shown, and how many times it was dismissed. When you make a message, put it in table 1. When someone views the page, find a message that's supposed to be visible that day that hasn't been dismissed by that user (or that hasn't yet been dismissed the proper number of times). Since you're displaying a message, insert/update the record in table 2 for the display date of the message. When the user dismisses the message, update table 2 (maybe through an ajax call) to reflect that they dismissed it. -Dan
-
Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include) In your second example, $myvar will not be available in get_header() because functions don't access the global scope. If you wish $myvar to be available in get_header you'll have to pass it in. (It should be noted that your second example is a PHP fatal parse error). The manual page for variable scope will tell you more. -Dan
-
Don't ever use eval() for something like this, a solution like call_user_func_array is the most correct solution. Eval() is for testing, debugging, and for when you've reached a level of skill where you know for a fact that PHP cannot do what you need it to do with the native functionality. -Dan
-
Well, make a page that selects the postings linked to the currently logged in user. Then make a page that can edit a single posting. On the portion of the code that saves the edits to a posting, make sure the posting being edited belongs to the currently logged in user. -Dan
-
You have to build an array of username=>score and then use ksort to sort by keys. -Dan
-
Have you done any debugging on your own? install firebug, chrome, or IE dev tools, and see what the console says about the error. I bet you have a concatenation problem or something else easily fixed. -Dan
-
isset() returns true or false and doesn't throw a PHP warning when you try to check a variable that doesn't exist. This gives you two benefits: 1) An absolute true/false expression to put in your conditional. 2) No warnings generated As a side comment: 3) Strings and integers can be SET but still return FALSE when checked inside an IF. So to comment on all your examples: if(isset($_POST['name'])) This is the correct way to do this. It throws no warnings and will always act exactly like you expect. if($_POST['name']) If $_POST['name'] is not set, this code will throw a warning. Also, if $_POST['name'] is SET but EMPTY, this code will return false and the contents of the IF won't be processed. if(!$_POST['name']) Again, this will throw a warning, and the execution plan is the opposite of the one above (if the string is unset or empty, the IF will execute) if($_POST['submit']=='Submit') Aside from throwing a warnings if $_POST['submit'] isn't set, this code will only succeed if the value is the word "Submit". It won't succeed if the value is anything else. This is used to make sure a specific form was submitted or to ensure that nothing has tampered with the data (though this is a poor example of that). -Dan
-
Putting a site live but keeping it private till it's ready?
ManiacDan replied to galvin's topic in Application Design
Don't tell anyone where it is and don't point a domain name to it. Also, configure apache to do authentication, then you can secure the whole webserver. You can remove that one apache directive the day you want to go live. -Dan -
Adam's name is Adam, his title is Guru ;-) The tricky stuff comes from the interpretations of "next" and "last". For instance, if I ask for "next wednesday" I get 7 days from now. If I ask for "thursday" or "next thursday" I get tomorrow. The definition of "next" for the computer is "the next occurring one." The definition of "next" in common parlance is "the one during next week." Sometimes strtotime produces results you don't expect, so I'd test this for a week's worth of days to make sure it always returns the right dates before releasing it. -Dan
-
use the example to write the code Your example says nothing about "saturday" at all, the example does not pertain to the question that was asked. OP: The strtotime solution is the best one, however, as he said, strtotime is a tricky beast. You may want to use: strtotime('+1 saturday') -Dan
-
Pulling sunset time from txt file and displaying on site daily
ManiacDan replied to master4g's topic in PHP Coding Help
mjdamato, as much as I hate to turn your posts back at you, but did you look at what he said? he doesn't want to use the sunrise and sunset functions because this is a thought experiment about displaying any data for a specific date based on date ranges stored in a table. master4g, given the table structure I gave you earlier, a query for "sunrise and sunset today" would be: SELECT Sunrise, Sunset FROM thatTableYouMade WHERE NOW() BETWEEN Start AND End; -Dan -
Pulling sunset time from txt file and displaying on site daily
ManiacDan replied to master4g's topic in PHP Coding Help
To actual answer your more direct question: This is a very basic PHP/SQL question. Create a table in MySQL with 4 columns: Start, End, Sunrise, Sunset Start and End should be of DATETIME format. When you input your data, make sure the records always butt up against each other and do not overlap: 2011-09-11 00:00:00, 2011-09-13 23:59:59, 6:45, 7:12 2011-09-14 00:00:00, 2011-09-17 23:59:59, 6:53, 7:22 Have an HTML form with a date picker or an input box (for testing, you want date formats like YYYY-MM-DD). The PHP script that queries the database should accept the inputted date, format it to YYYY-MM-DD format (if necessary), and perform the query: SELECT Sunrise, Sunset FROM thatTableYouMade WHERE '{$theInput}' BETWEEN Start AND End; The result, which should be one row, will have the sunrise and sunset time. -Dan -
Pulling sunset time from txt file and displaying on site daily
ManiacDan replied to master4g's topic in PHP Coding Help
Pikachu beat me to it, PHP does this for you already. -Dan -
The "session" is a small array that stores variables relevant to the user's current session (the time they've spent browsing your site in the last hour or so). Sometimes the session data can be stored in the database if you have a good reason for it, but otherwise just leave session handling alone. Now, once you have a session of some sort, you will then have a login system which uses that session. The current user's ID and/or username would then be a part of the session. When you perform database queries, make sure to always include the user's ID (and make sure your tables are designed so they all link to a specific user where appropriate). In this way, you can limit all queries to only affecting the items for the currently logged in user. -Dan
-
Imageshack lets me upload anything I like, then it's checked AFTER the upload completes. You'll have to scan the file's headers afterward to decide which files are allowed and which are not. Since you've posted to a PHP message board, I'm assuming you run PHP. This document (warning - PDF) explains how to secure file uploads. -Dan
-
This is far too vague to be answered properly. The "global" keyword is not to be used like that, you use it for importing variables INTO functions, not usually for exporting them out into the global scope. That being said, it's almost always the wrong decision anyway. Globals are dangerous and break the flow of your application. Anything that exists in the global scope is automatically available in the global scope (persisting through include files). Things in the global scope are not available inside of functions or classes. Any items a function needs to do its job should be passed into the function as arguments. Any items the function needs to change should be returned by the function OR passed in by-reference so they can be changed inside the function without having to be returned. In general, "global" should not appear in your codebase. It's a hack. -Dan
-
This makes no sense at all. R8kit, as Adam posted above, you're not understanding the way PHP works. PHP, as he said, is "stateless." That means that each refresh of a PHP page is a completely new execution of that page. No variables or values are retained unless you specifically save them into the session or a database. When you execute your page for the first time, your ELSE will be executed and the user_agent will be saved to the session. The next time you execute your page, the IF will be executed but $agent will be empty because this is the first time that code has run. -Dan
-
The manual section on strings contains everything you'll want to know about string concatenation. -Dan
-
Fenway's comment leads directly to a repeat of Funster's question: Why aren't you redesigning this? How much code could possibly be touching this table? Breaking this list out into its own table properly will give you a good database design that can be used for real tasks. -Dan