Need $i to display 1 instead of 0

Discussion in 'PHP' started by radagast, Nov 8, 2011.

  1. #1
    Hi


    I hope some can help me

    I have the following code:

    for($i=0; $i<count($height); $i++){
    echo "<tr></td><td><input type='text'name='item_no$i' id='item_no' size='1' value='$i' style='border: none' class='nice'/></td>

    Which displays:

    item.jpg

    I need to change the value='$i' so the it displays:

    Item No

    1

    2

    Any help or suggestions would be greatly appreciated.
     
    radagast, Nov 8, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Change:

    for($i=0; $i<count($height); $i++){

    to

    for($i=1; $i<=count($height); $i++){
     
    jestep, Nov 8, 2011 IP
  3. David Lemarier

    David Lemarier Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is what you should use:

     
    David Lemarier, Nov 8, 2011 IP
  4. radagast

    radagast Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am pulling the below from another page:

    for($i=0; $i<count($height); $i++)
    echo "<tr></td><td><input type='text'name='item_no$i' id='item_no' size='1' value='$i' style='border: none' class='nice'/></td>

    So when i do

    for($i=1; $i<=count($height); $i++)
    echo "<tr></td><td><input type='text'name='item_no$i' id='item_no' size='1' value='$i' style='border: none' class='nice'/></td>

    It will not pull through the row beginning with 0


    I am using the following script to add the rows:

    <script>
    function addRowToTable()
    {
    var tbl = document.getElementById('quote_details');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);

    // Parcel ID
    var parcel_id = row.insertCell(0);
    var textNode = document.createTextNode(iteration);
    parcel_id.appendChild(textNode);


    // Goods Discription
    var discription = row.insertCell(1);
    var sel = document.createElement('select');
    sel.name = 'selRow0[]' + iteration;
    sel.options[0] = new Option('Box', 'Box');
    sel.options[1] = new Option('Flyer', 'Flyer');
    sel.options[2] = new Option('Something big', 'Something big');
    discription.appendChild(sel);


    // Height
    var height = row.insertCell(2);
    var el1 = document.createElement('input');
    el1.type = 'text';
    el1.name = 'height[]' + iteration;
    el1.id = 'vol' + iteration;
    el1.size = 5;
    el1.setAttribute("onChange", "sum()");
    el1.setAttribute("onBlur", "sum()");
    el1.setAttribute("onFocus", "sum()");
    el1.setAttribute("onClick", "sum()");
    el1.setAttribute("onKeydown", "sum()");
    el1.setAttribute("onKeyup", "sum()");
    el1.setAttribute("class", "validate[required]");

    height.appendChild(el1);

    // Length
    var length = row.insertCell(3);
    var el2 = document.createElement('input');
    el2.type = 'text';
    el2.name = 'length[]' + iteration;
    el2.id = 'length' + iteration;
    el2.size = 5;
    el2.setAttribute("onChange", "sum()");
    el2.setAttribute("onBlur", "sum()");
    el2.setAttribute("onFocus", "sum()");
    el2.setAttribute("onClick", "sum()");
    el2.setAttribute("onKeydown", "sum()");
    el2.setAttribute("onKeyup", "sum()");
    el2.setAttribute("class", "validate[required]");


    length.appendChild(el2);

    // Width
    var width = row.insertCell(4);
    var el3 = document.createElement('input');
    el3.type = 'text';
    el3.name = 'width[]' + iteration;
    el3.id = 'width' + iteration;
    el3.size = 5;
    el3.setAttribute("onChange", "sum()");
    el3.setAttribute("onBlur", "sum()");
    el3.setAttribute("onFocus", "sum()");
    el3.setAttribute("onClick", "sum()");
    el3.setAttribute("onKeydown", "sum()");
    el3.setAttribute("onKeyup", "sum()");
    el3.setAttribute("class", "validate[required]");


    width.appendChild(el3);

    // Mass
    var mass = row.insertCell(5);
    var el4 = document.createElement('input');
    el4.type = 'text';
    el4.name = 'mass[]' + iteration;
    el4.id = 'mass' + iteration;
    el4.size = 5;
    el4.setAttribute("onChange", "sum()");
    el4.setAttribute("onBlur", "sum()");
    el4.setAttribute("onFocus", "sum()");
    el4.setAttribute("onClick", "sum()");
    el4.setAttribute("onKeydown", "sum()");
    el4.setAttribute("onKeyup", "sum()");
    el4.setAttribute("class", "validate[required]");



    mass.appendChild(el4);



    }


    function removeRowFromTable()
    {
    var tbl = document.getElementById('quote_details');
    var lastRow = tbl.rows.length;
    if (lastRow > 2) tbl.deleteRow(lastRow - 1);
    }




    </script>


    Thanks
     
    radagast, Nov 8, 2011 IP
  5. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #5
    The code below can be helpful

    1)
    for($i=0; $i<count($height); $i++){
    $no =$i+1;
    echo "<tr></td><td><input type='text'name='item_no$i' id='item_no' size='1' value='$no' style='border: none' class='nice'/></td>";
    }

    or

    2)
    $j = 1;
    for($i=0; $i<count($height); $i++){
    echo "<tr></td><td><input type='text'name='item_no$i' id='item_no' size='1' value='$j' style='border: none' class='nice'/></td>";
    $j++;
    }
     
    Last edited: Nov 9, 2011
    avinash gaud, Nov 9, 2011 IP
  6. radagast

    radagast Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi avinash gaud

    Thanks a stack option 1. works 100%

    So for the rest of the year YOU ARE THE MAN :cool:
     
    radagast, Nov 9, 2011 IP