Welcome Guest, Not a member yet? Register   Sign In
Updating config/routes.php using mysql
#1

[eluser]mattnewark[/eluser]
Model
Code:
function update_routes()
    {
            $this->db->select("*");
            $query = $this->db->get("articles");
            foreach ($query->result() as $r)
                {
                    $data = '$route["' . $r['link'] . '"] = "' . $r['route'] . '";';
                    write_file(APPPATH . 'cache/routes.php', $data);
                }                
    }
Controller
Code:
function update_routes()
    {
       $this->articles_model->update_routes();
       redirect('admin/articles', 'refresh');
    }


Can someone please tell me what I am doing wrong, I know it is going to be something stupid but I think I have been looking at it for so long I am unable to see the mistake. When I run the above function, instead of it running through all the database rows and entering them into the file it is only adding the last row into the file.

Can someone please tell what I am doing wrong.

Thanks
#2

[eluser]robert.fulcher[/eluser]
I think the write_file will replace the contents and append. so try it like this...

Code:
function update_routes()
    {
            $this->db->select("*");
            $query = $this->db->get("articles");
            foreach ($query->result() as $r)
                {
                    $data = $data.'$route["' . $r['link'] . '"] = "' . $r['route'] . '";';
                }
            write_file(APPPATH . 'cache/routes.php', $data);                
    }

You might need to put a newline character in-between $data & $route.

Hope this helps.
#3

[eluser]ivantcholakov[/eluser]
foreach ($query->result_array() as $r)
#4

[eluser]mattnewark[/eluser]
Hi,

Thanks for the replies but unfortunately non of the options worked, when I do the below:

Code:
$data = $data.'$route["' . $r['link'] . '"] = "' . $r['route'] . '";';

I get an error advising that I have an undefined variable $data.

and when adding the below I get the same issues, it only writes the last row..

Code:
foreach ($query->result_array() as $r)

I just have no idea why it is not writing all the rows to the file..

Thanks
#5

[eluser]robert.fulcher[/eluser]
mattnewark,

From looking at the code, you are looping, and the blow code
Code:
$data = '$route["' . $r['link'] . '"] = "' . $r['route'] . '";';
will reassign $data to the most recent information that is in the string to the right of the equals. Resulting in just the last line only being written to the file.

So what you need to do is build your full $data string and then write it once to the file. I think the reason you got the undefined variable is because you did not define $data outside the for each.

Give this a try

Code:
function update_routes()
    {
            $this->db->select("*");
            $query = $this->db->get("articles");
            $data = '';
            foreach ($query->result() as $r)
                {
                    $data = $data.'$route["' . $r['link'] . '"] = "' . $r['route'] . '";';
                }
            write_file(APPPATH . 'cache/routes.php', $data);                
    }

Basically you initialize the $data outside the foreach and it should be ok.

Thanks
#6

[eluser]mattnewark[/eluser]
Hi,

Marvellous, Robert.Fulcher, much appreciated. Smile

Worked a treat.

Thanks
#7

[eluser]robert.fulcher[/eluser]
You are welcome.....
#8

[eluser]mattnewark[/eluser]
Hi, Me Again Smile

The code from previous adds the info to the cache file which is great, now I am including the cache file in the routes file so that it dynamically updates the $routes.
What is happening is when I update the routes the info that is in the routes file shows at the top of the page, I have no idea why it is showing, it seems as though it is echoing the file contents.
Also, even though the routes file is updated and is in the routes.php file the routes are not working.

Model:
Code:
function cache_routes ()
    {
        $this->db->where('route', '1');
        $query = $this->db->get('cms-page');
        $route = 'site/pages/main';
        $data = '';
        foreach ($query->result() as $row)
        {
            $data =$data.'$route["' . $row->page_url . '"] = "' . $route . '";';
        }
        write_file(APPPATH .  "cache/routes.php", $data);
    }
Controller:
Code:
function update_routes()
    {
       $this->cms_model->cache_routes();
       redirect('admin/cms', 'refresh');
    }
Routes.php
Code:
require_once("application/cache/routes.php");

which produces this:

$route["about"] = "site/pages/main";$route["events"] = "site/pages/main";$route["news"] = "site/pages/main";$route["events/organise-your-event"] = "site/pages/main";$route["referrals"] = "site/pages/main";$route["fundraising"] = "site/pages/main";$route["care"] = "site/pages/main";$route["volunteering"] = "site/pages/main";$route["business"] = "site/pages/main";$route["test"] = "site/pages/main";
404 Page Not Found

The page you requested was not found.

in the view.

Can anyone please shed some light on why this happening and what I can do to to get the $routes reading form the cache file and not showing in the view.

#9

[eluser]CroNiX[/eluser]
It looks like you are forgetting to insert an opening php tag at the top of the routes.php file that you're writing, so it's being interpreted as text instead of a php file.
Before the loop, try changing
Code:
$data = '';
to
Code:
$data = "<?php \n";

Also it would be more readable if you inserted a line break at the end of each route.
Code:
$data =$data.'$route["' . $row->page_url . '"] = "' . $route . '";' . "\n";
#10

[eluser]mattnewark[/eluser]
Hi,

Thanks for the help, unfortunately when I add the <?php \n to the $data it breaks the site completely as it adds the <?php /n to the start of all the $routes. and the \n at the end of the foreach doesn't make a difference.

Is there anything else you ca think of?

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB