[eluser]Barwick[/eluser]
I currently have this in my controller (works and functions as I need it too). Individually grabs a row (3 rows in total) from a "pricing" table and displays the info throughout 3 packages in the view.
(1) My first question - is there any easier way to write this? Or better question, how do I simplify this? I need 3 sets of different variables to display in the view...
(2) Second question - considering this is a database call, it should likely go in the model.
However, I need to send variables to the view. How would I create a model function for this instead? Allowing me to send the necessary variables to my view...
Code: // Pricing and package details insert from db
$this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$row1 = $query->row(0); // 0 grabs first row in table: Package 1
$data['row1'] = $row1;
$row2 = $query->row(1); // 1 grabs second row in table: Package 2
$data['row2'] = $row2;
$row3 = $query->row(2); // 2 grabs third row in table: Package 3
$data['row3'] = $row3;
// adds view within view, $scripts
$data['scripts'] = $this -> load -> view('shared/scripts_view', '', TRUE);
$this -> load -> view('shared/header_view', $data);
$this -> load -> view("subscribe.php", $data);
$this -> load -> view('shared/footer_view', $data);
[eluser]jprateragg[/eluser]
You could just save the result to an array:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['pricing'] = $query->result_array();
[eluser]Barwick[/eluser]
[quote author="jprateragg" date="1352263616"]You could just save the result to an array:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['pricing'] = $query->result_array();
[/quote]
Wouldn't I then need to define each row still though?
[eluser]n_uryanto[/eluser]
wow thnaks for ur answer for problem.
i have problem same for you.
nuryanto
[eluser]Aken[/eluser]
[quote author="Barwick" date="1352264580"][quote author="jprateragg" date="1352263616"]You could just save the result to an array:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['pricing'] = $query->result_array();
[/quote]
Wouldn't I then need to define each row still though?[/quote]
It depends how you're accessing the data in your views. If you need them in individual, separate parts of your view, then using result() or result_array() might not be very efficient. If you can do a foreach() in your view, then those methods would be more appropriate.
[eluser]Barwick[/eluser]
[quote author="Aken" date="1352278042"][quote author="Barwick" date="1352264580"][quote author="jprateragg" date="1352263616"]You could just save the result to an array:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['pricing'] = $query->result_array();
[/quote]
Wouldn't I then need to define each row still though?[/quote]
It depends how you're accessing the data in your views. If you need them in individual, separate parts of your view, then using result() or result_array() might not be very efficient. If you can do a foreach() in your view, then those methods would be more appropriate.[/quote]
I need them in separate parts unfortunately. So I guess I'm stuck with several extra lines of code then. Just don't want to develop bad or incorrect CI habits. Hence odd question like this one.
[eluser]boltsabre[/eluser]
You could wrap your three assigns inside a loop, reducing the need to "copy/paste" and manually update the number. Saves a few lines of code and it less prone to bug...
[eluser]Barwick[/eluser]
[quote author="boltsabre" date="1352299267"]You could wrap your three assigns inside a loop, reducing the need to "copy/paste" and manually update the number. Saves a few lines of code and it less prone to bug...[/quote]
How do you mean? code example?
[eluser]PhilTem[/eluser]
You could do this fancy thing:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['items'] =& $query;
$this->load->view('view', $data);
and in your view
Code: $item1 = $items->row(0);
echo $item1->title;
Or, to make it a little less "let's put a db resource handle object into our view" do it like this
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data = array();
for ( $i = 0; $i < 3; $i++ )
{
$data['item' . $i] = $query->row($i);
}
$this->load->view('view', $data);
which will give you as well access to
PS: The second example is just a short version of your originally posted code
[eluser]Barwick[/eluser]
[quote author="PhilTem" date="1352302135"]You could do this fancy thing:
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data['items'] =& $query;
$this->load->view('view', $data);
and in your view
Code: $item1 = $items->row(0);
echo $item1->title;
Thanks! This works...but leaning the framework, this is probably not the best route. I'll forget what this means lol.
Or, to make it a little less "let's put a db resource handle object into our view" do it like this
Code: $this->db->select('title, price, number_sites, number_entries, white_label');
$query = $this->db->get('pricing', 3); // limit 3 rows
$data = array();
for ( $i = 0; $i < 3; $i++ )
{
$data['item' . $i] = $query->row($i);
}
$this->load->view('view', $data);
which will give you as well access to
PS: The second example is just a short version of your originally posted code  [/quote]
|