Handy PHP
Wordtracker
Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Creating Your First Template Driven PHP Website Part 2
(1 viewing) 1 Guest
Go to bottom
  • Page:
  • << Start < Prev 1 2 Next > End >>
TOPIC: Creating Your First Template Driven PHP Website Part 2
#217
amwd07
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Creating Your First Template Driven PHP Website Part 2 4 Years, 5 Months ago Karma: 0
This thread discusses the Content article: Creating Your First Template Driven PHP Website Part 2

Really could use some help on this one
I have first tried posting messages with PHPFREAKS
they have said there is a problem with this template system

heres my question

I had several replies but this one the main one

the PHP within the dining_includes.php will not be executed, since it's being echoed as a string (it doesn't parse the PHP when it is obtained through file_get_contents()). in order to have those includes parsed within the dining_includes.php file, you'll need to include it directly rather than spitting it out as a string. it's not really compatible with your template system.

I really like the template system but I need to be able to call a dynamic file within the $main = &quot;&quot;; currently it calls the file but puts it outside the template

here is my code I really hope someone can help with this?

$site_title = &quot;Restaurants, Pubs, Places to Eat, Food &amp; Drink Dine with us&quot;;
$page_title = &quot;TEST&quot;;
$main = file_get_contents ('dining_include.php');


&lt;?php
//require('include/header.php');
//require('include/strapline.php');
//require('include/footer.php');

require('include/nav2.php');

// First we read the contents of the template.php file as the variable $template.
$template = file_get_contents('include/template.php');

// Now we do a search and replace on $template inserting content where the keywords were placed
// We do this with 2 arrays, one for search and one for replace.
// We then use the preg_replace function to make the changes and create an output variable

$search = array('|{PAGE_TITLE}|',
'|{MAIN_MENU}|',
'|{MAIN}|'
//'|{HEADER}|',
//'|{FOOTER}|',
//'|{STRAPLINE}|'

);
$replace = array(&quot;$site_title - $page_title&quot;,
$main_menu,
$main,
//$header,
//$footer,
//$strapline
);

$output = preg_replace($search, $replace, $template);

echo $output;
?&gt;
 
Logged Logged
  The administrator has disabled public write access.
#219
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Re:Creating Your First Template Driven PHP Website 4 Years, 5 Months ago Karma: 5
What you were told at PHPFreaks is true for the most part! Generally, in the template, the content is generated in each of the included files then that content is assigned to a variable. For example, your &quot;dining_include.php&quot; file could be rewritten so that instead of outputting all of the content to the browser or in your case, the file_get_content function, you could assign all of that content to the variable $main instead.
What I mean is, instead of &quot;dining_include.php&quot; using echo or print statements to output the content, assign that same content to a variable.
See below:
Original (sample.php):
CODE
$building = &quot;a house&quot;;
$vehicle = &quot;a car&quot;;
echo &quot;I own $building and $vehicle.&quot;;


Rewritten (sample.php):
CODE
$building = &quot;a house&quot;;
$vehicle = &quot;a car&quot;;
$main = &quot;I own $building and $vehicle.&quot;;


Then in index.php, you could simply use include('sample.php')!

But, if &quot;dining_include.php&quot; is too complex for you to rewrite, then you do have a couple of choices. Neither option should be considered &quot;Good Scripting&quot; but should work.

The first is to use file_get_contents remotely! If you use a url instead of a filename in this function, the server that you are getting the data from will parse the file before sending it to you server. This is true even if they are the same server.
So instead of file_get_content(&quot;dining_include.php&quot;); use file_get_content(&quot;www.domain.com/dining_include.php&quot;);
This nearly triples the work load of your server but will fix your issue I believe.

The second option is easier but again, increases the workload of the server and is generally not recommended. This option also tends to be a bit fussy about when and how it will work and I haven't used it much to tell you for sure. The keyword based template system uses regular expressions to replace keywords with actual content. This is done in my tutorial with an array of keyword, an array of content or content containing variable, a string that has the keywords that are to be replaced, and finally the preg_replace() function that actually performs the action. If in the keyword array, you modify some or all of the items to include an &quot;e&quot; just before the quote(s), you can get script to evaluate the corresponding content as PHP. I know that is probably hard to understand. But I'll try to explain better with an example.
Your keyword array original:
CODE

