Jump to content

[SOLVED] Help with if($var == TRUE) - I'm so close!


nz_mitch

Recommended Posts

Hi there,

 

I've just about got a sign-up form working perfectly with the Campaign Monitor API. Had a trouble with it being too slow, but with help from people on this board and a bit of digging around I've finally got it super fast.

 

Just having trouble with the right way to define the if is true statement. Here's what I've got:

 

	// Get info from form
$name = $_POST["Field1"];			// Name
$email_in = $_POST["Field2"];		// Raw email from form
$email_out = strtolower($email_in); // Strip emails to lower case
$ref = $_POST["Field109"];			// Referring Agent

// Check if already subscribed, add if not, output error if is

$var = $cm->subscribersGetIsSubscribed($email_out,$list_id);
print_r(array_values($var));
if ($var['0'] == TRUE) {
		echo "Sorry, this email address is already in the database!";
		echo '<br /><br />';
		echo 'Click <a href="/" title="Add a New User">here</a> or use the back button in your browser to try a different subscriber.';
		}
	else {
		// Add to Database with Custom Fields
		$result = $cm->subscriberAddWithCustomFields("$email_out","$name", array('ReferringAgent' => '$ref'));
			if($result['Result']['Code'] == 0) {
		echo "Success! You've added <strong>$name</strong> with the email address <strong>$email_out</strong> to the database.";
		echo "<br />$ref<br />";
		echo 'Click <a href="/" title="Add a New User">here</a> or use the back button in your browser to try a different subscriber.';
	} else {
		echo 'Error : ' . $result['Result']['Message'];
}}

 

This seems to be almost perfect. The print_r outputs True or False correctly - according to whether or not the email address is already subscribed.

 

My problem is that I'm obviously not writing the conditional statement right. If I use:

 

if ($var['0'] == TRUE) 

 

It always subscribes, even if the person is on the list.

 

If I used:

 

if ($var['anyType'] == TRUE) 

 

It never subscribes, even if the person is not on the list.

 

As I mentioned, the print_r indicates the look up is working properly outputting the following if a user is already subscribed:

 

Array ( [0] => True )

 

Or the following if they're not:

 

Array ( [0] => False )

 

I'm sure I'm just doing something stupid with the conditional statement - can anyone put me right?

 

Thanks in advance! :)

 

Link to comment
https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/
Share on other sites

Sure thing:

 

	/**
* @return string A parsed response from the server, or null if something failed.
* @see http://www.campaignmonitor.com/api/Subscribers.GetIsSubscribed.aspx
*/

function subscribersGetIsSubscribed( $email, $list_id = null )
{
	return $this->subscriberUnsubscribe( $email, $list_id, true );
}

 

There's another API option, but it's really slow. Takes 5 - 10 seconds to check the database. The subscribersGetIsSubscribed seems to work very quickly, and is outputting TRUE / FALSE correctly when I print the values, so I'm guessing it should be able to work..

try this

 

if($var[0] === TRUE) {

echo "Sorry, this email address is already in the database!";

echo '<br /><br />';

echo 'Click <a href="/" title="Add a New User">here</a> or use the back button in your browser to try a different subscriber.';

}

 

this will make sure the the value at index 0 of array $var is not only true,1 but also boolen . so this condition wont be true even if $var[0] contains 'data' or any thing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.