使用PHP,我如何能够在我的内容模板中加载锚定部分,基于id从文件名读取?

问题描述 投票:2回答:1

所以这是我的布局:

    randompagenumber.php
             |
             |
           / | header.php
php include -| content.php
           \ | footer.php

页脚有一个php脚本,它调用randompagenumer.php的文件名,删除它的扩展名和其名称中的破折号,并输出该文件名的数字作为底角的页面编号。 (左或右,取决于该数字是偶数还是奇数)。

我有几页(现在10页),我称它们为--1.php, - 2.php, - 3.php, - 。4pp, - 5.php, - 6.php , - 7.php, - 8.php, - 9.php,-10.php,...

所以脚本只需要在1,2,3,4,5,6,7,8,9,10 ......

我对页脚的代码:

<?php
$pagenr = basename($_SERVER['PHP_SELF']);
$pagenr = str_replace( array( '.php', '.htm', '.html', '-', '_' ), '', $pagenr ); // Remove extensions and spaces in filename

if ($pagenr % 2 == 0) {
$footlr = "footerlinks";
$pcontainer = "pcontainerl";
} else{
$footlr = "footerrechts";
$pcontainer = "pcontainerr";
}
?>

<div id="<?=$footlr?>">
<div id="pnummer" class="pcontainer"><?=$pagenr?></div>

所以带有id“<?=$footlr?>”的div会在验证偶数或奇数时更改名称,因此会读取另一个css样式(#footerlinks和#footerrechts)。

现在我试图完成的内容与content.php有关我从html知道它可以链接到它自己的文件中的锚点所以我试图与php有一些相同的结果,但我只需要为它管理它加载php页面,但它输出整个content.php

所以基本上我想实现这一点,如果randompagenumber.php读取其文件名(通过footer.php),它会加载来自某个部分的内容,其id与该页码对应。

这就是我的randompagenumber.php目前的样子:

<?php    
$PageName = str_replace( array( $PageName ),  '#p', $pagenr, $PageName );
?>
<!DOCTYPE html>
<html>
<?php include('templates/pheaderhst.php'); ?>
<?php include("templates/content.php$PageName"); ?>
<?php include('templates/pfooter.php'); ?>

所以我在这里做的是创建一个名为PageName的新字符串,它调用从footer.php计算的pagenr字符串,让它作为php页面回显加载<?php include("templates/content.php$PageName"); ?>

只是发现它实际上是加载整个页面,而不仅仅是我命名为section id的内容。

因此,知道php脚本将文件名从006.php更改为简单的数字6,然后将6更改为#p6(因为id不能以数字开头,我先用ap命名)..怎么会这样加载整页而不只是我的<section id="p6">content of page 6</section>的内容?

对于那些感兴趣的人,这里是content.php的样子;

<?php
?>
<section id="p1">
<div id="contentwrapperpaginas"><?php echo 'Content from page 1' ?></div>
</section>
<section id="p2">
<div id="contentwrapperpaginas"><?php echo 'Content from page 2' ?></div>
</section>
<section id="p3">
<div id="contentwrapperpaginas"><?php echo 'Content from page 3' ?></div>
</section>
<section id="p4">
<div id="contentwrapperpaginas"><?php echo 'Content from page 4' ?></div>
</section>
<section id="p5">
<div id="contentwrapperpaginas"><?php echo 'Content from page 5' ?></div>
</section>
<section id="p6">
<div id="contentwrapperpaginas"><?php echo 'Content from page 6' ?></div>
</section>
php include-path
1个回答
0
投票

更新解决方案

所以我开始工作了!

首先是我的content.php:

<?php
?>
...some div's with page id's here...
<div id="p5">
<div id="contentwrapperpaginas">Content from page 5</div>
</div>
<div id="p6">
<div id="contentwrapperpaginas"><p style="color:yellow;margin-left:-25px;">Content from page 6</p></div>
</div>
...some more div's with page id's here...

因此,通过以下脚本,我得到它来设法只加载一个div和它的内部元素。我的复制/粘贴中使用的示例将加载<div id="p6">。该脚本位于index.php中,并使用<?php echo $content_div[0]; ?>加载。 :

$url = 'templates/content.php';
$content = file_get_contents($url);
$first_step = explode( '<div id="p6">' , $content );
$content_div = explode("</div>" , $first_step[1] );

解决方案是将<?php include("templates/content.php$pagenr"); ?>改为<?php echo $content_div[0]; ?>

© www.soinside.com 2019 - 2024. All rights reserved.