Echo the following:
date(M m, time());
If that doesn't display 'August 08' then your server clock is broken...:S
As to Madison...that looks rather interesting, high chance it's faster then PHP. Never thought of that ^^, will look into it.
Printable View
Echo the following:
date(M m, time());
If that doesn't display 'August 08' then your server clock is broken...:S
As to Madison...that looks rather interesting, high chance it's faster then PHP. Never thought of that ^^, will look into it.
Echo the following:
date(M m, time());
If that doesn't display 'August 08' then your server clock is broken...:S
As to Madison...that looks rather interesting, high chance it's faster then PHP. Never thought of that ^^, will look into it.
MySQL databases can perfectly handle loops, they should just be all in one query. If you do a for loop of 200 items (for $i = 0; $i < 200; $i++) { [query] } you will hang your database.
That being said, I'm appaled at your code. It's not ugly because it's basic, it's ugly because it uses no abstraction, no indentation, no descriptive variable names, mixes display logic with data logic and you rely upon PHP's string parser to build your dynamic texts. If you plan to still write PHP code in 10 years I strongly advice you to read an article I wrote a while ago here on maintainable PHP code.
Then, to actually go to the usefull part, a much more elegant way to count active users is to count active sessions. To do that you can either loop trough the session_safe_path directory or write a custom session handler that uses a database - it's fairly easy. I've written an article on how to do that here. It includes a link to my own session handler that has a public member function to show the amount of logged in users, though it is all written for PHP 5.
Last but not least: date/time conversions are faster in SQL than in PHP, just for your information :wink:
I read your first article, FragFrog. Thanks for the feedback. One thing I noticed is my varriable for last_edited. It should be called last_action instead, to better relate to the SQL database, you're absolutly right. I think that's what you noticed. I'm going to have to change that for the future.
I'm still fairly new to PHP, as I'm sure you all can tell, haha; -but what exactly do you mean by: "mixes display logic with output logic and you rely upon PHP's string parser to build your dynamic texts." That could be my biggest problem, but I don't see the problem there.. Do you mean that I manipulated my timestamp too much and relied on that to build the date display?
I always want to improve. With that said, I'd like to know a couple downfalls to this if it's not too much trouble.
To me, all of the varriable names, with the exception of $last_edited being last_action and $zero being null, make perfect sense for their placements. The indent is bad.. -I mostly just used spaces because I wrote it in 10 minutses-.. It's simple, not very abstract or complex/creative, but it gets the job done. It kinda hurts to hear that you're all appalled by it.. lol
I was actually proud of myself for splitting up the timestamp like a roasted ham, but I can sort of see why I shouldn't rely on it.. Then again.. I did do what I needed to do, and there's no question it works logicly.. (might as well say 1+1=2 here) I never thought I'd get so much conversation over such a simple little thing, but I guess I haven't seen anywhere near everything yet.
As for indents, I don't take as much time as I should formatting my code like I used to when I first started. I should improve there.
It's 4:41 am, here. I value all the time you all spent here helping me. I promise someday I'll become a better coder. Untill then, I'll just keep helping with what I know, and keep practicing, reading tuts, inventing, reading PHP.net docs and user input etc. etc. like I've been doing.
I think I need a good kick in the ass every once in a while to keep me in line with my programming.. Thanks Frag
Most specifically I meant blocks like these:
For one, this could have been written a lot shorter:PHP Code:while($getdata3=mysql_fetch_array($getdata2)) {
$getdata3[userID]=strip_tags($getdata3[userID]);
$getdata3[last_action]=strip_tags($getdata3[last_action]);
$getdata3[logged_in]=strip_tags($getdata3[logged_in]);
Notice the indents and whitespace there, making it a lot clearer what block sits where. However, it doesn't explain to me what "getdata3" is. Or what "getdata2" is. Or "getdata1", "getdata34" or whatever getdata you are using. Instead use descriptive variables:PHP Code:while ($getdata3 = mysql_fetch_array($getdata2)) {
foreach ($getdata3 as & $field)
$field = strip_tags($field);
}
Now an outsider can see what each variable is and does - note that I've slightly exagerated this, a variable called $userQueryResource can, if the context is small enough, also be called $queryResource for instance. I advice against abbreviating types and names, though within limit's they can't hurt. As I wrote in that article, it takes you only half a second to write out a name, it will take someone else reading your code a lot longer to figure out what "$qrRsltRsrc54" refers to :smile:PHP Code:$userSelectQuery = "SELECT *
FROM `users`";
$userQueryResource = mysql_query($userSelectQuery) or die(mysql_error());
while ($userData = mysql_fetch_array($userQueryResource))
foreach ($userData as & $field)
$field = strip_tags($field);
As for mixing presentation and business logic, this I talked about here. In short, in a good coding model you will want the part that loads the info from the database, modifies it etc to be seperate from the HTML code your users will eventually see. This filosophy is much better described in the two most commonly used (web)development architectures, the MVC model and the Multitier architecture. The most general way to achieve this is the use of a template engine, which can be something small you wrote or an extensive engine like Smarty.
To give you an idea: my PHP files contain 0,0 HTML code. Most texts the user sees are stored in a database. For one of our latest sites we had to implement a translation future where every single field could be displayed in any of 8 given languages - and they had to be translateable by an outside company. This resulted in a system where each and every piece of text was stored as a "section" and "key" combination (for example, "userLogin", "submitButton") with translations stored in a different table which could be added, updated or removed without trouble.
With your code, that would mean making 8 PHP files for each page, having to manually edit the files for each translation and if you'd make a change to one page you'd have to update 7 other files with the exact same change. I hope it's clear that would be an unworkeable situation :smile:
An additional adventage is that you can simply modify the looks of your site without having to change your PHP code. Say you want a block moved from the header to the footer, in your case you'd have to strip out the PHP code, retrieve the HTML, move it downwards, verify all your variables are still good (ie, not overwritten later on), echo the HTML again and fix any PHP code that got broken. With a template engine you can savely move it anywhere because all the PHP code will be done already.
Well currently I'm creating includes to put in place of certain PHP code. Things such as connections, comments, messages and I already setup the friendlist like that.
Since I have to do all this anyway, I'm most def going to rename all my getdata123's to names that correspond to their actions. The idea of calling things userQuery123 and friendQuery123 instead of getdata swam through my mind. But I can just as easily say userDate, userQuery, userRow instead. I don't know why I use getdata.. I've used it since I first learned how to make a connection, and stuck with it. Obviously the guy just used getdata as an example of how to do a connection, but for some reason I never did rename any of my data fetchers untill I ran into problems repeating varriables. Then after that I named the second one getdata4,5,6.. Ha.. stupid me.
After I made that mistake, I should've just gave them all definitive names. I do actually write out getdata, I don't copy and paste usually.. well not always. So I really don't know why I didn't just say userData, userQuery, userRow... But I will from now on.. (for users, that is.)
Of corse friends would be friendData, friendQuery, friendRow etc.
I really should've saw that coming.. ha
My sig should say: There's a fly on my brain! - lol
Also, and I think FragFrog meant that as well, try not to use $variables inside strings, but rather use the append function. This makes it much more clear that you're inserting a variable, and requires less parsing for the interpreter. I always use single quote above double quotes, since single quotes cannot contain $variables, and thus would be marginally faster.
I would also suggest that you use less variables where it makes no sense.
Could as well be written on one line (and is just as clear, IMO):PHP Code:$getdata="SELECT * FROM `users` ";
$getdata2=mysql_query($getdata) or die(mysql_error());
Notice I use mysql_errno() rather then mysql_error(). The latter can display some valuable information for possible hackers, while the former simply indicates what kind of an error occured. You can then check your server logs for the actual error (or create a log yourself).PHP Code:$result = mysql_query("SELECT * FROM `users`") or die(mysql_errno());
Now lets take a look at this part:
It could be written much smaller and still have the same functionality:PHP Code:$last_action=$getdata3['last_action'];
$last_action=strtotime($last_action);
$Onehour = 3600 ;
$expired = $last_action + $Onehour;
$time=time();
$elapsedTime=$time-$last_action;
That is only half the number of rows, and a few variables less :)PHP Code:$last_action = strtotime($getdata3['last_action']); // must get rid of this line
$expired = $last_action + 3600; // time at which one is considered being logged off
$elapsedTime = time() - $last_action; // the last time an action occured
Oh, and I forgot to mention that I did update my stuff, and everything seems to be working like a charm.. Except one problem with the guest getting old data that doesn't even exist! Ugh.. see my new post. :) lol..
And I changed most of my sloppy code at least to make it shorter.
I changed everything I found that says $getdata to something more like $userQuery or $profileQuery...
I also changed some things like
toPHP Code:$row['var']=strip_tags($row['var']);
$var=$row['var']
Most things are running just as smooth. And now it's easier to update certain things because I made include files for data, post_form data, create_profile data.. etc.PHP Code:$var=strip_tags($row['var']);