While I am by no means a programmer, I do get in touch with code every once in a while. Be it clumsy attempts in writing my own userscripts to customize dA, be it modding userscripts to further enhance and personalize my experience with websites like dA, be it copying lines of code off tutorial sites to enhance the experience of websites I create, be it HTML and CSS "code" when writing websites in the first place. But this is as far as it goes.
This few lines of code above I wrote while following a tutorial from 3D Buzz some time ago, trying to get into the php stuff a bit. What does it do? Simple, it's a webcounter, telling you how many people visited this page.
Info - Editor: UltraEdit-32 10.10b (with customized colours)- - Visual Style: b0se Classic Final by ~b0se.
This is a submission for and tribute to #CodeIsArt.
I was thinking about this one, and I came up with an idea, where you could use the write functions to simply append a byte to a file, and then use the file size function to find out how many bytes long it was. As long as you used the session to only rack up the counter once per session, 2 million visits would only be 2MB, and you could add in a counter-reset after, say 10MB.
I'm not personally an expert in PHP. There's a great deal of commands and tricks one can employ for ultimate compactness, but I think this code is lacking in personality. For example:
* Why define $query, when you could just put it into the function call?
* Why use a database, which can be unreliable, when a text file could suffice? Also, running two SQL queries every time the page is viewed puts unnecessary strain on the DB under heavy load.
* You could also use the session to remember a viewer and prevent them from racking up the count just by pressing refresh.
* Why kill the whole PHP page when an SQL error occurs for the page view counter? This seems over-kill when the rest of the page may work fine or not even use the database. I would place the counter in a function, and just exit the function on an error so that people could still use the site, even if the counter was not working.
* You shouldn't do "SELECT *" it's extra load on the server, always select just the fields you need.
* The second query could be built directly off of the first one, like thus:
$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++.";";
mysql_query("UPDATE `counter` SET `num_views`=$views;");
This would replace lines 6-13 with two lines, and I'm certain there's an even more compact way to do that in PHP.
Thanks very much for your time and effort to submit your code, I've enjoyed looking at it
I'll do my best to incorporate these into a possible future version, and then maybe make a direct comparison. Should be interesting. For now I can only say, I'm probably much more of a newb in PHP than you might think. But who knows, I'm always trying to learn.
Correction, that should be:
$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++;
mysql_query("UPDATE `counter` SET `num_views`=$views;");
Doesn't look like a lot of code when you glance at it, but yet it creates a web counter which is pretty nifty. PHP is just too much for me and that's why I respect anyone that can get to grips with it - much like yourself.
Ever tried using notepad++ rotane? my brother finds it extremely usefull and he's doing a degree in IT Management at the moment so should know exactly how this works more so than me
Very usefull code tho Perhaps adding on how long the page has taken to load might be a nice addon to this?
Noo, I haven't even heard about it before, but it sure looks nice -- will check it out, mate, thanks for the hint. Say, is your brother on dA? If so, he might be interested in the ~CodeIsArt project..
Unfort not and he's in his final year at uni so he's pretty busy with coding for fake websites for his course lol well actually real ones now i guess XD but ill pass on the msg for sure. and [link] link to notepad++ if you hadnt already googled it
* Why define $query, when you could just put it into the function call?
* Why use a database, which can be unreliable, when a text file could suffice? Also, running two SQL queries every time the page is viewed puts unnecessary strain on the DB under heavy load.
* You could also use the session to remember a viewer and prevent them from racking up the count just by pressing refresh.
* Why kill the whole PHP page when an SQL error occurs for the page view counter? This seems over-kill when the rest of the page may work fine or not even use the database. I would place the counter in a function, and just exit the function on an error so that people could still use the site, even if the counter was not working.
* You shouldn't do "SELECT *" it's extra load on the server, always select just the fields you need.
* The second query could be built directly off of the first one, like thus:
$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++.";";
mysql_query("UPDATE `counter` SET `num_views`=$views;");
This would replace lines 6-13 with two lines, and I'm certain there's an even more compact way to do that in PHP.
Thanks very much for your time and effort to submit your code, I've enjoyed looking at it
I'll do my best to incorporate these into a possible future version, and then maybe make a direct comparison. Should be interesting. For now I can only say, I'm probably much more of a newb in PHP than you might think. But who knows, I'm always trying to learn.
Again, thanks!
$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++;
mysql_query("UPDATE `counter` SET `num_views`=$views;");
Nice one hon
Very usefull code tho
And thanks, I'll see what I can do.
Yep, tried it already, looks promising!