How to echo just one time a link in a for loop - new to php

Hi guys,

this is the code I want to change:

[php]
$li = ‘

  • ’;
    for ($i=1; $i<=10; $i++) {
    if (isset($_GET[‘add’]) && ($_GET[‘add’] == ‘’.$i.‘ingredients’)) {
    $repeat = str_repeat($li, $i);
    echo $repeat;
    }
    echo " Add ingredient ";
    }
    [/php]

    So, I am trying to create a form where the user adds a recipe. I have input for every ingredient. Under the inputs should be a link that says: “Add ingredient” - and a new input should appear. The problem in my script is that the link is shown $i times, because, off course, is in a loop.
    How could I echo the link just one time, with the last $i value… I tried echo-ing the link out of the loop, but it doesn’t work like that.

    I’m new to php and programming generally… ???

    Thank you in advance. Hope I was clear with my problem.

  • I think you should rethink your form fields. This looks highly inefficient.

    Use arraysname="ingredient[0]"
    is acceptable in php forms, and you will find a lot of your editing of form fields, lookups, loops a lot simpler

    Sponsor our Newsletter | Privacy Policy | Terms of Service