![]() |
small blog help needed passing data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: small blog help needed passing data (/showthread.php?tid=44342) Pages:
1
2
|
small blog help needed passing data - El Forum - 08-11-2011 [eluser]echo sara[/eluser] Hi I am currently trying to make a small testing blog. Iam currently stuck on one line (getting a error) I need assistance figuring thats wrong with that line. I also need assistance on if it was coded right? can someone skim through it and see if it makes sense. any advise is welcome as well. my error was on line 16 on the model which is Code: if ($data_retrive->num_row()> 0) { Controller Code: function entrytest($data) { Model Code: function sendentry($title, $comment) { Thank You!!! Sara small blog help needed passing data - El Forum - 08-11-2011 [eluser]echo sara[/eluser] hey i figured it out it was rows not row. Now i am able to see the title field data in the view. I want to show a second field as well how would i include that i tried this but i dont think its right. Model Code: function sendentry($title, $comment) { doesnt work tho. any idea? small blog help needed passing data - El Forum - 08-12-2011 [eluser]SlavomirJ[/eluser] Did you try debug your results? ![]() Code: var_dump($row) see if you are actually retrieve any data small blog help needed passing data - El Forum - 08-12-2011 [eluser]echo sara[/eluser] I am able to retrieve data but i get errors. A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Entry::entrytest() Filename: controllers/entry.php Line Number: 19 Controller line 19 Code: function entrytest($data) { A PHP Error was encountered Severity: Notice Message: Undefined variable: row Filename: models/entrymodel.php Line Number: 17 Model like 17 Code: foreach($data_retrive->result() as $row){ A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: controllers/entry.php Line Number: 25 Controller line 25 Code: if($data > 0){ I don't think i scripted it correctly. Can you review the script above in my other post and see if that looks correct. Thanks Sara small blog help needed passing data - El Forum - 08-12-2011 [eluser]jvicab[/eluser] What I usually do is create separate functions for add and list info from database. In your case, for getting the info from database I would do something like: function list() { $data = array(); $rows = $this->db->get('entries'); if ($rows->num_rows()> 0) { foreach($rows->result() as $row) $data[] = array('title' => $row['title'], 'body' => $row['body']; return $data; } } so now you will have an array of rows on entries and yopu will able to access any of the field value like: $arr = list(); foreach ($arr as $r) { echo "title: " . $r['title'] . ", body: " . $r['body'] . "<br/>"; } small blog help needed passing data - El Forum - 08-12-2011 [eluser]echo sara[/eluser] wouldn't having 2 function take longer? controller sends it to first function then returns back to controller; controller redirects it back to model to a different function then return with the data? small blog help needed passing data - El Forum - 08-12-2011 [eluser]jvicab[/eluser] usually the extra time is not an issue and that way makes your code clear, and modular, thereforre easier to troubleshoot and debug I think in your approachm, you don't need the data parameter in the entrytest function, and you do when you call $this->Entrymodel->sendentry($title, $comment);, so it would look like: function entrytest() { $this->load->model('Entrymodel'); $title = $this->input->post('title'); $comment = $this->input->post('comment'); $data = $this->Entrymodel->sendentry($title, $comment); if($data > 0){ echo $data; $this->load->view('blogentryview', $data); } else { echo "there was no blogs added"; } } that way $data will hold what is returned by sendentry function and you will not have the Missing argument 1 for Entry::entrytest() and Undefined variable: data errors. small blog help needed passing data - El Forum - 08-12-2011 [eluser]echo sara[/eluser] Thank you!!! for your reply and advice. So i created another function just to return the output but i executed that function (in model) from the other model function. Is that a no no when it comes to the MVC structure? I redid what i had before and this is what i have; also an error on line 24 line 24 (sendback function in model) Code: foreach($rows->return() as $row); Controller Code: function entrytest($data) { Model Code: function sendentry($title, $comment) { small blog help needed passing data - El Forum - 08-12-2011 [eluser]jvicab[/eluser] no it is right, you should not include Entrymodel when calling a function of the same class. You should call it that way: $data = $this->sendback(); change the foreach loop code for this: foreach ($rows->result_array() as $row) $data[] = array('title'=>$row['title'], 'body' =>$row['body']); small blog help needed passing data - El Forum - 08-12-2011 [eluser]echo sara[/eluser] ok i have made those changes and for some reason when it returns it returns as there no data in the database. i check to see if data was getting passed in the model and it did. Also getting a error on line 17. i have also attached my view just in case that the problem. but the key issue is that i get this "echo "there was no blogs added"; (else statement in the controller function even when their is data in the database. Line 17 from controller Code: function entrytest($data) { Controller Code: function entrytest($data) { View Code: <html> |