SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    for loop did not work

    Hi..

    I am new in using for loop to get the values from while loop:

    here is my code:

    PHP Code:
    $sql "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items";
    $res_bom mysql_query($sql$con);

    while(
    $row mysql_fetch_assoc($res_bom)){
        
        
    $Items $row['Items'];
    echo 
    "<tr>
            <td style='border: none;font-weight: bold;'>&nbsp;<input type='name' value='
    $Items' name='Items[]' id='Items' readonly = 'readonly' style = 'border:none;width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size='5'></td>
            <td style='border:none;'>&nbsp;</td>
            <td style='border:none;'>&nbsp;</td> 
            <td style='border: none;'><center><input type='text' name='DemandedQty' id='DemandedQty[]' value='' size='7'></center></td>
            
            </tr>"
    ;       

    sample:

    Items -- I input demanded qty
    P28 -----1
    P30------2
    P32------3

    when I debug my script by checking if what data was get bhy using for loop:

    PHP Code:
    $Items $_POST['Items']; 
    $DemandedQty $_POST['DemandedQty'];  
    for(
    $s 0$s <= count($Items); $s++){
        
    echo 
    $DemandedQty[$s];
    echo 
    '<br/>';
    echo 
    $Items[$s];
                        

    the ouput is:
    3
    P28
    P30
    P32

    I don't know why only the last demanded qty was get.

    I want output is :
    P28- 1
    P30- 2
    P32 - 3

    Thank you

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your id and name attribute values for DemandQty are back to front, the [] should be part of the name attribute and not the id attribute.

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your suggestion.

    Now, I encountered error:

    Column count doesn't match value count at row 1

    but when I count my row it was match:

    Code:
    for($i = 0; $i < count($Items1); $i++) 
    { 
    
        if ( $DemandedQty[$i] != "" )
        {
        $sql = "INSERT INTO stock_requisition 
        (sr_date, sr_number, Items, SubItems, ItemCode, SubQty, UoM, Class, Description, BINLocation) 
        VALUES 
        ('$sr_date', '$sr_number', '$Items1[$i]', '$SubItems[$i]',  '$ItemCode[$i]', '$SubQty[$i]' '$UoM[$i]', '$Class[$i]', '$Description[$i]', 
        '$BINLocation[$i]') 
        ";   
        $result = mysql_query($sql, $con) or die(mysql_error()); 
        
        
        }      
    }
    Thank you

  4. #4
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found my error: i forgot ',' between $SubQty[$i] and $UoM[$i]

    Thank you


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
  •