SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Incremement Value in Foreach Loop

    Hi all, I'm trying to echo a field in a foreach loop and make the field name increment each time so I would have fields 'name1', 'name2', etc. Tried having a go but can't seem to make it work. Think I'm pretty close but I must be doing something not quite correct.

    PHP Code:
    while ($row mysql_fetch_array($search)){
                        echo 
    '<div class="searchRes">
                        <span class="searchRow" style="width: 20px;">'
    .$row['customer_ref'].'</span>
                        <span class="searchRow" style="width: 65px;">'
    .$row['c_title'].' '.$row['c_firstname'].' '.$row['c_lastname'].'</span>
                        <span class="searchAddress" style="width: 100px;">'
    .str_replace(",",",<br/>"$row['c_address']).'</span>
                        <span class="searchRow" style="width: 70px;">'
    .$row['c_number'].'</span>
                        <span class="searchRow" style="width: 115px;">'
    .$row['c_email'].'</span>
                        <span class="searchRow" style="width: 60px;">'
    .$row['c_dob'].'</span>
                        <span class="searchRow" style="width: 100px;">'
    .$row['c_regdate'].'</span>
                        <span class="searchRow" style="width: 70px;">'
    .$row['c_notes'].'</span>
                        <span class="searchRow" style="width: 700px; margin-top: 10px;">
                        <span class="searchRow"><a href="createorder.php?id='
    .$row['customer_ref'].'">Add Order</a></span>
                        <span class="searchRow"><a href="editcust.php?id='
    .$row['customer_ref'].'">Edit Customer</a></span>
                        <span class="searchRow"><a href="deletecust.php?id='
    .$row['customer_ref'].'">Delete Customer</a></span>
                        <span class="searchRow"><a href="orderhistory.php?id='
    .$row['customer_ref'].'">View Order History</a></span>
                        </span>
                        </div>'
    ;
                        
                        foreach (
    range(0$row['customer_ref']) as $key) {
                        echo 
    '<input type="hidden" value="" name=""><input type=\'submit\' name=\'delete'.$key.'\' onclick="return confirm(\'Do you want to make these changes ?\');"></span>';
                        }
                    } 

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What output are you getting with the current code? What does $row['customer_ref'] contain?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, sorry, think I've scored a hatric for posts today! $row['customer_ref'] contains the customers id number such 1, 2, 3, 4, etc. Don't think I need to use this? I've tweaked it slightly and it's now echoing the right amount of fields (one) for each record instead of 50 or so when using the $row.

    Trouble now is, each fields name is delete1. Instead of incrementing, but guessing this is because of the while loop the foreach is contained in. So all in all, I'm guess I'm snookered. Apologies, this must sounds really confusing!

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    What about this?

    PHP Code:
    foreach (range(0$row['customer_ref']) as $key => $value) {
    ... 
    $key will now increment by one every loop.

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    What about this?

    PHP Code:
    foreach (range(0$row['customer_ref']) as $key => $value) {
    ... 
    $key will now increment by one every loop.
    Hi Immerse, that's perfect, exactly what I was looking for. Thanks for your time, greatly appreciated as always dude


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •