Generating a list with the while loop

Discussion in 'PHP' started by pr0xy122, Jun 2, 2012.

  1. #1
    Hi guys, im quite new to PHP so bare with me.

    I would like to create a "list generator" script. What I mean by that is I would like a script that does the following:

    Black desk
    Green desk
    yellow desk
    white desk

    ---
    I don't want to have type both words in, just input a list such as:black,green,yellow,white
    then have the script generate the above

    Hope that makes sense

    Thanks all!@!
     
    pr0xy122, Jun 2, 2012 IP
  2. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #2
    $input = "black,green,yellow,white";
    $items = explode(",", $input);
    
    function makeList($val)
    {
    	echo $val . " desk <br />";
    }
    
    array_walk($items, "makeList");
    PHP:
    I am open for any PayPal donations :D
     
    akhileshbc, Jun 2, 2012 IP
  3. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #3
    That seems like a non-conventional way to do this.

    
    $colors = array("Black", "Green", "Yellow", "White");
    
    foreach ($colors as $color) {
        print "$color Desk\n";
    }
    
    PHP:
     
    NetStar, Jun 3, 2012 IP