| | |
Convert a string to an integer
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Solved Threads: 0
I am using a script that counts clicks on various links in the home page. The site is for the community channel of a cable TV company and the user can purchase ads or post an obituary or a weather related delay or calcellation.
We want to add a count so the siteowner can see how many clicks on the paid ads links actually result in an ad being submitted for posting. When the user clicks on the button "Place Ad", an email is sent to the community channel. The following code was added to email.php
[php]echo "The ad selected is $ad";
//Yes the name of the ad is printed to screen
switch($ad){
case "One Day General or Business Message":
$id=2;
break;
case "One Month Nonprofit Message":
$id=3;
break;
default:
echo "The ID is not getting thru.";
break;
}
completed($id); [/php]
The function called is as follows:
[php]function completed($id){
// First check if the ID is set and if it is valid (contains only numbers)
$id = $_POST['id']; echo "ID in function completed($id) is $id<br />";
//NO! the ID does not print to screen
if(empty($id) || preg_match("/\D/",$id)) {die("Invalid ID, numbers only allowed!");}
// This scentence does print to screen
// Get lines from file
$lines=file($settings['logfile']);
// Let's find the line that contains our ID
$found=0;
$i=0;
foreach ($lines as $thisline) {
if (preg_match("/^($id\%\%)/",$thisline)) {
$thisline=chop($thisline);
// We found the line, now we get number of ads PLACED from the line
list($id,$added,$url,$count,$placed,$name)=explode("%%",$thisline);
// Increase placed by 1 and update this line
$placed++;
$lines[$i]=$id."%%".$added."%%".$url."%%".$count."%%".$placed."%%".$name.$newline;
$found=1;
break;
}
// This line didn't contain ID, lets go to the next one
$i++;
}
if($found != 1) {die("This ID doesn't exist!");}
// Rewrite the log file with the updated line
$content = implode('', $lines);
$fp = fopen($settings['logfile'],"wb") or die("Can't write to log file! Please Change the file permissions (CHMOD to 666 on UNIX machines!)");
fputs($fp,$content);
fclose($fp);
// Redirect to the link URL
Header("Location: $url");
exit();
}
?>[/php]
Does anyone have any idea why the above code doesn't work. Is there something special that must be done to assign a string to an integer value?
Any help will be greatly appreciated
We want to add a count so the siteowner can see how many clicks on the paid ads links actually result in an ad being submitted for posting. When the user clicks on the button "Place Ad", an email is sent to the community channel. The following code was added to email.php
[php]echo "The ad selected is $ad";
//Yes the name of the ad is printed to screen
switch($ad){
case "One Day General or Business Message":
$id=2;
break;
case "One Month Nonprofit Message":
$id=3;
break;
default:
echo "The ID is not getting thru.";
break;
}
completed($id); [/php]
The function called is as follows:
[php]function completed($id){
// First check if the ID is set and if it is valid (contains only numbers)
$id = $_POST['id']; echo "ID in function completed($id) is $id<br />";
//NO! the ID does not print to screen
if(empty($id) || preg_match("/\D/",$id)) {die("Invalid ID, numbers only allowed!");}
// This scentence does print to screen
// Get lines from file
$lines=file($settings['logfile']);
// Let's find the line that contains our ID
$found=0;
$i=0;
foreach ($lines as $thisline) {
if (preg_match("/^($id\%\%)/",$thisline)) {
$thisline=chop($thisline);
// We found the line, now we get number of ads PLACED from the line
list($id,$added,$url,$count,$placed,$name)=explode("%%",$thisline);
// Increase placed by 1 and update this line
$placed++;
$lines[$i]=$id."%%".$added."%%".$url."%%".$count."%%".$placed."%%".$name.$newline;
$found=1;
break;
}
// This line didn't contain ID, lets go to the next one
$i++;
}
if($found != 1) {die("This ID doesn't exist!");}
// Rewrite the log file with the updated line
$content = implode('', $lines);
$fp = fopen($settings['logfile'],"wb") or die("Can't write to log file! Please Change the file permissions (CHMOD to 666 on UNIX machines!)");
fputs($fp,$content);
fclose($fp);
// Redirect to the link URL
Header("Location: $url");
exit();
}
?>[/php]
Does anyone have any idea why the above code doesn't work. Is there something special that must be done to assign a string to an integer value?
Any help will be greatly appreciated
This line in your function is wrong:
[PHP]$id = $_POST['id']; echo "ID in function completed($id) is $id<br />";[/PHP]
You are passing the $id through the function call, so no need to reference the $_POST value. Change the first line to:
[PHP]echo "ID in function completed($id) is $id<br />";[/PHP]
[PHP]$id = $_POST['id']; echo "ID in function completed($id) is $id<br />";[/PHP]
You are passing the $id through the function call, so no need to reference the $_POST value. Change the first line to:
[PHP]echo "ID in function completed($id) is $id<br />";[/PHP]
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.
Lafinboy Productions
:: Website Design :: Website Development ::
Lafinboy Productions
:: Website Design :: Website Development ::
If $ad is being passed through a $_POST submission then it is possible that there are extra hidden characters being returned, and so the switch statements will never match.
Try using trim and strtolower on the $ad variable, and also on the switch match value, so that both values should be the same:
[PHP]echo "The ad selected is $ad";
//Yes the name of the ad is printed to screen
switch(trim(strtolower($ad))){
case "one day general or business message":
$id=2;
break;
case "one month nonprofit message":
$id=3;
break;
default:
echo "The ID is not getting thru.";
break;
}
completed($id);[/PHP]
Try using trim and strtolower on the $ad variable, and also on the switch match value, so that both values should be the same:
[PHP]echo "The ad selected is $ad";
//Yes the name of the ad is printed to screen
switch(trim(strtolower($ad))){
case "one day general or business message":
$id=2;
break;
case "one month nonprofit message":
$id=3;
break;
default:
echo "The ID is not getting thru.";
break;
}
completed($id);[/PHP]
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.
Lafinboy Productions
:: Website Design :: Website Development ::
Lafinboy Productions
:: Website Design :: Website Development ::
![]() |
Similar Threads
- to convert string to integer value (Legacy and Other Languages)
- String to integer conversion (C)
- Convert string "aA" to a BigInteger (Java)
- Converting String to Integer help (C++)
- String to integer to ascii (C)
Other Threads in the PHP Forum
- Previous Thread: PHP e-mail copy to viewer themselves ???
- Next Thread: registry entry
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql navigation number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white xml youtube





