Reputation: 1141
The user want to buy lets'say 30 items, is there other way than make the for loop?
how can i make 30 inserts
public function buy($item_id)
{
$price = $this->input->post('price');
$amount = $this->input->post('amount');
$sum = $this->input->post('sum');
$price = intval($price);
$amount = intval($amount);
$sum = intval($sum);
if(!is_numeric($price) || !is_numeric($amount) || !is_numeric($sum))
{
exit("Неверные значения");
return false;
}
if(!empty($_SESSION['id']))
{
$user_id = $_SESSION['id'];
}
else
{
echo 'not logged';
exit();
}
$q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id);
$t = $this->db->query("SELECT * FROM items WHERE id = ".$item_id." AND
TO_DAYS(end) - TO_DAYS(now()) >= 0");
if($this->db->affected_rows() != 0)
{
$user = $q->result_array();
$item = $t->result_array();
$active = $user[0]['active'];
$user_id = $user[0]['id'];
$money = $user[0]['money'];
$price = $item[0]['price'];
$bought_times = $item[0]['bought_times'];
$coupons = $item[0]['coupons'];
$discount = $item[0]['discount'];
$short_desc = $item[0]['short_desc'];
$status = "";
//echo $coupons;
//exit();
for($i = 0; $i < $amount; $i++)
{
if($coupons <= 0 || ($amount > $coupons))
{
$status = 'out of coupons';
break;
}
if($money >= $price)
{
$money = $money - $price;
$this->db->query("INSERT INTO user_items (`discount`, `short_desc`, `user_id`)
VALUES(".$discount.", '".$short_desc."', ".$user_id.")");
$this->db->query("UPDATE users SET money = ".$money." WHERE id = ".$user_id);
$coupons -= 1;
$this->db->query("UPDATE items SET coupons = ".$coupons." WHERE id = ".$item_id);
$q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id);
$user = $q->result_array();
$money = $user[0]['money'];
$_SESSION['money'] = $money;
$status = 'buy successfull';
//redirect('');
}
else
{
//
$status = 'not enough money';
break;
}
}
switch($status)
{
case 'out of coupons':
echo 'out of coupons';
break;
case 'buy successfull':
$bought_times += 1;
$this->db->query("UPDATE items SET bought_times = ".$bought_times." WHERE id = ".$item_id);
echo 'buy successfull';
break;
case 'not enough money':
echo 'not enough money';
break;
}
/*foreach($q->result_array() as $u);
{
$_SESSION['id'] = $u[0]->id;
$_SESSION['email'] = $u[0]->email;
redirect('');
}*/
//return 'trolol';
}
else
{
echo 'out of time';
}
//echo $user_id;
}
this is the controller that ajax requests, in my local machine all its okey, but in the hosting its gives an error.
i need to subtract money and subtract coupons and insert row depends of the users amount
can you give me an advice about this code?
Upvotes: 0
Views: 1036
Reputation: 802
make your query more dynamic having a one blow query only try making a concatenation using the loop that may produce a query something like:
INSERT INTO my_table(column1, column2, column3) VALUES ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3') ...
in this way you would be able to throw just one query but inserts multiple items into your table just separate each set of values by comma (,).
Upvotes: 1