FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP and MySQL questions





bladesage
Okay, first off, is it possible to add zeros before a number? I want to do it for a hit counter script, but it just keeps adding zero to the number itself Confused.

Example:
000002 instead of 2

And for MySQL, how the heck can I make it update values correctly? It won't seem to work right...

Code:

$query1 = "UPDATE counters SET page_hits=$curhits WHERE page_id=$pg_id";


When I run this through a mysql_query, it doesn't update it at all Crying or Very sad.

If anyone can help me with this, I'd really appreciate it.
BlackSkad
First of all, a number with leading zero's can be made by calling this function.

Code:
<?php
/**
 * Functions creating a string from the given number
 * by appending 0 in front of it, untill it reached the given length.
 *
 * Calling the function like this: getLeadingZeroString (39, 6);
 * will return a string which looks like "000039"
 */
function getLeadingZeroString ($number, $stringLength) {
   //string that will be returned.
   var $string = "";

   //calculate the number of 0 that need to be appended.
   $numberLength = strlen ("" . $number);
   $zeros = $stringLength - $numberLength;
   //append the zeros to $string
   for ($i = 0; $i < $zeros; $i++) {
      $string .= '0';
   }
   //finally, append the number to the end of the string and return it.
   $string .= $number;
   return $string;
}
?>


Secondly, you normally just add only 1 view to the total number of views is it? And after creating the sql, you also need to send it to the database, using mysql_query. So the update script looks like this (assuming that you assign a proper value to $pg_id before calling this script):

Code:
$updateViewsSQL = "UPDATE counters SET page_hits = page_hits + 1 WHERE page_id = '$pg_id'";
$updateViewsQuery = mysql_query ($updateViewsSQL)
   or die ("An error occured while updating the counters!" . mysql_error());


Greatz,
BlackSkad
Ranfaroth
bladesage wrote:
Okay, first off, is it possible to add zeros before a number?
Just use the str_pad function.
svecia
bladesage wrote:
Okay, first off, is it possible to add zeros before a number? I want to do it for a hit counter script, but it just keeps adding zero to the number itself Confused.
Example:
000002 instead of 2
.


Use :
printf ("%06d",$Your_counter);

That is % format-start
0 left padding
6 # digits
d Integer presentation
Related topics
PHP, MySQL...
ASP +PHP+MySQL Tutorials
what is your php mysql apache installer package
Need some php/mysql dlls
The Basics (php, mysql etc)
What is useful way in this php+mysql problem?
PHP/Mysql - beginner!
A very good PHP MySQL Tutorial
Setup php,mysql and others...
php, mysql and iis5
 EASY 40 frih$ IF YOU KNOW PHP+MySQL 
Php Mysql security testing
Project to join - php,mysql??
Link PHP/Mysql with Outlook Express
knowing php/mysql, Is CGI/Perl worth learning?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.