Page 1 of 1

[Solved] Array help

Posted: Fri Feb 02, 2007 8:05 am
by facets
Hi,

How could I send the following results data into an array?
I have been able to find a way to do this.

Code: Select all

function loadNextDropDown() {
        echo "<tr>";
        echo "<td width=100px valign=top>".deliveryTypes()."</td>";
        echo "<td width=150px>".showProduct()."</td>";
        echo "<td width=75px valign=top>".quantity()."</td>";
        echo "</tr>";
}
ta. Will./

Posted: Fri Feb 02, 2007 8:48 am
by superdezign
Manually?

Posted: Fri Feb 02, 2007 8:56 am
by facets
that should read I haven't been able to do this.
I believe I need to get this into an array so I can then send that data to my DB.
There maybe 1 - 10 iterations of this array.

Posted: Fri Feb 02, 2007 9:14 am
by boo_lolly
exactly what data do you want stored in the array? deliveryType(), showProduct(), quantity()?

Posted: Fri Feb 02, 2007 10:57 am
by RobertGonzalez
Where is your data coming from?

Posted: Fri Feb 02, 2007 5:35 pm
by facets
thanks for taking the time to look into this. (requested code is below)
I really only know how to explain the business logic so I hope you don't mind that..

The user fills out the initial row. Then clicks the add link and a new row appears, and so on.
Now once it's submitted I need to go through each of these rows and insert into mysql. I can do one, I just don't know how to set up this loop.
I do know that each rows needs a unique identifier for a loop to iterate through it.

Any suggestions?

Code: Select all

function showProduct() {
    $sql_query = mysql_query("SELECT id, prod_name from product ORDER BY prod_name ASC");
    $output = "<select name=\"productID\">\n";
    $output .= "<option value=\"\">-- Please Select --</option>\n"; 
        while(list($id, $prod_name)=mysql_fetch_array($sql_query)) {
        $prod_name = stripslashes($prod_name);
        
        if($id==@$productID){
                $output .= "<option selected value=\"$id\">$prod_name</option>\n";
        } else {
                $output .= "<option value=\"$id\">$prod_name</option>\n";
        }
    }
    $output .= "</select>";
    mysql_free_result($sql_query);		
return $output;
}               
                
function quantity() {
        $output = "<select name=\"quantityID\">\n";
        for($qty = 0; $qty < 20; $qty++) { 
                $output .= "<option value=\"$qty\">$qty</option>\n"; 
        }
        $output .= "</select>";
return $output;
} 

function deliveryTypes() {
        $output = "<select name=\"driverType\">\n";
        $output .= "<option value=\"delivery\">Delivery</option>\n"; 
        $output .= "<option value=\"returns\">Return</option>\n";
        $output .= "</select>";
return $output;
}

Posted: Fri Feb 02, 2007 6:31 pm
by RobertGonzalez
Looking into your code, this is going to not yield what you think it might...

Code: Select all

<?php
function showProduct() {
    $sql_query = mysql_query("SELECT id, prod_name from product ORDER BY prod_name ASC");
    $output = "<select name=\"productID\">\n";
    $output .= "<option value=\"\">-- Please Select --</option>\n";
        while(list($id, $prod_name)=mysql_fetch_array($sql_query)) {
        $prod_name = stripslashes($prod_name);
       
        if($id==@$productID){
                $output .= "<option selected value=\"$id\">$prod_name</option>\n";
        } else {
                $output .= "<option value=\"$id\">$prod_name</option>\n";
        }
    }
    $output .= "</select>";
    mysql_free_result($sql_query);           
return $output;
}
?>
That is because the $ProductID variable is completely nonexistent in the scope of the function. Of course, you are not seeing that issue with that because the error suppression operator (@) is quieting the output of the warning for undefined/uninitialized variable. Now, on to your logic...

I assume that the form you are talking, in which a client adds rows, is handling the row adds through javascript. Is that correct? If it is, what you need to do is set each field in each row to an array (name="fieldname[]") instead of a string (name="fieldname"). Then you can loop through each of the array members to handle your inputs once the form is submitted for processing.

Posted: Fri Feb 02, 2007 9:49 pm
by facets
Thanks for info about the "error suppression operator". I wasn't aware of that.

To simplify things if statically limit data entry to 10 rows how could I insert the data into the db consecutively?
This code works but only for the last row.

Code: Select all

$sql_add_invoice_items = "INSERT INTO invoice_items VALUES('','$prod_name', '$quantityID'";              
        mysql_query($sql_add_invoice_items) or die(mysql_error());
Is some kind of row identifier is required?
Or perhaps each object/field requires the array[] number?

So the above code would become :

Code: Select all

$sql_add_invoice_items = "INSERT INTO invoice_items VALUES('','$prod_name[1]', '$quantityID[1]')";              
        mysql_query($sql_add_invoice_items) or die(mysql_error());

Posted: Sat Feb 03, 2007 9:40 am
by RobertGonzalez
You'd loop the array and at each iteration you'd process the SQL (unless your server version supports transactions, then you could just throw one big query at the database and rollback if there is an error).

Posted: Mon Feb 05, 2007 4:26 am
by facets
Hey All,
I'm attempting to setup an array to handle creating the following.

Code: Select all

$prod_name0 = mysql_real_escape_string($_POST['prod_name']);
$prod_name1 = mysql_real_escape_string($_POST['prod_name']);
Can anyone see why this would not be working?

Code: Select all

$variableArrayPostName = array('prod_name', 'quantityID','price', 'gst', 'wholesale_price', 'wholesale_gst');
$variableArrayPostNameStatic = array('prod_name', 'quantityID','price', 'gst', 'wholesale_price', 'wholesale_gst');
$variableArrayPostEntry = array(0,1);

for($y = 0; $y < count($variableArrayPostEntry); $y++) {       
        for($x = 0; $x<count($variableArrayPostName); $x++) {
                $$variableArrayPostName[$x];$variableArrayPostEntry[$y] = 
                mysql_real_escape_string($_POST[$variableArrayPostNameStatic[$x]]);
        }
}
Will./

Posted: Mon Feb 05, 2007 6:14 am
by CoderGoblin
Could you use something like this ?

Code: Select all

$temp=array('prod_name', 'quantityID','price', 'gst', 'wholesale_price', 'wholesale_gst');
$results=array();
foreach ($temp as $value)
   $results[$value.'0']=mysql_real_escape_string($_POST[$value]);
   $results[$value.'1']=mysql_real_escape_string($_POST[$value]);
}
extract($results);

Posted: Mon Feb 05, 2007 10:25 am
by RobertGonzalez
Each row of the form would need to have a counter. This makes it simpler in your loop. As you add a row to the form, add 1 to the row counter, so in your form you'd have fields similar to:

(row 1)

Code: Select all

<input type="hidden" name="rowcount[1]" value="1" />
<select name="productId[1]">
<!-- ENTER YOUR OPTIONS -->
</select>
<select name="quantityId[1]">
<!-- ENTER OPTIONS HERE -->
</select>
(row 2)

Code: Select all

<input type="hidden" name="rowcount[2]" value="2" />
<select name="productId[2]">
<!-- ENTER YOUR OPTIONS -->
</select>
<select name="quantityId[2]">
<!-- ENTER OPTIONS HERE -->
</select>
(row 3)

Code: Select all

<input type="hidden" name="rowcount[3]" value="3" />
<select name="productId[3]">
<!-- ENTER YOUR OPTIONS -->
</select>
<select name="quantityId[3]">
<!-- ENTER OPTIONS HERE -->
</select>
Then in your code, you can loop through the rowcount array and handle the SQL from there:

Code: Select all

<?php
$count = count($_POST['rowcount']);
$prods = $_POST['productId'];
$quan = $_POST['quantityId'];

for ($i = 0; $i < $count; $i++)
{
    $sql = "INSERT INTO `invoice_items` VALUES('','{$prods[$i]}', '{$quan[$i]}'";
    // yadda yadda
}
?>
Please note: this code is only an example. It will need to be reworked, but it might be the logic you are looking for, or at least along the same lines.

Posted: Thu Feb 08, 2007 10:59 pm
by facets
Thank You Everah for your assistance and to everyone else also.
The last reply was exactly was I was trying to do.

Now for some validation fun!

Will.

Posted: Fri Feb 09, 2007 10:37 am
by RobertGonzalez
Glad I could help.