Forums | MacLife
You are not logged in.
#1 2006-11-10 10:05 am
- reefdog
- Manly man
- Registered: 2000-05-15
- Posts: 10701
PHP stumper. Except for smart people.
Hi. I'm new here.
Let's pretend that this is the code I'm working with (when in fact, I've stripped out all meaning to get to the root of the problem):
Code:
for ($i=0;$i<$somecount;$i++) {
$item=$i;
$a->add_field('x_line_item', $item);
}The objective is to create as many copies of "$a->add_field('x_line_item', $item);" as necessary. But of course, the for loop is simply overwriting each previous version, and only the last loop is saved.
How do I effectively echo each evaluated copy of "$a->add_field('x_line_item', $item);" back into the code, so that each iteration gets processed? Clearly "echo" won't work, right?
If you really want the in-context version, here y'go. It's based on this PHP Authorize.net class.
Code:
require_once('authorizenet.class.php');
$a = new authorizenet_class;
/* more stuff */
$result_cart=db_fetch_cart_by_string("target_id='$tid' AND session_id='".session_id()."' AND status='O'");
if ($result_cart!==false) {
for ($i=0;$i<sizeof($result_cart);$i++) {
$item=$result_cart[$i]->product_id."<|>".$result_cart[$i]->product_number."<|>".$result_cart[$i]->product_name."<|>".$result_cart[$i]->quantity."<|>".$result_cart[$i]->price."<|>Y";
$a->add_field('x_line_item', $item);
}
}
/* more stuff */
$a->process()If I can give any more context, do let me know.
Offline
#2 2006-11-10 1:55 pm
- reefdog
- Manly man
- Registered: 2000-05-15
- Posts: 10701
Re: PHP stumper. Except for smart people.
Never mind, you useless monkeys. We figured it out. Turns out it was a design flaw in that class I linked to. For the curious: it was storing each field name as an array key, so naturally feeding it multiple "x_line_item" key/value pairs was confusing it, despite the fact that duplicate "x_line_item" fields are acceptable (and expected) by the Authorize.net API. We got around it by appending an iterating number to the end of each x_line_item field name and then modifying the class to remove it during the curl.
Code:
for ($i=0;$i<sizeof($result_cart);$i++) {
$item=$result_cart[$i]->product_id."<|>".$result_cart[$i]->product_number."<|>".$result_cart[$i]->product_name."<|>".$result_cart[$i]->quantity."<|>".$result_cart[$i]->price."<|>Y";
$field="x_line_item-$i";
$a->add_field($field, $item);
}And the modified authorizenet.class.php:
Code:
$crap=array("-","0","1","2","3","4","5","6","7","8","9");
foreach( $this->fields as $key => $value )
$this->field_string .= "".str_replace($crap,"",$key)."=" . urlencode( $value ) . "&";Whee.
Offline
