Click to See Complete Forum and Search --> : [Resolved] Price Comparison
naruponk
Oct 10th, 2006, 09:00 AM
Hi, there.
I have a page that show price and productid, also checkbox to choose to compare. how can i send parameter to another php page as array?
any better way?
any one has any example of price comparison (PHP) ?
thanks for advance
kows
Oct 10th, 2006, 05:39 PM
how are you getting the information? database? text files? static information from -you-? I'm not sure what you mean by price comparison.. do you just want to compare two items and see which costs more? or what do you want to do? please give more detail.
you can send information to another page simply by setting up a redirect (or link, if you wish, or even a form if you want) that uses GET to pass your array information. "GET" information is stored in the query string of a URL, for example if your script was named "index.php" and someone called "index.php?name=kows," then the query string would be "name=kows." To use this information in PHP, you can simply call "$_GET['name']" and the value of that variable would be "kows." if you're using a form instead of a link or redirect, it might be better to use POST, that way the end user cannot just change the URL to change the information gathered (which might be something you don't want them to be able to do). POST information is gathered basically the same way that GET is, except it isn't stored in the query string, but rather is just submitted to the script and is not directly viewable by the end user. you can call POST variables using the same syntax as GET though, $_POST['name'] would produce "kows" if you submitted "name" with a value of "kows."
hope that makes sense @_@
naruponk
Oct 12th, 2006, 12:25 AM
Can I send information as an array?
kows
Oct 12th, 2006, 01:02 AM
yes, I believe you could just do something like this:
(filename is "form_array.php" if you want to run it by itself)
<?php
if($_SERVER['REQUEST_METHOD'] === "POST"){
//submitted
//print the $_POST array
echo '<pre>';
print_r($_POST);
echo '</pre>';
//now, resave our array
$myarray = array();
for($i = 0; $i < count($_POST); $i++){
if(isset($_POST['arrayitem_' . $i])){
$myarray[$i] = $_POST['arrayitem_' . $i];
}else{
//array ended, break out of the for loop
break;
}
}
//now, print our new array, it will print out the same result as the $_POST print out
//thus, we've recreated our array.
echo '<pre>';
print_r($myarray);
echo '</pre>';
exit;
}
//not submitted
echo '<form method="post" action="form_array.php">' . "\n";
$myarray = array("item1", "item2", "item3", "item4");
for($i = 0; $i < count($myarray); $i++){
echo '<input type="hidden" name="arrayitem_' . $i . '" value="' . $myarray[$i] . '" />' . "\n";
}
echo '<input type="submit" value="pass array" />' . "\n";
?>
Now, while I was making this, I remembered that you could just build the array in the form and have it automatically send itself through the form.. I just decided to include the above example anyway! So, here is how you can do that as well, which is probably the better/easier thing to do:
(this file should be form_array2.php if you want to run it by itself)
<?php
if($_SERVER['REQUEST_METHOD'] === "POST"){
echo '<pre>';
print_r($_POST);
echo '</pre>';
//you can now reference your old array using the following:
// $_POST['arrayitem'][i] where i is the number you want to reference
//ie: let's print out all of the items in the array
for($i = 0; $i < count($_POST['arrayitem']); $i++){
echo $i . ': ' . $_POST['arrayitem'][$i] . '<br>';
}
exit;
}
?>
<form method="post" action="form_array2.php">
<input type="hidden" name="arrayitem[]" value="item1">
<input type="hidden" name="arrayitem[]" value="item2">
<input type="hidden" name="arrayitem[]" value="item3">
<input type="hidden" name="arrayitem[]" value="item4">
<input type="hidden" name="arrayitem[]" value="item5">
<input type="submit" value="submit">
</form>
hope you can make sense of it.
both scripts run fine, if you wanted to test them to see how they worked.
naruponk
Oct 12th, 2006, 02:19 AM
Thanks a lot man, this is what I want :wave:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.