#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2

    Using the same foreach multiple times?


    Hi.

    I'm using a foreach to load the name en contents of a certain product.
    In my navigation it has to load the name, further down we need to load the contents but in a slider.. but this is not working?

    PHP Code:
    <ul class="nav nav-tabs">
        <li class="active"><a href="#content1" data-toggle="tab">Overzicht</a></li>
        <?php foreach($projects as $project) : ?>
            <li><a href="tab<?= $project['id'?>" data-toggle="tab"><?= $project['name'?></a></li>
        <?php endforeach ?>
    </ul>

    <div id="tabContent" class="tab-content">
        <div class="tab-pane fade in active" id="content1">
            Content 1 tab
        </div>
        <?php foreach($projects as $project) : ?>
        <div class="tab-pane fade" id="tab<?= $project['id'?>">
            test
        </div>
        <?php endforeach ?>
    </div>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by notflip
    but this is not working?
    Have you tried turning it off and on again?

    C'mon, after half a year and 135 posts, you should know that "doesn't work" tells us nothing. We need a concrete error description, and we need some basic info. What is $projects? An array, a mysqli_result, a PDOStatement, a custom iterator, ...?

    Not every iterator can be repeated. When you've fetched rows from a database, you cannot go back and loop through them a second time.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Originally Posted by Jacques1
    Have you tried turning it off and on again?

    C'mon, after half a year and 135 posts, you should know that "doesn't work" tells us nothing. We need a concrete error description, and we need some basic info. What is $projects? An array, a mysqli_result, a PDOStatement, a custom iterator, ...?

    Not every iterator can be repeated. When you've fetched rows from a database, you cannot go back and loop through them a second time.
    The second time i use the iterator it doesn't print anything in the page source. The $projects is a PDO query.. so i use foreach to loop over the query. it's a simple

    SELECT name FROM projects

    So i can't use this loop 2 times.. any idea's how to accomplish this then? thank you
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by notflip
    The second time i use the iterator it doesn't print anything in the page source. The $projects is a PDO query..
    OK.



    Originally Posted by notflip
    So i can't use this loop 2 times.. any idea's how to accomplish this then? thank you
    If the number of projects isn't too big, simply use fetchAll() to save the rows in an array and then loop through this array.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    2
    Working perfectly! Thanks..

    So when do you use the fetchAll() ? Only if you need to have the values re-used or to work with them as you would with an array ( thanks by the way )
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by notflip
    So when do you use the fetchAll() ? Only if you need to have the values re-used or to work with them as you would with an array ( thanks by the way )
    You use fetchAll() whenever you want to have the rows in a "physical" array rather than just loop through them once.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo