So... I'm trying to make the counter on my site graphical.... I wrote this string of code up, but was wondering if anyone knows an easier/shorter set of code... or I might just throw this all in another file and call it up, same as I do with my header... although I'm not sure if you can call use php to call up portions of the code your using on that page... hmm... I don't really wanna add another 50 lines onto every one of my main files :P Anyway, take a look at this code, see if you can think of a way to simplify/shorten it hehe.
$pt1 = "<IMG src=";
$pt2 = "Pic/counter/";
$pt3 = ".jpg";
if ($var > 999999) {
$var -= 1000000
$1d = 1;
} else {
$1d = 2;
}
if ($var > 899999) {
$var -= 900000
$2d = 9
} else if ($var > 799999) {
$var -= 800000
$2d = 8
} else if ($var > 699999) {
$var -= 700000
$2d = 7
} else if ($var > 599999) {
$var -= 600000
$2d = 6
} else if ($var > 499999) {
$var -= 500000
$2d = 5
} else if ($var > 399999) {
$var -= 400000
$2d = 4
} else if ($var > 299999) {
$var -= 300000
$2d = 3
} else if ($var > 199999) {
$var -= 200000
$2d = 2
} else if ($var > 99999) {
$var -= 100000
$2d = 1
} else {
$2d = 0
}
if ($var > 89999) {
$var -= 90000
$3d = 9
} else if ($var > 79999) {
$var -= 80000
$3d = 8
} else if ($var > 69999) {
$var -= 70000
$3d = 7
} else if ($var > 59999) {
$var -= 60000
$3d = 6
} else if ($var > 49999) {
$var -= 50000
$3d = 5
} else if ($var > 39999) {
$var -= 40000
$3d = 4
} else if ($var > 29999) {
$var -= 30000
$3d = 3
} else if ($var > 19999) {
$var -= 20000
$3d = 2
} else if ($var > 9999) {
$var -= 10000
$3d = 1
} else {
$3d = 0
}
if ($var > 8999) {
$var -= 9000
$4d = 9
} else if ($var > 7999) {
$var -= 8000
$4d = 8
} else if ($var > 6999) {
$var -= 7000
$4d = 7
} else if ($var > 5999) {
$var -= 6000
$4d = 6
} else if ($var > 4999) {
$var -= 5000
$4d = 5
} else if ($var > 3999) {
$var -= 4000
$4d = 4
} else if ($var > 2999) {
$var -= 3000
$4d = 3
} else if ($var > 1999) {
$var -= 2000
$4d = 2
} else if ($var > 999) {
$var -= 1000
$4d = 1
} else {
$4d = 0
}
if ($var > 899) {
$var -= 900
$5d = 9
} else if ($var > 799) {
$var -= 800
$5d = 8
} else if ($var > 699) {
$var -= 700
$5d = 7
} else if ($var > 599) {
$var -= 600
$5d = 6
} else if ($var > 499) {
$var -= 500
$5d = 5
} else if ($var > 399) {
$var -= 400
$5d = 4
} else if ($var > 299) {
$var -= 300
$5d = 3
} else if ($var > 199) {
$var -= 200
$5d = 2
} else if ($var > 99) {
$var -= 100
$5d = 1
} else {
$5d = 0
}
if ($var > 89) {
$var -= 90
$6d = 9
} else if ($var > 79) {
$var -= 80
$6d = 8
} else if ($var > 69) {
$var -= 70
$6d = 7
} else if ($var > 59) {
$var -= 60
$6d = 6
} else if ($var > 49) {
$var -= 50
$6d = 5
} else if ($var > 39) {
$var -= 40
$6d = 4
} else if ($var > 29) {
$var -= 30
$6d = 3
} else if ($var > 19) {
$var -= 20
$6d = 2
} else if ($var > 9) {
$var -= 10
$6d = 1
} else {
$6d = 0
}
$7d = $var
echo $pt1.".$pt2.$1d.">.$pt1.".$pt2.$2d.">$pt1.".$pt2.$3d.">$pt1.".$pt2.$4d.">$pt1.".$pt2.$5d.">$pt1.".$pt2.$6d.">$pt1.".$pt2.$7d.">;

