spdwrench Posted August 16, 2007 Share Posted August 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/ Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326042 Share on other sites More sharing options...
spdwrench Posted August 16, 2007 Author Share Posted August 16, 2007 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........."; Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326056 Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326064 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326068 Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 alternatively, if you ever just want to nab everything past the last instance of a certain character (this is especially handy for file extensions), you can use strrchr(). Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326070 Share on other sites More sharing options...
spdwrench Posted August 16, 2007 Author Share Posted August 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65289-solved-how-to-sort-my-members-by-email-type-little-help/#findComment-326073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.