Arrays.

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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Arrays.

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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);
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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]);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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}");
}
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Thanks guys.

Seen some interesting snippets in both codes. :)
Post Reply