944,111 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 17747
  • PHP RSS
Jul 11th, 2006
0

Convert a string to an integer

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
conetrader is offline Offline
4 posts
since Jul 2006
Jul 11th, 2006
0

Re: Convert a string to an integer

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]
Reputation Points: 16
Solved Threads: 7
Junior Poster
Lafinboy is offline Offline
166 posts
since Jul 2004
Jul 12th, 2006
0

Re: Convert a string to an integer

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
conetrader is offline Offline
4 posts
since Jul 2006
Jul 12th, 2006
0

Re: Convert a string to an integer

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]
Reputation Points: 16
Solved Threads: 7
Junior Poster
Lafinboy is offline Offline
166 posts
since Jul 2004
Jul 13th, 2006
0

Re: Convert a string to an integer

Thanks for the suggestion. However, it didn't fix the problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
conetrader is offline Offline
4 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP e-mail copy to viewer themselves ???
Next Thread in PHP Forum Timeline: registry entry





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC