![]() |
unable to display values from array in a view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: unable to display values from array in a view (/showthread.php?tid=69450) Pages:
1
2
|
unable to display values from array in a view - nadeem14375 - 11-25-2017 Hi all, I am trying to out values using loop from an array, but facing error: Quote:Message: Undefined index: payment_description here my model: PHP Code: public function get_fee_invoice_4class($data){ Here my controller: PHP Code: public function rep_fee_invoice_4class() Here my view, I am using html2pdf for pdf outpu. PHP Code: <style type="text/Css"> I tried /* echo '<pre>'; print_r($this->data['fee_detail']); echo '</pre>'; die(); */ and it return the following but unable to display these fields in view. Quote:Array RE: unable to display values from array in a view - PaulD - 11-25-2017 Your problem is that your array does not contain the index 'payment_description' so when you try to output it then php is saying it is not defined, which it is not. It seems you are selecting it though in your model, which is strange. If I were in this situation, I would first output the query string itself and check it is doing what I expected. Use $this->db->last_query() as described in docs: https://www.codeigniter.com/user_guide/database/helpers.html Then run the query that was outputted in your database directly and look at the results. Again, are they what you expected? ie do they have payment description field in them. If you remove the line for payment description, is your code finding and displaying the payment_amount? If so you are doing nothing wrong in your output, just that the index is not defined for payment_description. Hope that helps, Paul. RE: unable to display values from array in a view - nadeem14375 - 11-25-2017 (11-25-2017, 10:34 AM)PaulD Wrote: Your problem is that your array does not contain the index 'payment_description' so when you try to output it then php is saying it is not defined, which it is not. It seems you are selecting it though in your model, which is strange. Hi Paul, Thanks for your reply. $this->db->last_query() Return this query Quote:SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0 I tested the query in Mysql Workbench, it return data. I tried only the payment_amount in my view, but still the same error. Please check the last lines of my view are these correct? RE: unable to display values from array in a view - PaulD - 11-25-2017 Your query is asking for: PHP Code: ->select('payment_amount', 'payment_description','payment_student'); But your array is not showing those indexes. I do not understand why not. You have a missing index 'payment_description'. In the error message, where is it indicating this is an issue, as if it does not exist in your database I am not certain how the query would run in the first place. Is it in the model, the controller or the view? Perhaps you are fiddling with the code between pastes, but the query string you posted is different as it is not selecting 'payment_description', so where did that come from? Paul. RE: unable to display values from array in a view - nadeem14375 - 11-25-2017 (11-25-2017, 11:56 AM)PaulD Wrote: Your query is asking for: Whats the problem with query in this model? PHP Code: $this->anotherDb echo $this->anotherDb->last_query(); returns the following, means the about ->select has problem, here is only payment_student. PHP Code: SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0 I can execute the following in query in mysqlworkbench. PHP Code: SELECT payment_description, payment_student, payment_amount FROM payments; RE: unable to display values from array in a view - PaulD - 11-25-2017 IGNORE THIS: (Your query is in a for loop so you do not want a single query as written here) Try replacing your query with this: PHP Code: return $this->anotherDb You also do this PHP Code: foreach ($fee_detail as $key => $data) { PHP Code: foreach ($fee_detail as $data) { Does that make any difference? PS I really do not like your for loop to get each student id. Just nest two foreaches, would be cleaner to debug. RE: unable to display values from array in a view - nadeem14375 - 11-25-2017 no luck I tried both foreach ($fee_detail as $key => $data) { and foreach ($fee_detail as $data) { tried to pick the last_query, and the result is strange: echo $this->anotherDb->last_query(); die(); SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0 RE: unable to display values from array in a view - PaulD - 11-25-2017 Try something like this: PHP Code: public function get_fee_invoice_4class($data) Should work, however this is BAD, because imagine you have 1000 students, then you will have 1000 database calls because of running a query in a loop. I think what you really need is a more complex query with a join from students to students payments to get all the data in one query. RE: unable to display values from array in a view - nadeem14375 - 11-25-2017 here is the output pdf attached. RE: unable to display values from array in a view - PaulD - 11-25-2017 (11-25-2017, 01:44 PM)nadeem14375 Wrote: SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0 What result do you get without all that strange date stuff that I am unfamiliar with it. This looks wrong too "%d-%m%Y" should it be "%d-%m-%Y" (notice the additional hyphen) |