Welcome Guest, Not a member yet? Register   Sign In
Cannot Retrieve the Correct Content
#1

(This post was last modified: 03-08-2024, 02:39 AM by demyr.)

Hi. That could be a silly problem but let me write: I send a request from my Controller to the model as usual to retrieve a page. To make everything certain I use id.

Controller:

Code:
$page = $this->PageModel->fetch_page($page_id);


However, I see that it duplicates the number in my Model and brings me the latest row. For example, if the id is 1, it makes it 11. 

My Model:

Code:
public function fetch_page($page_id){
      
        $db      = \Config\Database::connect();
        $builder = $db->table('pages as p');
        $query = $builder->select('*')
                        ->where('p.page_id', $page_id)
                        ->where('p.page_status', 1)
                        ->get();

        return $query->getRow();
      }

Btw, it's CodeIgniter version 4.4.6 - the latest one for now.

P.S. It is getting more interesting. Now, it doesn't duplicate but gets me the latest row again. I cleared the sessions but didn't work.

P.S 2: Interestingly when I print_r() the data in my controller the content is correct.
Reply
#2

Are you saying that when the following is executed in your controller, the correct data is displayed:
PHP Code:
$page $this->PageModel->fetch_page($page_id);

echo 
dd($page); 
If that is the case I am curious what data you are sending to your view.
Reply
#3

(This post was last modified: 03-08-2024, 09:14 AM by demyr.)

(03-08-2024, 07:19 AM)JustJohnQ Wrote: Are you saying that when the following is executed in your controller, the correct data is displayed:
PHP Code:
$page $this->PageModel->fetch_page($page_id);

echo 
dd($page); 
If that is the case I am curious what data you are sending to your view.

Your echo line turns 0. And I just send $data['page'] to my view. Nothing unusual.

Well, it is so weird. Doesn't matter if I call a direct ->find method in Controller or if I try to fetch the data by slug as well as Id. Still believe it is going to turn out to be sth silly. Because if there were a problem with db connection, then I couldn't get the last row.
Reply
#4

I am confused. From your controller you call:
Code:
$page = $this->PageModel->fetch_page($page_id);

dd($page) returns 0, but print_r($page) has the correct object?
Then you send $data['page'] to your view?
Code:
$data['page'] = $this->PageModel->fetch_page($page_id);

Or do you fill $data['page'] in another way?
Reply
#5

(03-08-2024, 09:14 AM)JustJohnQ Wrote: I am confused. From your controller you call:
Code:
$page = $this->PageModel->fetch_page($page_id);

dd($page) returns 0, but print_r($page) has the correct object?
Then you send $data['page'] to your view?
Code:
$data['page'] = $this->PageModel->fetch_page($page_id);

Or do you fill $data['page'] in another way?

That's correct,  I send this to my view :

Code:
$data['page'] = $this->PageModel->fetch_page($page_id);

My other pages like contact or products etc work, I have problem only with this pages table.
Reply
#6

What does dd($data[‘page’]); show after calling fetch_page in your model?
Reply
#7

Well it works, your dd($data['pages']).

I think this is an issue with Routing. Because I can run the same calling for homepage. It works there, I can display the about page content on my homepage but inner pages do not work. Looks like a routing problem.
Reply
#8

Without seeing any actual code, I can’t help no more. There are enough tools like dd() to focus on the problem. 
Check the routing which doesn’t work again and see if it calls the right function in the model with the correct id.
Reply
#9

dd($page_id);
Reply
#10

(This post was last modified: 03-11-2024, 11:20 AM by demyr.)

(03-08-2024, 04:43 PM)kenjis Wrote: dd($page_id);

it returns 1 as string and when I convert it to integer then it is integer.

Well, I found the reason. As I first stated, it was really silly and I felt like a novice.

For the top menu I keep products and corporate pages for sub menus. And I usually name them as "products_for_menu" and "pages_for_menu" in order to prevent any confliction. However I realized that (while pasting my Controller here) I had mistyped for the pages this time and I kept it as "pages", which conflicted with the page content : "pages".  (facepalm) 

We shouldn't work under stress.

Thank you so much for your time and sorry for this small issue.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB