Thread: Foreach

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Foreach


    Hey there.

    I am working on a website based on wordpress, and i want to generate a table automatically wich will be having a 3 columns width. I want to use a foreach because it uses an external file to serve as "theme" for the content i want within each cell... and i have to do a cicle that, each 3 "<td>" will make a "<tr>"
    In c# i would use
    foreach(int i=0;i<3;i++)
    {
    "code"
    }
    but in php it doesn't work. How can i do this?
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Sep 2003
    Location
    Germany
    Posts
    1,355
    Rep Power
    323
    http://www.php.net/manual/en/control-structures.foreach.php ?

    Comments on this post

    • GuitarBoyPTG agrees : Helpfull if you know a bit about php programming, as i am a total noob, didn't help much
    IkoTikashi - ikotikashi.de
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    The problem


    Thanks for the reply, but that didn't help me much.

    The problem is: i read that and got nothing of it. I'm really a noob at programming, and i never did web programming before. i don't really know how to do this. Also i see no similarities between php and c#(i know that that is normal)...
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    What you need is actually a for loop, not a foreach loop. A foreach loop is only for iterating over something like an array. I'm pretty sure that's true of C# too.

    A for loop in PHP has the same syntax as a for loop in C#, however variables in PHP are different than variables in C#. First, they are always prefixed with a $ sign, and second they do not have types.

    PHP Code:
    foreach($i 0$i 3$i++)
    {
    // "code"

    The thing is, that's a pretty fundamental concept of PHP, so it's kind of hard to understand how you'll be able to write "code" without having understood at least that much about variables. I think you should find a good introduction to PHP tutorial and work through it first so that you can learn those types of things.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo