[Solved] Array help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[Solved] Array help

Post 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./
Last edited by facets on Thu Feb 08, 2007 11:30 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Manually?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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.
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

exactly what data do you want stored in the array? deliveryType(), showProduct(), quantity()?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Where is your data coming from?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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;
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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());
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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./
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
Post Reply