EllisLab text mark
Advanced Search
     
Problem with model
Posted: 09 October 2012 02:14 AM   [ Ignore ]
Joined: 2012-10-09
11 posts

Hello,

In one of my controller’s functions i get data from a model via a funtion, and put the data in a string.

$continut $this->biography->getEntries(); 

the getEntries() funtion looks like this:

function getEntries()
  $i
=0
  
$query $this->db->query("SELECT * FROM biografie ORDER BY data DESC");
  foreach (
$query->result() as $row){
   
if ($i ==0){  
    $this
->continut .='
    <div id="bioleft">
    <div class="bioheader">
      <h1 class="biofloat2">' 
$row->titlu '</h1>
    </div>
    <div class="rightline2">
     <p class="dreapta">' 
$row->subtitlu '</p><br /><br />
     <p class="dreapta">' 
nl2br($row->continut) . '</p><br />
    </div>
    </div>
    '
;
    
$i++;  
   
}else{
    $this
->continut .='
    <div id="bioright">
    <div class="bioheader2">
      <h1 class="biofloat">' 
$row->titlu '</h1>
    </div>  
      <div class="rightline">
      <p class="stanga">' 
$row->subtitlu '</p><br />
      <p class="stanga">' 
nl2br($row->continut) . '</p> <br />
    </div>
    </div>
    '
;
    
$i++;
   
}    
  }
  
  
return $this->continut;
 

The problem is the folowing:

If I modify my database and just fo to the function via link it dosen’t show what changes i made in the data base, only a refresh can fetch my data corectly.

I am new to codeigniter, and allso i am a bad writer.

Hope u can understand what i ment.
Please HELP!

 
Posted: 09 October 2012 02:46 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

Correct me if i am wrong MVC works this way:

The view sends a HTTP request tot the controller, the controller accesses a model, and sends data back to the view.

Since the view is something hat the user can see, best practice says that u should never sent data about you’re database, or connect directly in the database in the view.

I wanted to do something with jquery and refresh the <div> where i put the data every second, and it worked great, except 1 thing.

I got the data(the one that i intended to put in the div) form a function in the controller, and the data didn’t refresh, so if i went to the function that posted the data and hit F5 then the string would update on my main page… From my point of view it is the same problem as the one above..


PS: at the moment the website is running on localhost. Would this problem disappear if i put it online? From what i know it shouldn’t matter, but I don’t know…

 
Posted: 09 October 2012 10:16 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

bump…

I really need a answer :(...
Nobody?

 
Posted: 09 October 2012 03:43 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

bump…

 
Posted: 11 October 2012 05:56 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

really nobody ???

:(

 
Posted: 11 October 2012 06:56 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2011-10-12
103 posts
Razvan V - 09 October 2012 02:46 AM

Correct me if i am wrong MVC works this way:
The view sends a HTTP request tot the controller, the controller accesses a model, and sends data back to the view.

MVC for my understanding works that way:

- the application calls the controller
- the controller calls the model
- the model returns data to your controller
- the controller calls the view
- the view displays the html / css / js

Its totally wrong to put html and stuff in your model. those things belong into the view.

 Signature 

SMC - full CRUD Model for CodeIgniter:
forum: http://codeigniter.com/forums/viewthread/223845/

 
Posted: 11 October 2012 07:02 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2011-10-12
103 posts

I can recommend my model for codeigniter for an easier integration with database.
I have also some examples in my bitbucket wiki.

http://ellislab.com/forums/viewthread/223845/

 Signature 

SMC - full CRUD Model for CodeIgniter:
forum: http://codeigniter.com/forums/viewthread/223845/

 
Posted: 11 October 2012 04:56 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts
alexwenzel - 11 October 2012 06:56 AM
Razvan V - 09 October 2012 02:46 AM

Correct me if i am wrong MVC works this way:
The view sends a HTTP request tot the controller, the controller accesses a model, and sends data back to the view.

MVC for my understanding works that way:

- the application calls the controller
- the controller calls the model
- the model returns data to your controller
- the controller calls the view
- the view displays the html / css / js

Its totally wrong to put html and stuff in your model. those things belong into the view.

HTTP request means that a form was send, ie the application calls the controller via a form submit, the controller calls the model and the model returns the result to the controller witch passes the information to the view.

What you wrote is exactly what i wrote…:(

If my knowledge of MVC is correct then why does codeigniter need a refresh to show the correct data?

 

 
Posted: 11 October 2012 06:35 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2011-10-12
103 posts

if its like that then you have a logical mistake in your application flow.

Maybe you displaying the old data. the one before you updated the database record ?!

 Signature 

SMC - full CRUD Model for CodeIgniter:
forum: http://codeigniter.com/forums/viewthread/223845/

 
Posted: 12 October 2012 06:56 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

that is the problem :( i don’t think i am doing it wrong…

I call the getEntries() furnion and i send the string it returns to the controller.
Then the controller passes the string in the view…

Is it posible that the porblem is the fact that the borwser is showing old data?

at the first acces of the website the data si shown correctly


thanx for taking the time to help me smile

 
Posted: 12 October 2012 07:09 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Are you retrieving the content from the database before processing the update?

Consider performing a redirect to the same page after performing the update. It’ll automatically refresh for you, and if the user hits refresh it will not resubmit the form submission another time.

 
Posted: 16 October 2012 04:16 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts
Aken - 12 October 2012 07:09 PM

Are you retrieving the content from the database before processing the update?

Consider performing a redirect to the same page after performing the update. It’ll automatically refresh for you, and if the user hits refresh it will not resubmit the form submission another time.

That’s not my problem.

I insert the data in the database in an administration section.

One page of my website shows the data form the database.
If i insert data in the database(and the page that shows data is loaded), and click the menu link that goes to the function that gets the data, the new post isn’t shown.

Only by a refresh does the data show.

How i see my problem: The browser keeps the previous data and dose’t show the new one. (at least that sounds like a potential problem)

Any idea how do i fix this?

 

 
Posted: 17 October 2012 08:06 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-06-09
50 posts

post your controller have to see what you have done there.

 Signature 

Anbu

 
Posted: 18 October 2012 07:28 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

The Function in my controller

public function biography() {
  
  $this
->load->model('biography');
  
$date['content'$this->biography->getEntries();

  
$this -> load -> view('website/biography'$date);
 

The funtion in the model:

function getEntries() {
  $i 
0;
  
$query $this -> db -> query("SELECT * FROM biografie ORDER BY data DESC");
  foreach (
$query->result() as $row{
   
if ($i == 0{
    $this 
-> continut .= '
    <div id="bioleft">
    <div class="bioheader_' 
$row->id_categorie .   '">
      <h1 class="biofloat2">' 
$row -> titlu '</h1>
    </div>
    <div class="rightline2">
     <p class="dreapta"><strong>' 
$row -> subtitlu '</strong></p><br /><br />';

    if (
$row -> rol != ''{
     $this 
-> continut .= '<p class="dreapta"><strong>' $row -> rol '</strong></p><br />';
    
}
    
if ($row -> continut != ''{
     $this 
-> continut .= ' <p class="dreapta"><i>' nl2br($row -> continut) . '</i></p>';
    
}

    
if ($row -> rol != ''{
     $this 
-> continut .= '<p class="dreapta">' nl2br($row -> descriere) . '</p>';
    
}

    $this 
-> continut .= '</div>
    </div>'
;
    
$i++;
   
else {
    $this 
-> continut .= '
    <div id="bioright">
    <div class="bioheader2_' 
$row->id_categorie .   '">
      <h1 class="biofloat">' 
$row -> titlu '</h1>
    </div>  
      <div class="rightline">
      <p class="stanga"><strong>' 
$row -> subtitlu '</strong></p><br />';

    if (
$row -> rol != ''{
     $this 
-> continut .= '<p class="stanga"><strong>' $row -> rol '</strong></p>';
    
}
    
    
if ($row -> continut != ''{
     $this 
-> continut .= '<p class="stanga"><i>' nl2br($row -> continut) . '</i></p> ';
    
}
    
if ($row -> descriere != ''{
     $this 
-> continut .= '<p class="stanga">' nl2br($row -> descriere) . '</p>';
    
}
        
    $this 
-> continut .= '</div></div>';
    
$i++;
   
}
  }

  
return $this -> continut;
 


The View has a:

<div>
<?php echo $content?>
</div

Again the problem if i refresh the page (f5) it updates if i acces my function via link

www.example.com/index.php/controller/biografie 

it dosent refresh…

 
Posted: 18 October 2012 07:37 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2012-10-09
11 posts

As you can see I’m not doing anything spectacular there, that is my problem i’m doing something simple and it doesn’t work right.

Does CodeIgniter save the results of a function at it’s first access and then is shows old data?