$POST field, then see what letter it starts with and run action based on it?

Discussion in 'PHP' started by wvccboy, Feb 14, 2008.

  1. #1
    Here's what I have.

    The processing field $post['field'];

    Now when the field receives the message, I set a new variable $field = $post['field'];

    How can I use PHP to detect the letter it begins with, then place that in the proper table in mySQL?

    What I'm making is a small directory broken down into the alphabet. When something is submitted it should break down the word, take the first letter and then put it in the proper table in mySQL.

    And then lastly should I use cases to break down each alphabet when clicked on?
    For example index.php?alpha=h would bring you to all the H listings.

    Thanks!
     
    wvccboy, Feb 14, 2008 IP
  2. mines

    mines Well-Known Member

    Messages:
    1,127
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #2
    quickly written, and untested (so probably got a really obvious error i just missed :p ), also bit hard to work out exactly what you want to do
    
    
    $table = mysql_real_escape_string($field{0});
    
    mysql_query("insert into `my_table_" . $table . "`...");
    
    Code (markup):
    (you may want to look at http://uk2.php.net/manual/en/function.substr.php)

    though you shouldn't really have a table for each letter (unless it really is some obscure directory where it is more suitable). just put a new field (category) in a new (main) table. (or if its a small table you could just use where `name` LIKE "a%" )

    edit - mysql_Real_escape_string is half uneeded there, really... and you should check if the table exists or you will just get errors.
     
    mines, Feb 14, 2008 IP
  3. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #3
    Didn't see that!

    That's exactly what i need (substring)

    Thanks!
     
    wvccboy, Feb 14, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually, if you only need to look at the first letter in the string, you do not have to do a complete function like substr. Every string is an array of chars, therefore, you can do $field[0] and it will return the 1st element of that char array, I.E. the first letter.
     
    zerxer, Feb 14, 2008 IP
  5. mines

    mines Well-Known Member

    Messages:
    1,127
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Yeah, it covers that feature on that page.
     
    mines, Feb 15, 2008 IP