Welcome Guest, Not a member yet? Register   Sign In
small blog help needed passing data
#1

[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) {
        $this->load->model('Entrymodel');
        $title = $this->input->post('title');
        $comment = $this->input->post('comment');
        $this->Entrymodel->sendentry($title, $comment);
        
        if($data > 0){
        
        echo $data;
        $this->load->view('blogentryview', $data);
        }
        else {
            echo "there was no blogs added";
        
        }
        
    }


Model
Code:
function sendentry($title, $comment) {
        
        $query_str = "INSERT INTO entries (title, body) Value ('{$title}', '{$comment}')";

        $this->db->query($query_str, array($title, $comment));
        
        $data_retrive = $this->db->get('entries');
        if ($data_retrive->num_row()> 0) {
            
            foreach($data_retrive->result() as $row){
                
                $data[] = row;
                echo $row->title;
                
            }
            return $data;
        }
                        
    }


Thank You!!!

Sara
#2

[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) {
        
        $query_str = "INSERT INTO entries (title, body) Value ('{$title}', '{$comment}')";

        $this->db->query($query_str, array($title, $comment));
        
        $data_retrive = $this->db->get('entries');
        if ($data_retrive->num_rows()> 0) {
            
            foreach($data_retrive->result() as $row){
                
                $data[] = row;
                echo $row->title;
                echo $row->body;          ********--------> this is the field i want to add from the database
                
            }
            return $data;
        }
                        
    }

doesnt work tho. any idea?
#3

[eluser]SlavomirJ[/eluser]
Did you try debug your results? Smile

Code:
var_dump($row)

see if you are actually retrieve any data
#4

[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
#5

[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/>";
}
#6

[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?
#7

[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.
#8

[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) {
        $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";
        
        }
        
    }


Model
Code:
function sendentry($title, $comment) {
        
        $query_str = "INSERT INTO entries (title, body) Value ('{$title}', '{$comment}')";

        $this->db->query($query_str, array($title, $comment));
        
        $data = $this->Entrymodel->sendback();
        
    }
        
        function sendback () {
            $data = array();
            $rows = $this->db->get('entries');
        
            if($rows->num_rows() > 0) {
            foreach($rows->return() as $row);
        
            $data[] = array('title'=>$row['title'], 'body' =>$row['body']);
            return $data;
            }
        }
#9

[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']);
#10

[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) {
        $this->load->model('Entrymodel');
        $title = $this->input->post('title');
        $comment = $this->input->post('comment');
        $data = $this->Entrymodel->sendentry($title, $comment);
        
        if($data > 0){
                    var_dump($data);
        echo $data;
        $this->load->view('blogentryview', $data);
        }
        else {
            echo "there was no blogs added";
        
        }
        
    }


View
Code:
&lt;html&gt;
<h1> Success this is the blog page with the css styling </h1>

<h3>&lt;?php echo $data->title?&gt;</h3></br>
<h4>&lt;?php echo $data->body?&gt;</h4>

&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB