简体   繁体   中英

Show content depending on url

Is it possible to determine what to show depending on the URL?

I have an index file which is:

<?php include './includes/header.php'; ?>
<?php include './includes/menu.php'; ?>
<?php include './includes/content.php'; ?>
<?php include './includes/sidebar.php'; ?>
<?php include './includes/footer.php'; ?>

Note: I have different " content.php "'s

Is it possible to do something like:

If Url = url {
    show only content for the url
}

and then have case system like

case: home.php
      show some

etc

I know Wordpress can do it. Is it possible with PHP and MySQL and HTML?

EDIT: Instead of content.php i would want show the desired HTML code gotten from my db

Use this function to see your current page. Then use the "switch" case for proper include file:

## Get Current Page / Section
function cur_page()
{
    $cur_page='';
    if(isset($_SERVER['PHP_SELF']) && $_SERVER['PHP_SELF']!='')
    {
        $temp_var1 = explode('/', $_SERVER['PHP_SELF']);
        $cur_page = $temp_var1[count($temp_var1)-1];
    }
    else if(isset($_SERVER['SCRIPT_NAME']) && $_SERVER['SCRIPT_NAME']!='')
    {
        $temp_var1 = explode('/', $_SERVER['SCRIPT_NAME']);
        $cur_page = $temp_var1[count($temp_var1)-1];
    }
    else if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']!='')
    {
        $temp_var1 = explode('/', $_SERVER['REQUEST_URI']);
        $cur_page = $temp_var1[count($temp_var1)-1];
        $temp_var2 = explode('?', $cur_page);
        $cur_page = $temp_var2[0];
    }
    else if(isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME']!='')
    {
        $temp_var1 = explode('/', $_SERVER['SCRIPT_FILENAME']);
        $cur_page = $temp_var1[count($temp_var1)-1];
    }
    return $cur_page;
}//end func.....

Querying from database.

I don't recommend MySql, and I hope you learn PDO instead, but just for this example

function get_me_title($page) {
$query = "SELECT * FROM title WHERE title = $page";
$result = mysql_query($query); 
foreach($result as $row) {

return $row[$page];
}

}

Now, you can use function . get_me_title('whatever') to query from database, and echo below

if(isset($_GET['page_id'])) {

$page = $_GET['page_id'];




switch($page) {

case "contact";
echo get_me_title('contact');
break; 

case "about";
echo get_me_title('about');
break; 


case "portofolio";
echo  get_me_title('portofolio')
break; 

default:
echo 'you are in home page';

}
}else {echo '404 ERROR! The Page you have requested does not exist';}

Instead of including content.php, you can include needed page. For example, if You build Your urls, where, for example, $_GET['page'] will refer to needed page, then simply You can do this.

$availablePages = array('default' => 'home', 'about');
if (isset($_GET['page']) && in_array($_GET['page'], $availablePages) {
    $page = $_GET['page'] . '.php';
} else {
    $page = $availablePages['default'] . '.php';
}

include $page;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM