0

I am creating a form for my workplace that will enter product serial numbers into a mySQL database.

You can see current working example here: http://jsfiddle.net/bELWq/

My issue at the moment is processing this form, at the moment I have a php script that simply echoes all of the information that is entered:

<?php
    $supplier = $_POST['supplier'];
    $date_inv = $_POST['date-inv'];
    $date_rec = $_POST['date'];
    $user = $_POST['user'];
    $product = $_POST['product'];
    $qty = $_POST['qty'];
    $serial = $_POST['serial'];

    foreach ($product as $p) {
        echo "$p<br />";
}    

    foreach ($qty as $q) {
        echo "$q<br />";
}

    foreach ($serial as $s) {
        echo "$s<br />";
}

    echo $supplier;
    echo $date_inv;
    echo $user;
    echo $date_rec;
?>

But the problem is that it echoes every product then all the qty's then all the serial numbers with nothing really to differentiate what belongs where.

Now i have racked my brain continuously over how to do this, and as i am not the most logically minded person it is starting to make my brain hurt.

Any suggestions on how to process this are greatly appreciated. Or if you can think of a better method to send the information, that would be good also.

2 Answers 2

1

You could add the data to a table like so.

<?
echo '<table>
    <tr>
        <th>Product</th>
        <th>Quantity</th>
        <th>Serials</th>
    </tr>';

foreach ($product as $i => $p) {
    $serial_cell = array();
    foreach($serial[$i] as $si => $sv){
        $serial_cell[] =$sv;
    }
    echo '
    <tr>
        <td>'.$product[$i].'</td>
        <td>'.$qty[$i].'</td>
        <td>'.implode('<br>',$serial_cell).'</td>
    </tr>';
}
echo '
    <tr>
        <td>Supplier:</td>
        <td colspan="2">'.$supplier.'</td>
    </tr>
    <tr>
        <td>Invoice Date:</td>
        <td colspan="2">'.$date_inv.'</td>
    </tr>
    <tr>
        <td>User:</td>
        <td colspan="2">'.$user.'</td>
    </tr>
    <tr>
        <td>Date Received:</td>
        <td colspan="2">'.$date_rec.'</td>
    </tr>';
echo '</table>';
Sign up to request clarification or add additional context in comments.

3 Comments

This works great, but the serials doesn't seem to quite work. Now i have the serials as a multi-dimensional array. i.e. serial[0][1] what is the best way to process this?
I've edited the answer to allow for multiple serials in an array
Great! Works perfect...now i just need to work it into an SQL query, which shouldn't be too hard now i have a bit of organisation.
1

You could do something like this:

    foreach ($serial as $key => $s) {
    echo "$s<br />";
    echo $serial[$key];
    echo $qty[$key];
    }

3 Comments

Doesn't seem to be working, not sure about the syntax of the foreach function what does the => $s mean/represent? maybe if i tweak it a bit i can get it to work
What doesn't work? Can you post a example of the post array. About the foreach is the following every array is build like this: array(0 => 'value') by doing foreach ($serial as $key => $s) you the $key will 0 and $s will be the value for this example it will be value. Check also the manual : php.net/manual/en/control-structures.foreach.php
Ok, after a bit of diagnosing, it seems that my form does not post any extra serial numbers, assuming its because of the dynamic html. It doesn't seem to want to POST any new serial number fields, if i usae the original one it will POST but as soon as i add the extra ones, it does not POST the any of the serials.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.