thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Conditional inclusion in index page #229

Closed mtaziz123456789 closed 7 years ago

mtaziz123456789 commented 7 years ago

Hello! I want to conditionally include a (as shown in bold text below) page but it doesn't work. My code is as follows Please help me ....

include('template/header.php');

include('views/'.$view['name'].'.php');

if(isset($nav['slug'] = home)) { include('body.php'); }

include('template/footer.php'); /

creptor commented 7 years ago

You're giving me too little information to address your issue if this measure doesn't fix the problem, please upload your index.php and setup.php to this site. (using Gist is appreciated)

My solution:

Important: The types folder, where all the options for the web layout are, should be located within the config folder (Where the setup.php is located). (they are php files...)

Important: Your page database should have at least these tables -> id, slug, type, status (file_name can be used with a correlation on type, so it can be fetch from other table)

setup.php

// code extras
define('DS',DIRECTORY_SEPARATOR);
define('CONF_FOLDER',__DIR__.DS);// C:\\path_to_config_folder\ (Linux or Windows compatible)
$home_page='home'; // change to desired home page, MUST BE THE SLUG of it
function alert($type,$msg,$close=false){
    if($close==true){
        return'<div class="alert alert-'.$type.' alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'.$msg.'</div>';
    }else{
        return'<div class="alert alert-'.$type.'">'.$msg.'</div>';
    }
}

//actual solution
if($path['call_utf8']==false||$path['call_utf8']=='index.php'){
    $path['call_parts'][0]=$home_page;
}
$page=fetch_page($path['call_parts'][0]);
if(isset($page['type'])&&$page['type']!=''){
    if($page['status']==false){
        $page=null;
        $message='The page is disabled right now, check back later';
        $page_type_path=CONF_FOLDER.'types'.DS.'default.php'; //main-page is backup for alerts of not found.
    }else{
        $view=fetch_page_type($page['type']);
        $page_type_path=CONF_FOLDER.'types'.DS.$view['file_name'];
    }
}else{
        // an error occurred, the type was not found
    $message=$page['message'];
    $page_type_path=CONF_FOLDER.'types'.DS.'default.php';
}

index.php

if(!require_once($page_type_path)){
    echo alert('danger','The page couldn\'t be loaded.');
}

Note that I use call_utf8 instead of slug, because the way that the page uses slugs wouldn't allow a site within a site :yum: .

Note: this is based on the site this solution, other methods can be used and in other cases the solution will not work.

creptor commented 7 years ago

Sorry for the long answer, there is no easy way to explain what I did on my site.

If something is not clear please ask, I'll be happy to help you.

mtaziz123456789 commented 7 years ago

My question is very simple but I am sorry I was unable to express. My setup.php and index.php are fully working and are as follows (original index.php)

Index.php

    <?php 
    include('includes/jscss.php'); 
    include('includes/content.php');

    include('includes/'.$view['name'].'.php');

    include('includes/footer.php');

    ?>

setup.php

    <?php
// Setup File:
error_reporting(0);

# Database Connection:
include('../includes/connect.php');

# Constants:
DEFINE('D_TEMPLATE', 'template');
DEFINE('D_VIEW', 'views');

# Functions:
include('includes/data.php');

# Site Setup:
$path = get_path();

$site_title = 'PkAdz';

if(!isset($path['call_parts'][0]) || $path['call_parts'][0] == '' ) {
    header('Location: home');

}

# Page Setup:
$page = data_post($dbc, $path['call_parts'][0]);
$view = data_post_type($dbc, $page['type']);

?>

Let me try to simplify my question. I just want to include a specific page (e.g. body.php) in the index page only (not in all the pages, with the data coming from the database, for this purpose what comes in my mind is as follows

index.php

    <?php
include('template/header.php');

include('views/'.$view['name'].'.php');

if(isset($nav['slug'] = home)) { include('body.php'); }

include('template/footer.php'); /
?>

Means when the slug 'home' is active then include body.php or not. But it doesn't work. Please help me for solving this problem. Note: I have discarded debuge portion from setup.php and also that I have a folder of includes instead of config

creptor commented 7 years ago

I get you now, but I would recommend you use another way to achieve this.

Your mistake is on the index.php file This:

if(isset($nav['slug'] = home)) { include('body.php'); }

Should be:

if(isset($nav['slug'])&&$nav['slug']=='home'){include('body.php');}

Now, the recommended way is to actually create a new page type for the home page. In that way your code will be more logical and reliable for future develop, but any alternative should work just fine.

creptor commented 7 years ago

&& is the same as AND by the way

mtaziz123456789 commented 7 years ago

Thnx for replying. It is not working respected dear.

if(isset($nav['slug'])&&$nav['slug']=='home'){include('body.php');}

Note: I can't use other type for home because in additional part of the page (body.php) I want use some php and mysql queries which is too difficult for me. Please help for this code become working.

creptor commented 7 years ago

Please add the URI of the page on where the if statement should work, maybe there's an alternative....

Also check if $nav is defined and not null.

mtaziz123456789 commented 7 years ago

Sorry I didn't got what you want to say please elaborate if possible.

creptor commented 7 years ago

like... the home page URI would be: http://localhost/home or like: http://thisisawesome.com/register/new/facebook

basically I need every URI that points to the page "home".

URI

mtaziz123456789 commented 7 years ago

index.php

<?php 
    include('includes/jscss.php'); 
    include('includes/content.php');
    include('includes/'.$view['name'].'.php'); 
    if(isset($nav['slug'])&&$nav['slug']=='home'){include('includes/body.php');}
    include('includes/footer.php');

    ?>

http://localhost/cms/pkadz/home

mtaziz123456789 commented 7 years ago

But it does't includes the body.php when I open home page

creptor commented 7 years ago

The index.php is located inside of pkadz? Do you have a site within a site?

I need to know that before making a solution sorry for not realizing before, I was on the phone.

mtaziz123456789 commented 7 years ago

Thanks sir. Yes index.php is inside 'pkadz' folder. No site within site just folder within folder.

creptor commented 7 years ago

Alright, so I have done some research and probably found the error that you're getting here.

The thing is that Apache, with clean URLs, doesn't check for bases (folders) if they aren't called explicitly in the URL. Example: http://localhost/pkadz/ will output that the folder pkadz exists, but http://localhost/pkadz/home won't.

This could lead to wrong website configuration if it's inside of a folder, mistaken redirects, includes, etc. That's why I would recommend you to put always the site on the root folder.

But there're also some fixes for this, one way is to adapt your code using the $path['call_parts'] and the respected array integer (and full URI redirects and includes), or to define the new base on the .htaccess file.

The second option seems more neat, but I don't know how to do it 😢 , I just know that you need to learn about RewriteBase.

The first option should be something like this:

if(isset($path['call_parts'][3])&&$path['call_parts'][3]=='home'){include('includes/body.php');}
creptor commented 7 years ago

If you still have any questions feel free to ask them here 😉

mtaziz123456789 commented 7 years ago

Thanx dear I got my problem solved. Thank you very much and sorry for late reply