Jump to content

[SOLVED] Explode()


thecase

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

Ok :P

 

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
Share on other sites

Ok :P

 

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
   }
}

?>

 

:P

 

 

EDIT: IM SO CONFUSED NOW

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.