Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

using part page url as reference

General discussions related to php

Moderators: macek, egami, gesf

using part page url as reference

Postby robhargreaves » Sun Jul 21, 2013 2:04 am

Hi

Please can someone tell me if possible and how to reference part of a page url in

<code>

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

</code>

so for example if my page is called news.php is it possible to prefix content.php and make it automatically news_content.php in the code.

I hope this makes sense!

Thanks Rob
robhargreaves
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Jul 21, 2013 1:44 am

Re: using part page url as reference

Postby MarioParty » Sun Jul 21, 2013 2:56 am

Code: Select all

<?php
// Get the current page filename
$include_file = __FILE__;

// Remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  
$ext = pathinfo($filename, PATHINFO_EXTENSION);
  
return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
$include_file = remove_extension($include_file);

// Add "_content.php"
$include_file .= '_content.php';

// Include the file if it exists
if(file_exists($include_file)) {
  include($include_file);
}
?>


I haven't tested this, but it should work.

There are many other ways of doing this, but this is a solution that matches the requirements.
MarioParty
New php-forum User
New php-forum User
 
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am

Re: using part page url as reference

Postby robhargreaves » Sun Jul 21, 2013 5:12 am

Hi Thanks for your help this is fantastic it.

If i wanted to use the same code but instead to take the string and substitute

Code: Select all
<?php echo $aboutmesubtitle1 ?>


with $filenamesubtitle1 no spaces which bit of the code would I change?

I have tried this

Code: Select all
   <?php
    // Get the current page filename
    $include_file = __FILE__;

    // Remove the file extension .php (or any other file extension)
    function remove_extension($filename) {
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
    }
    $include_file = remove_extension($include_file);

    // Add "_content.php"
    $include_file .= 'titlecontent ';

    // Include the file if it exists
    if(file_exists($include_file)) {
      include($include_file);
    }
    ?>


This isn't working so I think its because it is referencing itself - would it be possible to have it reference the parent?

Thanks
robhargreaves
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Jul 21, 2013 1:44 am

Re: using part page url as reference

Postby MarioParty » Sun Jul 21, 2013 3:00 pm

I don't really understand what you are asking about $aboutmesubtitle1 and $filenamesubtitle1.

Here is some altered code to help you do what you need to

news.php:
Code: Select all

<?php
// Get the current page filename
$include_file = __FILE__;
// $include_file = news.php

// Include news_content.php
include('content.php');
?>


content.php:
Code: Select all

<?php
// Remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
$include_file = remove_extension($include_file);
// $include_file = news

// Add "_content.php"
$include_file .= '_content.php';
// $include_file = news_content.php

// Include the file if it exists
if(file_exists($include_file)) {
  include($include_file);
}
// include('news_content.php');
?>
MarioParty
New php-forum User
New php-forum User
 
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am

Re: using part page url as reference

Postby robhargreaves » Mon Jul 22, 2013 1:28 am

Hi Mario

let me give a more detailed example

I have a model for the site where I call all the pages by a name lets call it example

example.php and all example.php content is referenced from variables.php so the title, meta, headings, text content everything text comes from the variables file. This is good for editing quickly.

here are a few examples of where im using the references

Code: Select all
<meta name="description" content="<?php echo $examplemetadescription;?>" />
<meta name="keywords" content="<?php echo $examplekeywords;?>" />
<meta name="author" content="<?php echo $exampleauthor;?>" />
<meta name="robots" content="<?php echo $examplerobots;?>" />

<link rel="stylesheet" type="text/css" href="genericpagestyle.css" media="screen" />

<title>website-name.com | <?php echo $examplepagetitle;?></title>



variables.php has a list of the text and adds it when the page is loaded in the browser so the file for the above looks like

Code: Select all
$examplepagetitle="Page Title";
$examplemetadescription="The Page Description for meta"


etc etc

I know this is a fairly standard approach so what I want to do is go one step further and save time having to edit all the explicit examples when creating new pages. Some code i found that ive been trying to adapt is

Code: Select all
    <?php
    // Get the current page filename
    $include_file = __FILE__;

    // Remove the file extension .php (or any other file extension)
    function remove_extension($filename) {
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
    }
    $include_file = remove_extension($include_file);
    ?>


Can you help me finish it off so I can add to each of the references to concatenate filename with variable end reference.

Thanks Rob
robhargreaves
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Jul 21, 2013 1:44 am

Re: using part page url as reference

Postby MarioParty » Sat Aug 03, 2013 3:51 am

What you need to do is something like this:

functions.php
Code: Select all
// Function to get the page name without the extension
function get_page_file_name_without_extension($file_name = __FILE__) {
  // Get the current page filename
  $include_file = $file_name;

  // Remove the file extension .php (or any other file extension)
  return $include_file = remove_extension($include_file);
}

// Function to remove the file extension .php (or any other file extension)
function remove_extension($filename) {
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  return preg_replace('/\.' . preg_quote($ext, '/') . '$/', '', $filename);
}
 


example.php
Code: Select all

<?php
include('functions.php');

$page_name = get_page_file_name_without_extension(__FILE__);
?>
<meta name="description" content="<?php echo ${$page_name . 'metadescription'}; ?>" />
MarioParty
New php-forum User
New php-forum User
 
Posts: 42
Joined: Tue Aug 02, 2011 2:52 am


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron