if, else in Grocery CRUD
- Single Page
Posted 10 March 2012 - 14:52 PM
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]I’m having some problems using Grocery CRUD with my database…[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]Here my table design.[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]Orders :[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]id, products_id,qty,discount, total,etc[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]Products:[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]products_id,, discounted(bool), etc[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]i suppose can check discounted value in if else condition[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3][php]if(discounted)
{
$crud->add_fields('qty','discount','etc');
$crud->edit_fields('qty','discount','etc');
}
else
{
$crud->add_fields('qty'','etc');
$crud->edit_fields('qty,'etc');
}
[/php][/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]is it possible to do that in grocery crud? how can i get the 'discounted' value?[/size][/font][/color][/left]
[left][color=#27343C][font=Arial, Helvetica, sans-serif][size=3]thanks[/size][/font][/color][/left]
Posted 11 March 2012 - 16:23 PM
but what i really want to do is get discounted value from Products table each time i select products_id in Order table(add/edit action).
Posted 12 March 2012 - 00:16 AM
Actually I didn't understand exactly your situation so I will give you 3 solutions.
1) take the value of product from the url:
function orders_example_1($product_id = null)
{
$discounted = false;
if(is_numeric($product_id))
{
$this->db->where('product_id',$product_id);
$product = $this->db->get('products')->row();
if(isset($product->discounted) && $product->discounted == 1)
{
$discounted = true;
}
}
$crud = new grocery_CRUD();
$crud->set_table('your_table_name');
if($discounted)
{
$crud->add_fields('qty','discount','etc');
$crud->edit_fields('qty','discount','etc');
}
else
{
$crud->add_fields('qty'','etc');
$crud->edit_fields('qty,'etc');
}
$output = $crud->render();
....
}
Or you can take it from session:
function orders_example_2()
{
$this->load->library('session');
$discounted = $this->session->get_userdata('discounted');
$crud = new grocery_CRUD();$crud->set_table('your_table_name');
if($discounted)
{
$crud->add_fields('qty','discount','etc');
$crud->edit_fields('qty','discount','etc');
}
else
{
$crud->add_fields('qty'','etc');
$crud->edit_fields('qty,'etc');
}$output = $crud->render();
....
}
or just from the url (that I suppose you don't want this simple solution):
function example_3($discounted = '0')
{
$crud = new grocery_CRUD();
$crud->set_table('your_table_name');
if($discounted == '1')
{
$crud->add_fields('qty','discount','etc');
$crud->edit_fields('qty','discount','etc');
}
else
{
$crud->add_fields('qty'','etc');
$crud->edit_fields('qty,'etc');
}
$output = $crud->render();
....
}
PS. Please have patient with your questions as we all try to help. Even if there is not an answer it is OK and I think it will not help to write again of why you didn't answer my question (also the question was made on Saturday).
If someone don't understand the question he will just say it. In my experience those who really need a quick answer they actually don't get any answer even when someones knows the answer. It is just my opinion nothing more.