thecase Posted July 30, 2009 Share Posted July 30, 2009 Hi, This is my code // Get user ID $id = '1'; // Get users who want to be E-mailed $email = mysql_fetch_array(mysql_query("SELECT users FROM email WHERE notification = 1")); // Store the info into an array $email = explode(",", $email[0]); echo "<P>Userid " . $id; for ($i = 0; $i < count($email); $i += 1) { if ($email[$i] == $id) { echo " does"; } else { echo " not"; } } echo " want to be emailed.</P>"; Any ideas why its echoing user id does not want to be emailed, its echoing back both outcomes. Thanks Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/ Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 Hi, This is my code // Get user ID $id = '1'; // Get users who want to be E-mailed $email = mysql_fetch_array(mysql_query("SELECT users FROM email WHERE notification = 1")); // Store the info into an array $email = explode(",", $email[0]); echo "<P>Userid " . $id; for ($i = 0; $i < count($email); $i += 1) { if ($email[$i] == $id) { echo " does"; } else { echo " not"; } } echo " want to be emailed.</P>"; Any ideas why its echoing user id does not want to be emailed, its echoing back both outcomes. Thanks change $i += 1) to $i++) change $i < count($email); to $i <= count($email); and print out the $email[0]. see if theres any data. Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886701 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 Thanks for that. $email['0']; prints 1 which is correct and now its echoing Userid 1 does not not want to be emailed. echo "<P>Userid ".$id; for ($i = 0; $i <= count($email); $i++) { if ($email[$i] == $id){ echo " does"; }else{ echo " not"; } } echo " want to be emailed.</P>"; echo "$email[0]"; Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886769 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 Can you further explain what you are trying to accomplish? Then i can understand lol... Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886776 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 Ok SELECT users FROM email WHERE notification = 1 Come back with the data "21,4,65,23,5,1,3" and theses are all member ids. I want to gather them all out the database and check if my id (when logged into my system) matches any so for example My ID = 43 echo "User ID does not want to be emailed"; My ID = 23 echo "User ID does want to be emailed"; Hope this is clear Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886783 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 Ok SELECT users FROM email WHERE notification = 1 Come back with the data "21,4,65,23,5,1,3" and theses are all member ids. I want to gather them all out the database and check if my id (when logged into my system) matches any so for example My ID = 43 echo "User ID does not want to be emailed"; My ID = 23 echo "User ID does want to be emailed"; Hope this is clear uh.. Your ID can never match anotehr ID because its a UNIQUE ID if thats how you set it up... If NOTIFICATION = 1, then that means those members want to be emailed. ' <?php // GRAB ALL THE MEMBERS TO BE EMAILED $sql = mysql_query("SELECT * FROM users WHERE notification = '1'"); if($sql){ while($row= mysql_fetch_array($sql)){ /// ,mail the members } } ?> EDIT: IM SO CONFUSED NOW Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886792 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 Oh yeah i got rid of the WHERE sitll no luck though. The user id is unique the email field isnt its just a field with all numbers in them that link back to the users id. So the data reads 21,4,65,23,5,1,3. I need to make a script to take all the numbers out of that sperated by a comma and see if any match up with $id which for example say was 21 Hope this is clear Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886793 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 Something like this? <?php $email[0] = '21,4,65,23,5,1,3'; $id = 4; $email = explode(',', $email[0]); foreach ($email as $e) { if ($e == $id) { echo 'Yes please<br/>'; } else { echo 'No thanks<br/>'; } } Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-886811 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 That is very near what I want but that prints out every members outcome hence the foreach I just need to get the outcome of the $id Hope this is clear Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887175 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 then take out the echo in the else statement Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887177 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 Oh the $id isnt static its dynamic it comes from the user id when logged into my system but in the example I have used a static number. Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887178 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 If you want only echo when the $id matches the email number then you can take out the whole else part. And this was just an example.. no matter where from that $id came just put your variable in the place of the $id in the example and erase that static one. Same goes to the $email -array.. it was there to demostrate maybe a result from your mysql query or something like that? Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887189 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 We are even closer now thanks alot my last problem is with this code $email[0] = '21,4,65,23,5,1,3'; $id = 40; $email = explode(',', $email[0]); foreach ($email as $e) { if ($e == $id) { echo 'Yes please<br/>'; } } As id 40 isnt in the list it does nothing because im telling the script to do nothing, but when I add an else it doesnt work so like I have it for if the id matches I can tell the script to do something in this case it echos "Yes please" but if it doesnt match how can I tell the script to do something like "No please". Thanks Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887203 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 then you want the else condition that tendolla posted before .. which you said earlier that you didn't want Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887209 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 The problem with the else condition is that it gives me every outcome $email[0] = '21,4,65,23,5,1,3'; I dont want to know about all them users just the $id user. That was why I said I didnt want it. Hope this is clear Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887223 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 i think this is what you mean <?php $email[0] = '21,4,65,23,5,1,3'; $id = 40; $email = explode(',', $email[0]); $idExists = false; foreach ($email as $e) { if ($e == $id) { echo 'Yes please<br/>'; $idExists = true; } } if(!$idExists) { echo "ID doesn't exist in the list or whatnot"; } Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887225 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 This has the same effect as does if-else statement. And no.. this is not definitely clear anymore... Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887227 Share on other sites More sharing options...
waynew Posted July 30, 2009 Share Posted July 30, 2009 <?php $email[0] = '21,4,65,23,5,1,3'; $id = 40; $email = explode(',', $email[0]); $idExists = false; foreach ($email as $e) { if ($e == $id) { echo 'Yes please<br/>'; $idExists = true; } else{ echo 'No please<br />'; } } if(!$idExists) { echo "ID doesn't exist in the list or whatnot"; } Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887228 Share on other sites More sharing options...
thecase Posted July 30, 2009 Author Share Posted July 30, 2009 lonewolf217, that was exactly what I was looking for many thanks to you and to everyone Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887231 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 This has the same effect as does if-else statement. And no.. this is not definitely clear anymore... its slightly different. before it would output the else for each ID comparison. now it only outputs once. Once if it compared successfully, and once if it did not find the ID in the list Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887235 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 This has the same effect as does if-else statement. And no.. this is not definitely clear anymore... its slightly different. before it would output the else for each ID comparison. now it only outputs once. Once if it compared successfully, and once if it did not find the ID in the list Indeed, this is true. edit: Not that this makes any difference but it could've been done in the else also. <?php $email[0] = '21,4,65,23,5,1,3'; $id = 123; $email = explode(',', $email[0]); foreach ($email as $e) { if ($e == $id) { echo 'Yes please<br/>'; } else { $msg = isset($msg) ? "ID doesn't exist in the list or whatnot" : false; } } echo $msg; Link to comment https://forums.phpfreaks.com/topic/168124-solved-explode/#findComment-887238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.