$search = array('|{PAGE_TITLE}|',
'|{MAIN_MENU}|',
'|{MAIN}|'
//'|{HEADER}|',
//'|{FOOTER}|',
//'|{STRAPLINE}|');


Changed to:
CODE

$search = array('|{PAGE_TITLE}|',
'|{MAIN_MENU}|',
&quot;|{MAIN}|e&quot;
//'|{HEADER}|',
//'|{FOOTER}|',
//'|{STRAPLINE}|');


Notice, you will have to change the single quote to a double quote for this to work. With that change, the script should try and evaluate whatever $main contains as PHP code.

I haven't tested either of these out for your specific needs but from what I have used these functions for in the past, I believe that both of these option should work if you won't rewrite the &quot;dining_include.php&quot; like I highly suggest you should.

Hope this helps.
vujsa
 
Logged Logged
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
#226
amwd07
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Creating Your First Template Driven PHP Website Part 2 4 Years, 5 Months ago Karma: 0
hi thankyou for the reply I will try this out Tuesday
I still think I am going to have to use include as there would be a page of code $main = &quot;&quot; which I am bound to run into more issues

Not sure if you can help me out on this one the guys at Freaksphp &amp; webmasterworld have drawn a blank on this one SEF URL's

currently here what my links look like I have replaced %20 with +
http://www.madeinshropshire.co.uk/demo/index.php?title=about+us

but what I need to work is the folowing link
http://www.madeinshropshire.co.uk/demo/index.php?title=about-us

notice on the first link which displays the page the title about us
I have used the function below but I you can see it does not seem to be outputting the content from the database

I have used this function to replace the spaces etc.
this has worked for the link but nothing found in DB
Any idea's on this one

&lt;?php function stringForUrl($strIn) {
$strOut = $strIn;
$allowed = '/[^a-z0-9 -]/ims';

// only alphanum and spaces allowed
$strOut = preg_replace($allowed, '', $strOut);

// swap spaces for _
$strOut = str_replace(' ', '-', $strOut);

// make lower case for consistancy
$strOut = strtolower($strOut);

return $strOut;
}

?&gt;
 
Logged Logged
  The administrator has disabled public write access.
#227
amwd07
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Creating Your First Template Driven PHP Website 4 Years, 5 Months ago Karma: 0
sorted it I tried you sollution but just showed blank page managed to get it working with the following code found on another forum seems to output the php fine, I would be greatful if you could just look in to my previous post about SEF URL's

ob_start();
include('home_include.php');
$main = ob_get_clean();
 
Logged Logged
  The administrator has disabled public write access.
#229
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Re:Creating Your First Template Driven PHP Website 4 Years, 5 Months ago Karma: 5
I'm sorry I haven't responded sooner but my schedule is very busy and I haven't been able to get a really good idea of what you need.

Here is my problem. I don't know what else is going on! For example, the way your code is written, all numbers and lowercase letters are replaced with nothing. Basically, as I'm reading it, you search for $allowed and replace it with nothing (In other words, you are deleting it), then replacing spaces with dashes and finally making all characters lowercase!

Perhaps there is a typo in the code you posted but that is how I see it. What I don't understand is what you are trying to do. Are you trying to create an SEF URL or are you trying to process (read) an SEF URL?

If you are trying to read an SEF that you have generated, then I suggest using mod_rewrite for Apache. Basically, that takes a URL request that looks like one thing and turns it into something that the script will understand.

For example, if your &quot;working&quot; link looked like this:
www.madeinshropshire.co.uk/demo/index.php?title=about+us

mod_rewrite would change:
www.madeinshropshire.co.uk/demo/about_us.html

so that the script actually read it the correct way.

Now, if you are only going to make very small changes to the URL's, then you can do that in PHP.

For example, the reason that you have &quot;title=about%20us&quot; is to pass information to the script. In this case you are telling the script that the variable $_GET['title'] = 'about us'!

What ever else you are trying to do, you can use that variable for your use in this situation.

Assuming that &quot;about us&quot; corresponds to and actual file or database entry, then &quot;about-us&quot; would not match that corresponding entity. You need to tell the script that the one is the same as the other or tell the script to change one to the other!

If you are looking for &quot;about us&quot; in the database and you want to use the query URL title=about-us, then you need to change the query URL to match the database entry like so:

$title = str_replace('-', ' ', $_GET['title']);

Then you can search the database for $title instead of $_GET['title']!

If you are wanting true SEF URL's then you'll need to replace the query URL with a standard URL and use mod_rewrite to rewrite the standard URL into a query URL that the script will recognize.

If this is not what you are needing, please try to be more specific as to what you are trying to do and the methods that you have tried. It isn't important whether or not the methods you tried worked or not but knowing what you already tried will give me and others that read this a better idea of your end goal is.

vujsa
 
Logged Logged
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
#231
amwd07
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Creating Your First Template Driven PHP Website 4 Years, 5 Months ago Karma: 0
I have to say you are provided me much better support than all the other forums webmasterworld etc.

here is what I have now in my code from what I can undestand my $sef has converted the link to about-us and your (' ', '-', $_GET['title']); converts this back for the page output but the problem is there is still no page output?

http://www.madeinshropshire.co.uk/demo/index.php?title=about-us

CODE

&lt;link href=&quot;css/template.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;?php require('include/connection.php');
require('include/functions.php');
 
$query=&quot;SELECT * FROM content WHERE title='$title'&quot;;
$result1=mysql_query($query);
mysql_close();

//////////////////////////////////////////////////////////

$content_id=mysql_result($result1,$i,&quot;content_id&quot;«»);
$title = str_replace('-', ' ', $_GET['title']);
$desc=mysql_result($result1,$i,&quot;desc&quot;«»);
$sef = stringForUrl($title);

////////////////////////////////////////////////////////

$site_title = &quot;Made In Shropshire Demo Site&quot;;
$page_title = &quot;$title&quot;;

$main .= &quot;&lt;a href=index.php?title=$sef&gt;$title&lt;/a&gt;&lt;hr&gt;$desc&quot;;

require('include/template_includes.php'); ?&gt;
<br><br>Post edited by: vujsa, at: 2007/08/30 23:08
 
Logged Logged
  The administrator has disabled public write access.
Go to top
  • Page:
  • << Start < Prev 1 2 Next > End >>