php help

Discussion in 'PHP' started by BAM78, Oct 17, 2008.

  1. #1
    I have a page that has

    url.php?n=


    If a person types in 1 2 3 4 5
    then I need it to echo out the word of the number they typed
     
    BAM78, Oct 17, 2008 IP
  2. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can you please explain a bit more
     
    mehdi, Oct 18, 2008 IP
  3. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    echo $_GET['n'];
     
    Synchronium, Oct 18, 2008 IP
  4. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    if (isset($_GET['n']))
        {
            for ($i=0;$i<=(strlen($_GET['n'])-1);$i++)
                {
                    $character = substr($_GET['n'], $i, 1);
                    if (strstr("01234567890",$character))
                        {
                            switch ($character)
                                {
                                    case 0:
                                    echo "zero";
                                    break;
                                    case 1:
                                    echo "one ";
                                    break;
                                    case 2:
                                    echo "two ";
                                    break;
                                    case 3:
                                    echo "three ";
                                    break;
                                    case 4:
                                    echo "four ";
                                    break;
                                    case 5:
                                    echo "five ";
                                    break;
                                    case 6:
                                    echo "six ";
                                    break;
                                    case 7:
                                    echo "seven ";
                                    break;
                                    case 8:
                                    echo "eight ";
                                    break;
                                    case 9:
                                    echo "nine ";
                                    break;
                                }
                        }
                }
        }
    ?>
    <FORM method="GET" action="">
        <input name="n" type="text" /> -
        <input name="submit" type="submit" value="Submit" />
    </FORM>
    
    PHP:
     
    Bind, Oct 18, 2008 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Yes, Synchronium is right, each variable of the query string (after ? mark) is accessible by GET request.

    For example if you have url.php?a=1&b=2&c=3&d=pofapdopfosd
    then you will have a value for $_GET['a'], $_GET['b'], $_GET['c'] and $_GET['d']

    Bind, doing something like
    
    $numbers=array('zero', 'one', 'two', 'three', ...)
    echo $numbers[$_GET['n']];
    
    PHP:
    would have been simpler, but your solution is good!
     
    webrickco, Oct 18, 2008 IP
  6. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    agreed, but still have to parse for each charactor - strlen()+substr() - unless only one number is allowed per form submit.

    here they are combined if anyone is interested:

    
    <?php
    $numname = Array("zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ");
    if (isset($_GET['n']))
        {
            for ($i=0;$i<=(strlen($_GET['n'])-1);$i++)
                {
                    $character = substr($_GET['n'], $i, 1);
                    if (strstr("01234567890",$character))
                        {
                            echo $numname[$character];
                        }
                }
        }
    ?>
    <FORM method="GET" action="">
        <input name="n" type="text" /> -
        <input name="submit" type="submit" value="Submit" />
    </FORM>
    
    PHP:
     
    Bind, Oct 18, 2008 IP