Jump to content

while {} echo problems


dannybrazil

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 (->)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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);";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.