Skip to content

Instantly share code, notes, and snippets.

@rsmarshall
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save rsmarshall/24b52155d162d6c781cf to your computer and use it in GitHub Desktop.

Select an option

Save rsmarshall/24b52155d162d6c781cf to your computer and use it in GitHub Desktop.
/**
* Add an item submitted via the app upload
*
* @param $packId
* @param $quantity
* @param $price
* @param $trolleyId
*/
public function addFromApp($packId, $quantity, $price, $trolleyId)
{
DB::statement("INSERT INTO trolley_items ( `trolley_id`,`pack_id`,`price`,`quantity` )
VALUES ( ?, ?, ?, ? ) ON DUPLICATE KEY UPDATE `quantity` = `quantity` + 1",
array($trolleyId, $packId, $price, $quantity));
}
@helium3bp

Copy link
Copy Markdown

public function addFromApp(array $items)
{

    // the stupid way but it sure does work
    $dataArray = [];
    $dataString = "";
    /*
     * $items = [
     * [1,100,200,123],
     * [2,150,210,206],
     * [id,quantity,price,trolleyId]
     * ]
     */
    foreach ($items as $item) {
        $dataString .= "( ?, ?, ?, ? ),";
        foreach ($item as $value) {
            $dataArray[] = $value;
        }
    }
    $dataString = rtrim($dataString,",");
    DB::statement("INSERT INTO trolley_items ( `trolley_id`,`pack_id`,`price`,`quantity` )
        VALUES
              $dataString
        ON DUPLICATE KEY UPDATE `quantity` = `quantity` + 1", $dataArray);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment