我正在构建一个在我的localhost上运行的工具,它有助于将静态网页放在一起更快一些。安全性不是问题,因为这只是在本地运行。
首先,我有一个名为include
的{{1}}文件,其中包含如下页面部分的变量:
components.php
然后我的网址看起来像这样:
$slide="Pretend this is markup for a Slider";
$port="Pretend this is markup for a set of portfolio images";
$para="<p>Just another paragraph</p>";
$h1="<h1>This is a Header</h1>";
我的索引文件包含:
//only calling 3 of the 4 sections
localhost/mysite/index.php?sections=h1-slide-para
这里的目标是使用我一直使用的行构建include 'components.php'
$sections = @$_GET['sections'];
$section = explode($sections,"-");
foreach ($section as $row){
echo $row;
}
文件,这样我就可以从浏览器的地址栏快速将页面布局放在一起。我只是不确定如何components.php
变量echo
,explode
只包含我从index.php
文件调用的标记。
答案 0 :(得分:2)
这应该适合你:
只需使用variable variables
访问components.php
文件中的变量(同时切换explode()
中的参数,它们是错误的方法),例如
$section = explode("-", $sections);
foreach ($section as $row) {
echo $$row;
//^^ See here the double dollar sign
}
另一种解决方案是将文件更改为ini
格式,例如
slide="Pretend this is markup for a Slider"
port="Pretend this is markup for a set of portfolio images"
para="<p>Just another paragraph</p>"
h1="<h1>This is a Header</h1>"
然后将其放入包含parse_ini_file()
的数组:
$arr = parse_ini_file("components.ini");
//^^^ Note, that you now work with an .ini file
$sections = @$_GET['sections'];
$section = explode("-", $sections);
foreach ($section as $row) {
echo $arr[$row];
}
答案 1 :(得分:1)
将字符串放入数组:
$sections = [
'slide' => "Pretend this is markup for a Slider",
'port' => "Pretend this is markup for a set of portfolio images",
'para' => "<p>Just another paragraph</p>",
'h1' => "<h1>This is a Header</h1>",
];
然后,按名称引用这些部分:
foreach (explode('-', $_GET['sections']) as $section){
echo $sections[$section];
}
答案 2 :(得分:0)
首先代替
<select style="width: 100%"></select>
使用
$section = explode($sections, "-");
以及
$section = explode("-", $sections);