Loading Page data from Database - UAP2 Howto
Views 546
Sweets 0
Images 0

4 years ago Topic

DaVaR
Offline
Administrator
Total Posts: 141
Joined: July 14, 2015
Posted: January 30, 2016 @ 02:01 PM
In this topic I am going to show you one way of pulling page data from database. For example you want to display your about page, but be able to edit it without having to use FTP. We would save that page\'s data to the database. HTML data that is. Create the database structure
Code
--
-- Table structure for table `uap_pages`
--

CREATE TABLE IF NOT EXISTS `uap_pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `page_url` varchar(255) NOT NULL,
  `page_title` varchar(255) NOT NULL,
  `page_content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
While I was in phpMyAdmin I added my sample page to the database. page_url: about page_title: About My Site page_content: Welcome to my about page. Hope you like it! Create a view page I am going to use subpage.php in this example, but you can easily create a new view file. Some of you may want to run all your html from the database, which is not hard to do. Just remove the html from the view file you created, and only return your $data[\'page_content\']. Add pages route to routes.php Just add the following to the routes.php file near the subpage route.
Code
/** Pages */
Router::any(\'p/(:any)\', \'Controllers\\Welcome@pages\');
That will let the site know we want to load a page like this: http://www.demo.com/p/about Create a page loader model I created a file named LoadPages.php in my Models folder. This file will only get data from the database. It should look something like this:
Code
<?php
/**
* Model to get all pages Data from pages database
*/

namespace Models;

class LoadPages extends \\Core\\Model {

  /**
	 * Get page title from database
	 */
	public function getPageTitle($where_page){
    echo \"$where_page\";
		$data = $this->db->select(\'SELECT page_title FROM \'.PREFIX.\'pages WHERE page_url = :page_url\',
			array(\':page_url\' => $where_page));
    if(count($data) > 0){
		    return $data[0]->page_title;
    }else{
        return \"Page Error\";
    }
	}

  /**
	 * Get page content from database
	 */
	public function getPageContent($where_page){
		$data = $this->db->select(\'SELECT page_content FROM \'.PREFIX.\'pages WHERE page_url = :page_url\',
			array(\':page_url\' => $where_page));
      if(count($data) > 0){
  		    return $data[0]->page_content;
      }else{
          return \"Page Error - The Page You Requested Does Not Exist.\";
      }
	}

}
Create a function in the controller to run pages First we let the Welcome controller know that we are using the LoadPages model.
Code
use Models\\LoadPages;
After the start of the Welcome class we want to call the var as public. (before construct)
Code
public $LoadPages;
Within the construct function we add the following to call for the Model:
Code
        /** initialise the LoadPages object */
    		$this->LoadPages = new \\Models\\LoadPages();
Then add the function pages near the bottom of Welcome controller.
Code
    /**
     * Ready the pages data for requested page and load template files
     */
    public function pages($page_url)
    {
        $data[\'title\'] = $this->LoadPages->getPageTitle($page_url);
        $data[\'welcome_message\'] = $this->LoadPages->getPageContent($page_url);
        $data[\'sidebar\'] = $this->RightLinks->DisplaySiteStats();

    		// Setup Breadcrumbs
    		$data[\'breadcrumbs\'] = \"
    			<li><a href=\'\".DIR.\"\'>Home</a></li>
    			<li class=\'active\'>\".$data[\'title\'].\"</li>
    		\";

        View::renderTemplate(\'header\', $data);
        View::render(\'welcome/subpage\', $data);
        View::renderTemplate(\'footer\', $data);
    }
That should do the trick. You may want to alter a few things here and there. Hope this gets you started. Enjoy!

Signature UAP v4.3.0 is Here!!!

Please keep questions and comments in the forum.

Reply By: xnic0nx
4 years ago #1

xnic0nx
Offline
Member
Total Posts: 19
Joined: January 26, 2016
Posted: January 30, 2016 @ 07:36 PM
Thank you, gonna try this out.


Reply By: DaVaR
4 years ago #2

DaVaR
Offline
Administrator
Total Posts: 141
Joined: July 14, 2015
Posted: January 30, 2016 @ 08:20 PM
This script works on my test site with UAP2. http://uap2demo.userapplepie.com/p/about

Signature UAP v4.3.0 is Here!!!

Please keep questions and comments in the forum.

Reply By: xnic0nx
4 years ago #3

xnic0nx
Offline
Member
Total Posts: 19
Joined: January 26, 2016
Posted: January 30, 2016 @ 08:27 PM
/** Pages */ Router::any(\'(:any)\', \'Controllers\\Welcome@pages\'); Works as well, I will load database and static pages. http://ezclan.ga/Test Man I could probably pick your brain for hours lol


Reply By: DaVaR
4 years ago #4

DaVaR
Offline
Administrator
Total Posts: 141
Joined: July 14, 2015
Posted: January 30, 2016 @ 08:40 PM
Nice work. Looks good!

Signature UAP v4.3.0 is Here!!!

Please keep questions and comments in the forum.

Reply By: xnic0nx
4 years ago #5

xnic0nx
Offline
Member
Total Posts: 19
Joined: January 26, 2016
Posted: January 30, 2016 @ 08:42 PM
I will post some changes once I get this done for multi-site setup.


Reply By: DaVaR
4 years ago #6

DaVaR
Offline
Administrator
Total Posts: 141
Joined: July 14, 2015
Posted: January 30, 2016 @ 08:50 PM
Thanks, and if you would like to use BBcode to format like the first post, check this topic out for what it accepts. http://www.userapplepie.com/Forum/display_topic/25/

Signature UAP v4.3.0 is Here!!!

Please keep questions and comments in the forum.

Forum Permissions
You cannot post in this forum.
You cannot moderate this forum.
You cannot administrate this forum.
Search Forums
Forum Recent Posts