Filtering rows starting with specified characters?

  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

Alright so I have a table. I don't want to display rows with titles starting with N or A (The first letter of the title)

This is what i have coded so far but somehow I can't find out why it still display all the rows in the table, instead of removing the ones with a title starting with N or A

Code: [ Select ]
$letters = array('N', 'A');
$numbers = mysql_query("SELECT * FROM table ORDER BY title ASC");
$i = 0;
    while($row = mysql_fetch_array($numbers))
        {
        $title = substr($row['title'],0,1);
        if ($title!=$letters[$i])
            {
                echo ($row['title'] . "<br />");
            }
        $i++;
        }
  1. $letters = array('N', 'A');
  2. $numbers = mysql_query("SELECT * FROM table ORDER BY title ASC");
  3. $i = 0;
  4.     while($row = mysql_fetch_array($numbers))
  5.         {
  6.         $title = substr($row['title'],0,1);
  7.         if ($title!=$letters[$i])
  8.             {
  9.                 echo ($row['title'] . "<br />");
  10.             }
  11.         $i++;
  12.         }



Anyone know the problem?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

http://www.php.net/strspn
http://www.php.net/language.types.strin ... ing.substr

Code: [ Select ]
$numbers = mysql_query("SELECT * FROM table ORDER BY title ASC");
 
$omit = 'NnAa';
 
while($row = mysql_fetch_assoc($numbers))
{
    if( ! strspn($row['title']{0}, $omit))
    {
        echo "{$row['title']}<br/>";
    }
}
  1. $numbers = mysql_query("SELECT * FROM table ORDER BY title ASC");
  2.  
  3. $omit = 'NnAa';
  4.  
  5. while($row = mysql_fetch_assoc($numbers))
  6. {
  7.     if( ! strspn($row['title']{0}, $omit))
  8.     {
  9.         echo "{$row['title']}<br/>";
  10.     }
  11. }
  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

wow that's amazing, i didn't know about that function. thanks
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

for the query part you can actually do:
Code: [ Select ]
$numbers = mysql_query("SELECT * FROM table WHERE columnname NOT LIKE 'N%' AND columnname NOT LIKE 'A%' ORDER BY columnname ASC");


I just query my sql using this and it works fine =]
  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

Oh thats cool. How come N% selects only the first character? t's the % doing that or what?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

In SQL the % is the equivelent of a wildcard.

N% will match anything starting with N
%N will match anything ending with N
%N% will match anything with N anywhere in it

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 137 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.