Page 1 of 1

Arrays.

Posted: Tue Jan 24, 2006 12:38 pm
by ol4pr0
Ok .. i'm a bit out of the game. havent been doing anything for almost a year now i think.

Got the following problem.
i have a x amount of 2 textboxes next to eachother like ->

1:name="value[]" 2:name="value2[]"
1:name="value[]" 2:name="value2[]"
1:name="value[]" 2:name="value2[]"

now i'm trying to insert this into the database which goes fine except it isnt inserted like i want to.
all vars of value[] are being pasteed together and inserted as one. Same goes for value2[]

this is the code i'm using now.

Code: Select all

function return_($vars) {
	$var = "";
	foreach ($vars as $postvars) {
		$var .= $postvars;
	}
	return $var;
}
$items=array('reference'=>return_($_REQUEST['ref']),'qty'=>return_($_REQUEST['qty']));
$result = $dbc->insert("zapatillas", $items) or die( $dbc->getError() );
Thanks

Posted: Tue Jan 24, 2006 12:47 pm
by John Cartwright

Code: Select all

foreach ($vars as $postvars) {
        $var .= $postvars;
    }
This is basically merging the array into a string.. but you never said what was the desired effect?

Posted: Tue Jan 24, 2006 1:14 pm
by ol4pr0
The desired effect is that i can insert multipulrows in one query.
cuase i can have just one row of 3 textboxes or i can have 20 rows of 3 textboxes.


textboxes like this

--------- ------------- -------------
--------- ------------- -------------
--------- ------------- -------------

they all need to be inserted seperated. 3 inserts of 3 values.

Posted: Tue Jan 24, 2006 1:44 pm
by ol4pr0
Maby this will make it easyer.

First there is this form that asks howmany textboxes there are to be generated.

Code: Select all

<?php
if (isset($_POST['step']) && ($_POST['step']=='2')) {?>
<form action='' method='post'> 
<?php 
echo("<TABLE>"); 
echo( "\n<TR BGCOLOR=\"#808080\">"); 
echo("<TH>" . Codigo. "</TH>"); 
echo("<TH>" . Pares . "</TH>"); 
echo("<TH>" . Box . "</TH>"); 
echo("</TR>"); 
for($j = 1; $j <= $_POST['tbx']; $j++) {?>
<tr>
<td><input type="text" name="ref[]" size="8" /></td>
<td><input type="text" name="qty[]" size="6" /></td>
<td><input type="text" name="qtyBox[]" size="6" /></td>
</tr>
<?php
}
echo("</TABLE>");
?> 
<input type="hidden" name="step" value="2">
<input type="submit" value="Submit" name="Submit">
<input type="reset" value="Reset" name="B2">
</form>
<?php 
}exit;
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<input type="text" name="tbx">
<input type="hidden" name="step" value="2">
<input type="submit">
</form>
Asuming this amount is always bigger than 1 it needs to be inserted into a database.
foreach insert it should be like this INSERT INTO ????? VALUES (ref,qty,qtyBox) and that times the number of textboxes that has been generated.

So this array

Code: Select all

Array ( [0] => 4812.F3 [1] => 9020.M1 ) Array ( [0] => 24 [1] => 24 ) Array ( [0] => 1 [1] => 1 )
Should be inserted like this.

Code: Select all

VALUES (4812.F3,24,1)
VALUES (9020.M1,24,1)
Now how to do that ?

Hopefully this is a better to understand

Posted: Tue Jan 24, 2006 5:20 pm
by raghavan20

Code: Select all

$values = array();
for ($i = 0; $i < count($_POST["ref"]); $i++){
	$values[] =  "('{$_POST["ref"][$i]}', '{$_POST["qty"][$i]}', '{$_POST["qtyBox"][$i]}')";
}
$query = "insert into `table_name` values ".explode(",", $values);
mysql_query($query);

Posted: Wed Jan 25, 2006 8:06 am
by ol4pr0
Thanks.

Yesteryday night i finally figured it out.

I kinda did the same however it might be so that ure method is better than whatever i did. (please comment)

Code: Select all

for ($i=0;$i<$_POST['tbx'];$i++) {
	$items=array('reference'=>$_REQUEST['ref'][$i],'qty'=>$_REQUEST['qty'][$i],'qtyBox'=>$_REQUEST['qtyBox'][$i]);

Posted: Wed Jan 25, 2006 10:21 am
by Christopher
If you are going to insert them into a database I would recommend filtering them first:

Code: Select all

for ($i=0;$i<$_POST['tbx'];$i++) {
    $items['reference']= preg_replace('/[^a-zA-Z0-9]/', '', $_POST['ref'][$i]);
    $items['qtyBox'] = intval($_POST['qtyBox'][$i]);
    $items['qty'] = intval($_POST['qty'][$i]);
Hopefully your database class is also using the database specific escaping function before inserting.

Posted: Wed Jan 25, 2006 10:46 am
by Jenk
try:

Code: Select all

function clean ($string) {
    if (get_magic_quotes_gpc()) {
        $string = stripslashes($string);
    }
    return mysql_real_escape_string($string);
}

if ((count($_POST['reference']) == count($_POST['qty'])) && (count($_POST['reference']) == count($_POST['qtybox']))) {

    $count = count($_POST['reference']);
    $final = array();

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

        $final[] = "('" . clean($_POST['reference'][$i]) . "', '" . clean($_POST['qty'][$i]) . "', '" . clean($_POST['qtybox'][$i]) . "')";

    }

} else {

    echo 'Mismatch in values entered!';

}

foreach ($final as $values) {
    mysql_query("INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES {$values}");
}

Posted: Wed Jan 25, 2006 12:32 pm
by ol4pr0
Thanks guys.

Seen some interesting snippets in both codes. :)