nz_mitch Posted April 5, 2009 Share Posted April 5, 2009 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 More sharing options...
Fruct0se Posted April 5, 2009 Share Posted April 5, 2009 Is var an array? if so try: if ($var[0] == TRUE) instead of if ($var['0'] == TRUE) Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801476 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 Thanks for the quick response! I believe it's an array, yea. I tried with if ($var[0] == TRUE) and it always adds to the database, even when it shouldn't. Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801491 Share on other sites More sharing options...
Fruct0se Posted April 5, 2009 Share Posted April 5, 2009 That is strange, have you tried echoing the output of $var[0] to see what it actually is? You can also evaluate the statement like if($var[0]) if you are just evaluating true or false. Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801494 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 print_r(array_values($var)); returns Array ( [0] => True ) (or False is the're not in the database). echo $var; returns Array ??? Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801496 Share on other sites More sharing options...
Fruct0se Posted April 5, 2009 Share Posted April 5, 2009 can you post the code for subscribersGetIsSubscribed Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801500 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 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.. Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801502 Share on other sites More sharing options...
taquitosensei Posted April 5, 2009 Share Posted April 5, 2009 just an FYI you could do it this way $var=True; if($var) { // do whatever you're going to do if you're variable is true } or Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801507 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 @taquitosensei Sorry don't quite follow what you mean there? Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801512 Share on other sites More sharing options...
lostnucleus Posted April 5, 2009 Share Posted April 5, 2009 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. Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801518 Share on other sites More sharing options...
nz_mitch Posted April 5, 2009 Author Share Posted April 5, 2009 Thanks @lostnucleus! That almost worked, but when I changed it to $var['anyType'] it fixed it! So I needed === and 'anyType' Thanks so much everyone for your help! Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801520 Share on other sites More sharing options...
taquitosensei Posted April 5, 2009 Share Posted April 5, 2009 if($var) { } checks for a boolean True, which would be 1 or True/TRUE/true if(!$var) { } checks for false Link to comment https://forums.phpfreaks.com/topic/152608-solved-help-with-ifvar-true-im-so-close/#findComment-801531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.