dannybrazil Posted January 25, 2013 Share Posted January 25, 2013 Hello, I have a WHILE loop that works fine, BUT when I add this to it I get an error. I guess my way is NOT how you suppose to do it My regular while: $query_3=mysql_query("SELECT * FROM client_order WHERE Client_id = $cid AND order_number = $order_number "); while ($row_3=mysql_fetch_assoc($query_3)){ echo 'smt....smt'; } I need to add: $paymentRequest->addItem('0001', 'Notebook prata', 2,430.00); // --- 0001 is increasing if I have more than 1 product 0002...000n The new one: $query_3=mysql_query("SELECT * FROM client_order WHERE Client_id = $cid AND order_number = $order_number "); while ($row_3=mysql_fetch_assoc($query_3)){ echo '$paymentRequest->addItem("'.$start_number.'", "'.$productName.'", '.$price.')'; $start_number++; } How to I write the code in a correct way that if I have lets say 3 products is will "print" 3 parts like this: $paymentRequest->addItem('1', 'Notebook prata', 2,430.00); $paymentRequest->addItem('2', 'Notebook red', 2,430.00); $paymentRequest->addItem('3', 'Notebook yello', 2,430.00); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/ Share on other sites More sharing options...
requinix Posted January 25, 2013 Share Posted January 25, 2013 You have to actually set the values of $productName and $price. Presumably from the $row_3 array. And if the error is not about those two variables being undefined, what is it? Also, try to switch to the mysqli or PDO extensions. They're better than the old mysql you're using now. Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408239 Share on other sites More sharing options...
dannybrazil Posted January 25, 2013 Author Share Posted January 25, 2013 I t is getting $productName and $price from $row_3. But it seems when I echo it , something goes wrong regarding this command (which is NOT mine I just copies it to use a shopping cart) $query_3=mysql_query("SELECT * FROM client_order WHERE Client_id = $cid AND order_number = $order_number ");[/font][/color] [color=#282828][font=helvetica, arial, sans-serif]while ($row_3=mysql_fetch_assoc($query_3)){[/font][/color] [color=#282828][font=helvetica, arial, sans-serif]$productName = $row_3['name']; $price = $row_3['price'];[/font][/color] [color=#282828][font=helvetica, arial, sans-serif]echo '$paymentRequest->addItem("'.$start_number.'", "'.$productName.'", '.$price.')';[/font][/color] [color=#282828][font=helvetica, arial, sans-serif]$start_number++; } I guess it has to be something with the echo or with the quotations or something. btw...I was trying to look for this online but couldn't : -> : what this mean...where can I find the manual for this operator (->) Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408241 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2013 Share Posted January 25, 2013 The three ->addItem() parameters are the actual values/variables holding the values. You are trying to make them into a string that looks like how it would get published in a book. $paymentRequest->addItem($start_number,$productName,$price); Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408243 Share on other sites More sharing options...
requinix Posted January 25, 2013 Share Posted January 25, 2013 How about an explanation of what's going wrong? So we don't have to keep guessing. -> is colloquially known as the "arrow operator" or officially the "object operator". On the left is an object and on the right is a property (variable) or method (function). Take a look at the manual for an overview. There's also :: "double colon" or "paamayim nekudotayim" for static properties and methods: instead of an object on the left it's the name of a class (or a keyword like self, parent, or static). Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408245 Share on other sites More sharing options...
dannybrazil Posted January 25, 2013 Author Share Posted January 25, 2013 This is the command (again it is NOT mine I just copied it) /** * @return the items/products list in this payment request */ public function getItems() { return $this->items; } /** * Sets the items/products list in this payment request * @param array $items */ public function setItems(Array $items) { if (is_array($items)) { $i = Array(); foreach ($items as $key => $item) { if ($item instanceof PagSeguroItem) { $i[$key] = $item; } else if (is_array($item)) { $i[$key] = new PagSeguroItem($item); } } $this->items = $i; } } /** * Adds a new product/item in this payment request * * @param String $id * @param String $description * @param String $quantity * @param String $amount * @param String $weight * @param String $shippingCost */ public function addItem($id, $description = null, $quantity = null, $amount = null, $weight = null, $shippingCost = null) { $param = $id; if ($this->items == null) { $this->items = Array(); } if (is_array($param)) { array_push($this->items, new PagSeguroItem($param)); } else if ($param instanceof PagSeguroItem) { array_push($this->items, $param); } else { $item = new PagSeguroItem(); $item->setId($param); $item->setDescription($description); $item->setQuantity($quantity); $item->setAmount($amount); $item->setWeight($weight); $item->setShippingCost($shippingCost); array_push($this->items, $item); } } example they gave - as it is // Add another item for this payment request $paymentRequest->addItem('0002', 'Notebook rosa', 2,560.00); what I want to do it getting the variables from my DB and with a WHILE loop adding the products. hope it is easier to understand now. the thing is that when I am in the WHILE loop it seems that the ($id) 0001...0002...000n is not being passed correctly in the while loop cus I am getting an error. when I just try to do it manually it is working so I guess something is wrong with the ECHO in the WHILE loop/ Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408247 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2013 Share Posted January 25, 2013 Someone posted above how to properly call the addItem() method with the three parameters. If you want to literally echo what the resulting php statement is, instead of executing it, you would enclose the statement in double-quotes (so that the three parameters get replaced with their value) and escape the first $ (so that the object in $paymentRequest is not replaced with its actual value) - echo "\$paymentRequest->addItem($start_number,$productName,$price);"; Quote Link to comment https://forums.phpfreaks.com/topic/273640-while-echo-problems/#findComment-1408252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.