Jump to content

[SOLVED] how to sort my members by email type? little help


Recommended Posts

what is the easiest way to sort my members by email type..

 

I tried to sort by email via the database but it sorts from the begining of email not the type..

 

example

 

a@yahoo.com

b@hotmail.com

c@gmail.com

d@yahoo.com

 

I need it to

 

c@gmail.com

b@hotmail.com

a@yahoo.com

d@yahoo.com

 

here is the code I am starting with:

 

$db=mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbdatabase,$db);

$sql="SELECT email FROM dt_members ORDER BY email;";
$result=mysql_query($sql);

while($row=mysql_fetch_assoc($result)){
echo $row['email'];
echo "<br>";
}
echo "Done.........";
?>

 

also if I wanted to search the back half of a variable how would I do that?? for example

 

$name="paul@yahoo.com";

 

how do I say?

 

if last 9 of name="yahoo.com"???

 

thanks for any help

 

Paul

You could split the email colum in your database to two columns containing the name and the domain, "a" and "yahoo.com". Or

$atpos = strpos($string, "@");
$name = substr($string, $atpos+1, strlen($string)-$atpos-1);

 

Will give you the domain from the email string. From there you can sort it yourself, if you want.

I tried this and $name comes back blank.... I replaced all the $string you gave with the variable I wanted to break up,,, did I mess something up??

 

thanks for the help

 


$db=mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbdatabase,$db);

$sql="SELECT email FROM dt_members ORDER BY email;";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$atpos = strpos($row['email'], "@");
$name = substr($row['email'], $atpos+1, strlen($row['email'])-$atpos-1);
echo $name;
if ($name=="gmail.com"){
echo $row['email'];
echo "<br>";
}
}
echo "Done.........";


anytime you're trying to order your results in a marginally complex way, have a look over in the MySQL manual's functions and operators section.  there's a good chance you can get MySQL to do the gruntwork as opposed to PHP, which is good practice because i'm sure MySQL often feels like it's got nothing to do but sit around and fiddle its thumbs.  you can use the SUBSTRING_INDEX() in this case:

 

SELECT email FROM dt_members ORDER BY SUBSTRING_INDEX(email, '@', -1)

 

if that spits out a syntax error on account of using a function in the ORDER BY clause, you can always SELECT that function and use its alias in the ORDER BY clause.

That's cool, didn't know you could do that. I would use that method over organizing it yourself.

 

As for $name echoing nothing, all I can assume is that the $row['email'] variable doesn't contain an "@". I just checked the code and it worked fine. Assuming you want to continue to try to fix this, I would echo out the $row['email'] variable before the strpos line and see if it has the @ symbol.

lemmin

 

I figured it out it was a simple syntax error on my part... I got it working your way but I will save the mysql thing for future reference...

 

thanks guys... I am starting a new thread now maybe you can help on :)

 

appreciate it...

 

Paul

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.