/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] getting only values from more than one array

Hi all,

I am developing a cart where the users can select multiple products with their respective quantities.I need to post 3 array values at a time.They are:

a)Product ID(pid)—-Array pid[]
b)Quantity (quantity)——Array quantity[]
c)Item price(item_price)——-Array item_price[]

[code=html]//html code within form

<input type=”text” name=”quantity[]” value=”” size=”7″ onchange=”if (/D/.test (this.value)) {alert (‘The quantity would be numeric’); this.value=”; this.focus()}”>

<input type=”checkbox” name=”pid[]” value=”<?=$pid;?>”>

<input type=”hidden” name=”item_price[]” value=”<?=$withdisc.”#”.$pid;?>”>//here I am joining the pid with the item price to match for which product what item price has been posted[/code]

[code=php]//after the form is being posted
if(isset($_POST[‘image1_x’]))
{
$pid=$_POST[‘pid’];
$quantity=$_POST[‘quantity’];
$item_price=$_POST[‘item_price’];

for(
;
list($key1, $value1 ) = each( $pid ) ,
list($key2, $value2 ) = each( $quantity )
list($key3, $value3 ) = each( $item_price)
;
)
{
//output of echo “Key1: $key1; Value: $value1<br />n”;

Key1: 0; Value: 1475
Key1: 1; Value: 1476
Key: ; Value:
Key: ; Value:
echo “Key1: $key1; Value: $value1<br />n”;

//output of echo “Key2: $key2; Value: $value2<br />n”;

Key2: 0; Value:
Key2: 1; Value:
Key2: 2; Value: 2
Key2: 3; Value: 3

echo “Key2: $key2; Value: $value2<br />n”;

//output of echo “Key3: $key3; Value: $value3<br />n”;

Key3: 0; Value: 152.71#1473
Key3: 1; Value: 174.31#1474
Key3: 2; Value: 180.05#1475
Key3: 3; Value: 196.74#1476

echo “Key3: $key3; Value: $value3<br />n”;

//how i can get the array elements which has value only…not the null values also i need to match the $item_price’s pid with the $pid and if it is matched then i have to run a insert query.Also the item_price needs to be multiply with the quantity.I have done something but in that case sometimes I am getting pid but not getting item_price or net_total or sometimes I am getting item_price and net_total but not getting pid.

“insert into temp_cart(cart_id,pid,quantity,item_price,net_total) values(‘$cart_id’,’$value1′,’$value2′,’$value3′,’$net_total’)”;

}
}[/code]

Please suggest me how can i get all the values of the insert query.

Raj

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@JonaOct 04.2006 — [font=trebuchet ms]Try using foreach() instead of list().[/font]

[code=php]
foreach ($pid as $pidValue => $pidKey){
echo "Key #$pidKey: $pidValue<br>n";
}
[/code]
Copy linkTweet thisAlerts:
@raj_2006authorOct 05.2006 — Hi

Thanks for your message first.

I have tried using only foreach....in this case I am getting on the values not the null values.....

But here is also a problem as there are multiple arrays.My main objective is to add multiple products in cart.

I think the way i am heading is not possible......coz of only multiple arrays.as i cannot get the insert query values in this way:

[code=php]foreach ($pid as $pidValue => $pidKey)
{
foreach ($quantity as $quantityValue => $quantityKey)
{
echo "insert into temp_cart(cart_id,pid,quantity,item_price,net_total) values('$cart_id','$pidKey','$quantityKey','$v1','$net_total')";
}
} [/code]


the above is not possible.....what do you think?
Copy linkTweet thisAlerts:
@JonaOct 07.2006 — [font=trebuchet ms]Does this help?[/font]

[code=php]
<?php
$ary1 = array('one', 'two', 'three', 'four', 'five', 'six');
$ary2 = array('1', '2', '3', '4', '5');
$ary3 = array('a', 'b', 'c', 'd', 'e');

$len1 = count($ary1);
$len2 = count($ary2);
$len3 = count($ary3);
$ex = 0;
$max = max($len1,$len2,$len3);

while($ex < $max){

$ary1Val = isset($ary1[$ex])?$ary1[$ex]:'';
$ary2Val = isset($ary2[$ex])?$ary2[$ex]:'';
$ary3Val = isset($ary3[$ex])?$ary3[$ex]:'';

echo '<pre>'
.'Ary1: '.$ary1Val."n"
.'Ary2: '.$ary2Val."n"
.'Ary3: '.$ary3Val."nn"
.'</pre>';

$ex++;
}
?>[/code]
Copy linkTweet thisAlerts:
@raj_2006authorOct 08.2006 — hi

thanks for your mesage.Let me try it.Though i have done the multiple items entry in a different way.That is the customer needs to select the product first(post the product ids in another page) and then he can add quantity.

can you please tell me what does this line means actually?

$ary1Val = isset($ary1[$ex])?$ary1[$ex]:'';

also in some cases any one of the array values may be empty.
Copy linkTweet thisAlerts:
@JonaOct 10.2006 — [font=trebuchet ms]I realized that in some cases the array values may be empty, which is precisely why the line that you asked me to explain exists. It checks to ensure that the array value PHP is looking for exists, and if it is, returns the value of that array; otherwise, it returns an empty string. If you simply run the test code I posted, you’ll notice that the last group of output only has one value — $ary1 outputs “six” and the rest of the arrays output nothing. They output nothing because there is no array index which corresponds to the current $ex variable in those two arrays, but there is one that corresponds in $ary1, which is why a 6th group of output appeared.

[i]Note: I edited the script I posted. There was a small bug in it.[/i][/font]
Copy linkTweet thisAlerts:
@raj_2006authorOct 10.2006 — I JUST SAY ITS CLASSIC WONDERFUL EXPLANATION AND LOGIC SENSE

Thanks a lot again mate.Yes its working great and I have understood the flow.

Thank you sooomuch.......... ?
Copy linkTweet thisAlerts:
@JonaOct 10.2006 — [font=trebuchet ms]I&#8217;m happy to have helped, Raj. ? [/font]
×

Success!

Help @raj_2006 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...