Development php text entry question...

Discussion in 'Software' started by Ubermich, 22 Feb 2003.

  1. Ubermich

    Ubermich He did it!

    Joined:
    21 Jun 2002
    Posts:
    4,389
    Likes Received:
    1
    Let's say I have a page that asks for your computer's name... And I want to make a list of the second letter of everyone's computer's name...

    How would I go about pulling just that second letter from the entry, omitting all of the other letters of the name?

    So when you type in "Commando" and click the submit button, the page only sees the "o" and discards the "C-m-m-a-n-d-o"

    If that makes any sense...

    Any help is greatly appreciated :)
     
  2. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1
    PHP:
    $second_letter substr(1,2,$computer_name);
    Where $computer_name is the variable that is holding the computer name entered by the user.

    HTH.
     
  3. Ubermich

    Ubermich He did it!

    Joined:
    21 Jun 2002
    Posts:
    4,389
    Likes Received:
    1
    then if I want it to output a certain number for the letter, do I have to do an If, then 25 elseif's for it?
     
  4. linear

    linear Minimodder

    Joined:
    5 Oct 2001
    Posts:
    4,393
    Likes Received:
    1
  5. Ubermich

    Ubermich He did it!

    Joined:
    21 Jun 2002
    Posts:
    4,389
    Likes Received:
    1
    Bah, I'm getting quite tired working this out. I'm trying make a page where you enter your name and it takes you to your new name based on This

    I'm probably going about it all wrong, but so far I've got it like so:
    <htmlhtmlhtmlhtml...
    <form action="poopants.php" method=post>

    My name is:
    <br>
    First <input type="text" name="firstname"> Last <input type="text" name="lastname">
    <p>
    <input type="submit" name="submit" value="Give me my new name">
    </form>
    ...htmlhtmlhtml>

    Then I have poopants.php:

    <?php
    $thirdletter = sustr(1,2,3,$firstname);

    if($thirdletter == "a"){
    $newfirstname = "poopsie"
    } elseif($thirdletter == "b"){
    $newfirstname = "lumpy"
    }
    ?>

    So, umm, did I do anything right?
     
  6. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1
    Should be
    PHP:
    $thirdletter substr(2,3,$firstname);
    Then after that, you're probably better of with a switch/case statement.
    PHP:
    switch($thirdletter) {
      case 
    'a':
        
    $newfirstname "poopsie"
        
    break;
      case 
    'b':
        
    $newfirstname "lumpy"
        
    break;
    }
    etc. :)
     
  7. linear

    linear Minimodder

    Joined:
    5 Oct 2001
    Posts:
    4,393
    Likes Received:
    1
    Or just build an associative array, it will make the code more readable/maintainable:

    Code:
    $newfirstnames = array(
      'a' => 'poopsie',
      'b' => 'lumpie',
      // and so on....
    );
    
    $newfirstname = $newfirstnames[$thirdletter];
    
     
  8. Ubermich

    Ubermich He did it!

    Joined:
    21 Jun 2002
    Posts:
    4,389
    Likes Received:
    1
    How does that bit work? I don't follow the "$newfirstnames[$thirdletter]"
     
  9. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1
    Ah. Very clever :)
    /me makes note
     
  10. DeX

    DeX Mube Codder

    Joined:
    22 Jul 2002
    Posts:
    4,152
    Likes Received:
    3
    Have you not looked at arrays yet? They are away of storing data in an ordered list and let you retrieve that data by specifying a position in that list. For example if you have a list of ten names stored in an array called $names, you could retrieve say the 5th name by using $names[5]; (I think, it's been a while since I did php). Of course if you didn't want the name associated with a number you could use a letter, which is the example linear gave. That way you can retrieve a name associated with the letter a by using $names['a']; (I think). If you have a variable which contains the letter you want to associate with the name you use $names[$letter];. Geddit? All you need to do then use linear's code above to create a list of what letter means which name and then you can use that one line of code to convert the letter a to a name. I recommend you do it this way rather than the switch case or massive if method. It will be much easier to read through.
     
  11. linear

    linear Minimodder

    Joined:
    5 Oct 2001
    Posts:
    4,393
    Likes Received:
    1
    Okay, linear's zeroth law of programming: "Get the data structures right and the code will write itself."

    This is a good example of the zeroth law. You need to select something based on a user input. You can do it in a number of ways, but an associative array makes use of a particular data structure to do it efficiently. You probably think of arrays as having a numeric index, but in PHP they can equally well have strings for the indices. That's what we used above. this is a powerful language construct, and PHP allows them to be nested as well, so you can really build complicated data structures.

    The only gotcha in this example is that you need to make sure that you handle the case where someone types a string where the third character is out of the bounds of what you expect. This is linear's first law: "never trust user input". So while the snippet I gave you is functional. it's not at all robust (although PHP won't do anything really horrible if you give it an out-of-bounds index, you need to watch out for this kind of assumption in general). So you should probably follow the $newfirstname = $newfirstnames[$thirdletter]; with a check to see that you got something. So that if someone enters a bizarro character in the third position, you have code to handle that case.
     

Share This Page