ive got some code where i have a check in a funtion that sets a variable to 1 or 0 whether the check returns true or false... problem i have is that the variable is not passed from the function to the rest of the code... any ideas? ive tried things like return $variable but that doesnt seem to work
show your code then, you can either use return or make the variable you are trying to set, a global so its visible inside and outside the function
i just call the function using:
EmailCheck(); (near the top of the code)
and heres the function at the bottom of the code:
function EmailCheck() { global $tablename; $emailvalidate=0; $checkthis = mysql_query("SELECT * FROM $tablename"); $checkthis = mysql_fetch_array($checkthis); $checkthis = strpos($checkthis['email'], $_POST['email']); if ($checkthis > 1) { $emailvalidate=1;// email exists in the database } else { $emailvalidate=0;// email does not exist in the database } return $emailvalidate; global $emailvalidate; }
$check = EmailCheck(); should work fine then... even with out the global def at the bottom, and just for safey, I would define it outside the function as a global, then initialize it inside the function like you did $tablename
i left the return $emailvalidate in the function (deleted the global $emailvalidate), and changed the EmailCheck(); to $emailvalidate = EmailCheck(); seems to work now, just that my check doesnt seem to work. the strpos returns false even when the email is in the db. (btw all the emails are in 1 row to get rid of the loop when the newsletter is sent (was slow on 4 emails(4rows))) not sure why its still not working lol. if you wanna take a look at some of the code (ie the subscribe part) then just ask..
So the email check gets info from db. the info from db is used as the 'haystack' and the posted email as the needle... heres an example of what it looks like in the db: email@test1.com, email@test2.com, email@test3.com still no idea why it doesnt work even with the correct email $checkthis returns 0 before the if statement...
function EmailCheck() { global $tablename; $checkthis = mysql_query("SELECT * FROM $tablename"); $checkthis = mysql_fetch_array($checkthis); $checkthis = strpos($checkthis['email'], $_POST['email']); if ($checkthis > 1) { $emailvalidate=1;// email exists in the database } else { $emailvalidate=0;// email does not exist in the database } return $emailvalidate; }
show me where you are calling this so i know you arent messing something else up... from what I can tell, that should work....
well no need really. theres nothing that says: $emailvalidate=0; anywhere apart from in the if statement.. so we know that it is being stated in the right place, its the strpos that doesnt seems to be working...
i also did a test code and found out that the $checkthis > 1 is wrong since the first char in a string is 0 so it will always read as not found. therefore ive gone back to the if ($checkthis === false) which seems to work will post or edit this one to see if i get it to work.. 🙂 ==EDIT==
Woo the === false works, curse that person who said to use > 1 🙂
just need to fix the delete part now...
ah i fixed it:
i changed:
$newemail = str_replace(', '.$_POST['email'],"",$existingemail);
to
$newemail = str_replace(", ".$_POST['email']."","",$existingemail);
that seem to fix it. woohoo
great when its a problem fixed by _self