Comments
Just use statcounter.com -
Just use statcounter.com - sign up for a free account and everything is taken care of (with either weekly or monthly stat reports.)
screw that, if i can't code
screw that, if i can't code it, it doesn't need to be on my site hehe...
there were a few issues with that code, but i worked it 'til it works now... just wish there was a simpler more compact way to do it :|
www.pyroamos.com - check it out ^^
Okies, let me see. Been
Okies, let me see. Been awhile from the last PHP code for me, but maybe I can cook up something for you.
I checked your code through and made a bit simpler and shorter version for you as a function. If you save the function in a separated file with .php extension, you can include it into the file you need to use it in and then just call the function in where you want to draw the counter. I added additional return value for this function you can optionally use to draw how many times the counter has gone around. I commented the code quite heavily, so hopefully it helps to understand what's going on. But I add the code here and you can save it as a file.
So if we would have saved the above code in example file called "counter_draw.php", we could use the function by first including it in the pages' PHP, then calling the function in the location of code where we want the counter to be drawn. If you want to draw also the times the counter has gone around, you can use the function's return value for that.
Here's small example in PHP how to use it.
Additionally, the function does not have to be saved in it's own file actually. If you have some file you include in every page already, like example file called functions.php, you can just add this function's code in there and it would then be usable when that file is included.
Hopefully this helped and my explanations was clear enough. This is of course only one kind of solution and there might well be better ones. But for the task you're having, this does the trick.
Also if there's aomething you want to ask about this, feel free to do so.
--------------------------------------------
Have fun, but let others have too :)
Also if you are using
Also if you are using folders in a way where all of your pages' PHP files are not in the same folder, you might want to make the path for the digits' images something you can pass to the function as parameter. That way the function works regardless of the folder you're using it from.
So just as an example, the beginning of the code would then change into:
The function can be still used as in previous example because the path-parameter has been given default value. But in case you would want to change the path for the images for some page, you could then call the function like:
--------------------------------------------
Have fun, but let others have too :)
thx taiv, your the ultimate
thx taiv, your the ultimate resource, lol....
I have the times counter has gone around saved in another file, although I think my counter will go up to 8 digits, but I only wrote my counter to go up to 1,999,999
I do have a file i include into every page on my site, header.php which has, not just the links at the top, but also has the coding for turning the scroll bar black, background, text, link, alink, vlink colors and title... my pages basically look like
<HTML>
<?php
include("header.php");
?>
and right into the body of that particular page.
I'd really like to take a PHP college class, as well as a few other basic computer languages like C++ ect, but the college I've looked at computer classes for have like... general graphics & web development classes and garbage like that, which I doubt would teach me much I don't already know.
The coding for my actual counter is soooo ugly, I'd rly love to learn how to clean it up haha... basically it just calls a txt file, adds +1 to the number in it and resaves it, I'm fairly sure there is a way to change it so all the diferant pages counters are on the same file, so I don't have to have a seperate txt file for every page I have a counter for, just like I'd like to learn how to make a forum for my site, although that would involve learning to mix php and mySQL which I think is proably a little beyond the realm of general forum advice.
Thanks alot for the help though ^^ I hope you enjoyed giving it as much as I enjoyed reading it keke.
www.pyroamos.com - check it out ^^
What is the dif of
What is the dif of include_once and include btw?
www.pyroamos.com - check it out ^^
PyroAmos wrote: What is the
What is the dif of include_once and include btw?
The difference with include and include_once is that the later checks if the code you're including has already been included before including it (oh man, lots of includes, haha). You can run into trouble if you're example accidentally including functions that are already declared in the code.
So include_once is safer to use because it checks if the code you're including is already there. That kind of situation can be easy example by including files that in turn include files that contain code you're already having and that would collide with the existing code. So you might end up accidentally overriding variables etc.
But when knowing what you're including and keeping an eye that you're not getting example same functions twice, you can manage with only include("file"). I'm just used to use the include_once when it can be used, so that's why it's also in the code example. Keep in mind that sometimes include_once is not working for the task if you'd example want to use same block of code in several spots of the page for purpose.
--------------------------------------------
Have fun, but let others have too :)
thanks for the
thanks for the explenation...
I'm not using your code though :( I don't really understand for commands, and a few of the commands you use in your for command... been using modified version of the code I gave, as it works and I understand everything going on in it... I really need to find a good php class where I can learn alot of this stuff... random coding based on alt-tabbing to sites like htmlgoodies.com, while I can produce the effect I want, turns out alot longer and uglier than how I would like it to be, and although I could use the code you gave, I don't really understand some of it, and I don't really like putting code in that I don't have a solid grasp of how it is operating :| thanks a ton for the help though, I may just look up for commands and figure out how that last coding operates, so I can use it comfortably :P
www.pyroamos.com - check it out ^^
I totally understand your
I totally understand your decision on that - it's good to know what the code does before using it. My above version of your code surely has couple weird looking functions and one of them is most likely the sprintf for formatted printing. PHP.net is a good place to search for what the functions actually do and basically these API documentations becomes the most important source for your coding after you have grasped the syntax of the programming language and have understanding for the philosophy of programming (functions, object oriented programming etc.).
To learn the philosophy of the programming you can basically just select some language supporting most of the principles and use that knowledge to later learn other languages with greater ease - it's mostly only the syntax you need to learn then as the philosophies stays the same. Thus if there is course for learning C++, that would surely be language to learn also most, if not even all the philosophies you need to know about programming. C++ can be a bit confusing at beginning, but it's syntax does have similarities with PHP you've already been learning.
I do not start explaining my code above with more detail yet, I give you opportunity to look the functions from the PHP.net first because I think it could be better for your learning. Later if needed, I can elaborate the explanations further to hopefully bring some understanding to the code.
--------------------------------------------
Have fun, but let others have too :)
hmm... I think I found a way
hmm... I think I found a way to use functions and the for tag... tell me does this look like it would work? Only problem I see with it is number would have to be 100,000~999,999 to work... maybe I'll try to figure out a way to calculate how many digits it is, so say its 4 digits, I could echo 10.jpg (which would be graphic for 0) twice and run function lessdigit() twice and then run eachnum() 4 times... after I go eat lunch... I got a little work I should get done today though to LOL
<?PHP
*/ Setup Initial Variables */
$a = 999999
$c = 100000
$digit = 10
$var = $countervalue
/* Create function to reduce to next digit */
function lessdigit() {
$freevar = 10 - $digit
$freevar *= $c
$a += $freevar
$a -= 9;
$a /= 10;
$c /= 10;
$digit = 10
}
/* Create function to echo current front digit /*
function eachnum() {
for (; $c >= $var ; ) {
if ($var > $a) {
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
lessdigit()
} else {
$a -= c$
$digit -= 1
}
}
/* Now to put counter on site */
eachnum()
eachnum()
eachnum()
eachnum()
eachnum()
eachnum()
?>
www.pyroamos.com - check it out ^^
dmn I just realized, nothing
dmn I just realized, nothing changes the value of $var... I could throw that in after echo and before calling lessdigit() function, just put somehting like $varreduc = $digit * $c then $var -= $varreduc
www.pyroamos.com - check it out ^^
Ignore that last one, read a
Ignore that last one, read a little more about functions and learned u have to define and transfer stupid variables, lol... have fix for $var reduc, and funtions variables defined here, this should look alot better
<?PHP
*/ Setup Initial Variables */
$var = $countervalue
$a = 999999
$c = 100000
$digit = 10
/* Create function to reduce to next digit */
function lessdigit($a = $a, $c = $c, $digit = $digit) {
$freevar = 10 - $digit;
$freevar *= $c;
$a += $freevar;
$a -= 9;
$a /= 10;
$c /= 10;
return $a;
return $c;
}
/* Create function to echo current front digit /*
function eachnum($var = $var, $a = $a, $c = $c, $digit = 10) {
for (; $c >= $var ; ) {
if ($var > $a) {
$lessvar = $digit * $c
$var -= $lessvar
return $var;
return $digit;
lessdigit($a, $c, $digit)
return $a;
return $c;
} else {
$a -= c$;
$digit -= 1;
}
}
eachnum($var, $a, $c, 10)
eachnum($var, $a, $c, 10)
eachnum($var, $a, $c, 10)
eachnum($var, $a, $c, 10)
eachnum($var, $a, $c, 10)
eachnum($var, $a, $c, 10)
?>
www.pyroamos.com - check it out ^^
I have now read your code
I have now read your code through several times during these two - three days and I have not really understood what you've tried to do with it, haha. But what I do know is that neither one of the code's versions will work in their current state. The way you've tried to use the functions is a bit off, as well with the for-loop. With functions you can only return one value, but it can be example a table though. But because the return clause will stop running the function and return to the caller when the return clause is executed, you cannot really use several sequential return clauses to forward values.
The for-loops on the other hand uses three statements as a parameter: initializer, comparing and lastly part to be ran at the end of every loop. The way you have used the for-loop won't really work for it's purpose. While-loop might be easier to understand at first and does the trick as long as you remember to increase your iterator or a kind in your loop to prevent it to be stuck to infinite looping.
Also it's generally good idea to name the variables in a way where you see where they are meant for as well as comment the functions and their parameters with what they should do. This helps if other people need to read your code or you have to get back to it later.
Could you explain with more detail what your code should do and maybe I could give some suggestions how to do it. I might just make some number based version for you anyway and maybe explain a bit further that first example of mine that handles the counter value as string. The string based system is pretty much as short of a code as you can do though.
--------------------------------------------
Have fun, but let others have too :)
I see... you can only return
I see... you can only return 1 variable with function... that really sux lol, and yeah, this wouldn't work like that, but here is what I was trying to do.
<?PHP
*/ Setup Initial Variables */
$var = $countervalue
$a = 999999
$c = 100000
$digit = 10
/* Create function to reduce to next digit
function lessdigit($a = $a, $c = $c, $digit = $digit) {
/* First $digit is the number we are using as the return for each. 10-$digit should equal the number of times we had to reduce first digit */
$freevar = 10 - $digit;
/* $c is the number we used to decrease the first number (by subtracting 1000 from 8999 for example) Multiplying the $freevar by this should make it the same as the total that had been decreased from $a */
$freevar *= $c;
/* adding $a and $freevar should equal $a that equals a number with a 9 at begining and end */
$a += $freevar;
/* these decrease a digit off $a and $c (so 9999 becomes 999, 1000 becomes 100) */
$a -= 9;
$a /= 10;
$c /= 10;
return $a;
return $c;
}
/* Create function to echo current front digit /*
function eachnum($var = $var, $a = $a, $c = $c, $digit = 10) {
/* for statement will continue to loop until $c is >= $var */
for (; $c >= $var ; ) {
/* if $var is higher than current $a then 10-$digit (the counter for how many times this if/else has run) is multiplied by $c (which should be a 10000 number) and subtracted from $var, taking off the first digit of the counter it then returns the new $var with first digit off it, and $digit which represents the value of the first digit */
if ($var > $a) {
$freevar = 10 - $digit
$lessvar = $digit * $c
$var -= $lessvar
return $var;
return $digit;
/* now the first function runs to reduce $a and $c 1 digit */
lessdigit($a, $c, $digit)
return $a;
return $c;
*/ the new $a and $c are returned now to be used for the next time the function is run */
} else {
/* this reduced the first digit on $a 1 number (so 8999 would become 7999) and reduces $digit by 1, to count how many times a$ has had to be reduced */
$a -= c$;
$digit -= 1;
}
}
/* now we just run the 2nd function once each for each digit in the counter */
/*every time this function runs, it should take the front digit off $var and return it as $digit, and also reduce the numbers used to find the first digit ($a and $c) by 1 digit) */
/* I left out the echo of the $digit between each running of the function, I'll add it here so you can see what it would look like */
eachnum($var, $a, $c, 10)
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
eachnum($var, $a, $c, 10)
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
eachnum($var, $a, $c, 10)
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
eachnum($var, $a, $c, 10)
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
achnum($var, $a, $c, 10)
echo "<IMG src=\"Pic/counter/$digit.jpg\">";
eachnum($var, $a, $c, 10)
?>
see how I was trying to make it work now? I was thinking of function as able to just call up a set of coding into a spot without having to repeatedly type it, but since you can only return 1 variable, it doesn't really work :|
www.pyroamos.com - check it out ^^
OK, took a while but I think
OK, took a while but I think I got what you were aiming for. There were some parts a bit backwards, like the $c >= $var, but generally your logic does work. I also added a check that prevents errors if the counter has zeros somewhere in it or the counter value is not yet large enough to reach all the digits. In those cases the added code prints zero as supposed.
I tried to keep the code as much the original as possible.This however ain't using the functions etc to it's benefit too well and so has the same code multiple times. Also lots of it is hardcoded, where example the number of digits in the counter has been decided in it. Additionally passing larger counter value than six digits breaks the result so it relies into additional coding to be functioning properly.
--------------------------------------------
Have fun, but let others have too :)
Here is more generic version
Here is more generic version of the code based solely on numbers. It has the same functionality as the first example of mine, but has to rely more on the math to get the same result. Basically the string based version (the first example) is the most simple one, but maybe this yet another version helps in understanding PHP.
This is used exactly the same way as the one in my first example. If it would have been saved in it's own file called "draw_counter.php", you would just include it in your page's file and call the function where the counter is wanted to be drawn. Also you can use the parameters as suiting for the purpose.
--------------------------------------------
Have fun, but let others have too :)
Here comes the code for
Here comes the code for additional functionality for text area to limit it's length in client side. It basically adds functions to existing objects to do so. To give the length, there's multiple choices, but I explain those after the code.
Then the example for the HTML form. I took this code from your page and made couple small changes for this functionality in mind. Mostly these changes are related to the counter box, but I also added tabindex attribute in the form fields to skip the counter.
As you notice in the window.onload, there's several ways to give the maximum length for the text area.
The form.body references to the text area by it's name attribute and if you're familiar with object oriented coding,
form.body.maxlength = 10assigns attribute/variable named maxlength to the form.body object (that is the text area). By doing it this way, it however does not show in the page source. This is to avoid non-standard code in the source. If you do not mind that, you can use theform.body.setAttribute("maxlength", "10");that adds the attribute in the actual page source. This would be same as putting the attribute directly into your HTML in the way of<textarea name="body" maxlength="10" etc.>. Additionally there is example how to pass data from PHP to your JavaScrip, when you could use one variable for the comment length in PHP and pass it to the JavaScript and so make sure you have same values both in client and server side.window.onload is an event that's ran when the whole page has been successfully loaded. JavaScript dealing with page structure should be initialized only after that. So let's go through the window.onload line by line:
window.onload = function() {We assign a function to the window.onload event. This function will be executed when the page is fully loaded.
var form = document.updateform;We get the form handling the comments in a variable to shorten our typing.
form.body.maxlength = <?php echo $commentLength ?>;Then we bring maxlength from PHP and assign it as variable to the text area of the form. As in the above code, there's several ways to do this phase tho, but this one would be good.
form.counter.update = function(count) {this.value = count;};We assign new function to our counter element. The update function will handle the updating the counter value. When we do it this way, we can basically add any kind of element to be our counter - we just need to add update function into it that handles the updating for that element type.
form.body.counter = form.counter;In here we assign/link the counter element into our text area. After this we can reference the counter from the text area.
form.body.onkeyup = limitText;Here we assign the previously declared and defined limitText function into onkeyup event of our text area. This practically means, that when you type into the text area and release a key, the limitText function will be executed.
form.body.onkeyup();We are calling the onkeyup event once to initialize the counter value. Otherwise the counter field would be empty until user starts typing into the text area. We could of course give the initial value on the input field of the counter also by using the PHP variable to inject it into the HTML and remove this line.
The limitText function does the actual work. It basically checks after every key press that did the user exceed the maximum length. If she did not, the function copies the current text into oldvalue attribute of the text area. If the maximum length was exceeded, the previous version of text is copied over into the text area and warning is shown to the user.
If the maxlength is not given either in the source or JavaScript object level, then the text area works as normally. Also the counter element is optional - you do not have to assign a counter element for the limiting to actually work.
--------------------------------------------
Have fun, but let others have too :)
Here comes new version of
Here comes new version of the Javascript for the text area. This one uses SPAN-element for the counter text and at the same time I made some general improvements to the original script.
The lines starting with // are commented out code and basically showing alternative way to do what's then done below. Example using innerHTML is kind of rogue-standard, it's not "real" Javascript, but still widely supported and used because it makes things so much easier in many cases. However, it can and will cause problems in some cases, so I made the document manipulation in somewhat right way below. A lot more complex tho.
//textarea.maxlength = 10;This again offers alternative way to give the maximum length for the text area. As maxlength is not standard attribute for textarea-element, someone might not want to add it into DOM.
As for counter, add the following where ever you want the counter text to show. Now, because of using the standard document manipulation functions instead of innerHTML, you cannot have HTML-tags inside the SPAN-tags of this piece: only text is allowed there.
In here the id of the SPAN-element is important, because the Javascript finds the element by it's id in this case. This allows the element to be located anywhere in the page. The SPAN-element can be empty to start with, the Javascript populates it at the end of window.onload().
--------------------------------------------
Have fun, but let others have too :)