Convert a string to an integer

Reply

Join Date: Jul 2006
Posts: 4
Reputation: conetrader is an unknown quantity at this point 
Solved Threads: 0
conetrader conetrader is offline Offline
Newbie Poster

Convert a string to an integer

 
0
  #1
Jul 11th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

Re: Convert a string to an integer

 
0
  #2
Jul 11th, 2006
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]
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 ::

Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: conetrader is an unknown quantity at this point 
Solved Threads: 0
conetrader conetrader is offline Offline
Newbie Poster

Re: Convert a string to an integer

 
0
  #3
Jul 12th, 2006
The problem is that I'm NOT passing the ID to the function. I'm not setting the ID. The switch statement always produces the default.

Any suggestions to fix that?
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 166
Reputation: Lafinboy is an unknown quantity at this point 
Solved Threads: 7
Lafinboy's Avatar
Lafinboy Lafinboy is offline Offline
Junior Poster

Re: Convert a string to an integer

 
0
  #4
Jul 12th, 2006
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]
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 ::

Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: conetrader is an unknown quantity at this point 
Solved Threads: 0
conetrader conetrader is offline Offline
Newbie Poster

Re: Convert a string to an integer

 
0
  #5
Jul 13th, 2006
Thanks for the suggestion. However, it didn't fix the problem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC