早上好,我正在尝试执行以下操作,以便在类型不是'main'时在li中回显区域名称。然而,它只是输出字符串中的内容,有没有办法成功地执行此操作并保存我创建800页脚页面?
$("#chk").prop("checked", "checked").trigger("change");
$("#chk").change(function() {
alert("triggered!");
});
答案 0 :(得分:4)
你可以用HTML编写你的PHP代码而不用双引号:
<?php if ($type == "main"): ?>
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>
</ul>
<?php else: ?>
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="locksmiths-<?= $areaname ?>.php">Home</a></li>
<li><a href="services-<?= $areaname ?>.php">Our Services</a></li>
<li><a href="services-upvc-repairs-<?= $area-name ?>.php">Upvc repairs</a></li>
<li><a href="services-<?= $area-name ?>-24hour.php">Emergency 24/7</a></li>
</ul>
<?php endif; ?>
答案 1 :(得分:2)
$area-name
无效变量名称,可能是拼写错误。
看到您的HTML我建议您浏览Alternative syntax for control structures
以下是提高可读性的其他方法,首先删除echo
。
<?php if ($type=="main" ): ?>
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>
</ul>
<?php else: ?>
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="locksmiths-<?php echo $areaname; ?>.php">Home</a></li>
<li><a href="services-<?php echo $areaname; ?>.php">Our Services</a></li>
<li><a href="services-upvc-repairs-<?php echo $areaname; ?>.php">Upvc repairs</a></li>
<li><a href="services-<?php echo $areaname; ?>-24hour.php">Emergency 24/7</a></li>
</ul>
<?php endif;?>
答案 2 :(得分:2)
更好的方法是使用三元运算符,这样您就不需要重复HTML了,只需更改链接:
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="<?= ($type == "main" ? 'index.php' : 'locksmiths-'.$areaname.'.php') ?>">Home</a></li>
<li><a href="<?= ($type == "main" ? 'services.php' : 'services-'.$areaname.'.php') ?>">Our Services</a></li>
<li><a href="<?= ($type == "main" ? 'upvc.php' : 'services-upvc-repairs-'.$areaname.'.php') ?>">Upvc repairs</a></li>
<li><a href="<?= ($type == "main" ? 'emergency.php' : 'services-'.$areaname.'-24hour.php') ?>">Emergency 24/7</a></li>
</ul>
或者,您可能希望将HTML中的数据抽象为数组,这样可以更轻松地添加更多链接或使链接更加动态(在数据库中)。
<?php
$links = [
[
'name' => 'Home',
'main' => 'index.php',
'area' => 'locksmiths-'.$areaname.'.php'
], [
'name' => 'Our Services',
'main' => 'services.php',
'area' => 'services-'.$areaname.'.php'
], [
'name' => 'Upvc repairs',
'main' => 'upvc.php',
'area' => 'services-upvc-repairs-'.$areaname.'.php'
], [
'name' => 'Emergency 24/7',
'main' => 'emergency.php',
'area' => 'services-'.$areaname.'-24hour.php'
],
];
?>
<ul class="footer-nav">
<h2>Links</h2>
<?php foreach ($links as $link): ?>
<li><a href="<?= ($type == "main" ? $link['main'] : $link['area']) ?>"><?= $link['name'] ?></a></li>
<?php endforeach ?>
</ul>
答案 3 :(得分:1)
这应该可以解决您的问题。 你忘了把别人说成是正确的陈述
<?php if ($type=="main" ) {
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>
</ul>';}
else {
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
<li><a href="services-'.$areaname.'.php">Our Services</a></li>
<li><a href="services-upvc-repairs-'.$areaname.'.php">Upvc repairs</a></li>
<li><a href="services-'.$areaname.'-24hour.php">Emergency 24/7</a></li>
</ul>';}
?>
答案 4 :(得分:1)
您有一些语法错误,请像这样使用
<?php if ($type=="main" ) {
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>
</ul>';
}else{
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
<li><a href="services-'.$areaname.'.php">Our Services</a></li>
<li><a href="services-upvc-repairs-'.$areaname.'.php">Upvc repairs</a></li>
<li><a href="services-'.$areaname.'-24hour.php">Emergency 24/7</a></li>
</ul>';
}
?>
确定变量名称,例如我将$area-name
替换为$areaname
答案 5 :(得分:1)
无需将回声置于回声中..请遵循以下更改..
<?php if ($type=="main" ) :
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>
</ul>';
else:
echo '
<ul class="footer-nav">
<h2>Links</h2>
<li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
<li><a href="services-.'$areaname.'.php">Our Services</a></li>
<li><a href="services-upvc-repairs-.'$area-name.'.php">Upvc repairs</a></li>
<li><a href="services-'.$area-name.'-24hour.php">Emergency 24/7</a></li>
</ul>'
endif;?>
答案 6 :(得分:1)
最短的代码
您只能echo
一次公共数据,就像我做的那样..
变量$area-name
无效......我将其转换为$ area_name。
<?php
echo '<ul class="footer-nav"><h2>Links</h2>'; //common data
if ($type=="main")
echo '
<li><a href="index.php">Home</a></li>
<li><a href="services.php">Our Services</a></li>
<li><a href="upvc.php">Upvc repairs</a></li>
<li><a href="emergency.php">Emergency 24/7</a></li>';
else echo '
<li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
<li><a href="services-'.$areaname.'.php">Our Services</a></li>
<li><a href="services-upvc-repairs-'.$area_name.'.php">Upvc repairs</a></li>
<li><a href="services-'.$area_name.'-24hour.php">Emergency 24/7</a></li>';
echo '</ul>';//common data
?>
答案 7 :(得分:0)
$areaname = 'wales';
$pages = [
'index.php' => 'Home',
'services.php' => 'Our Services',
'upvc.php' => 'Upvc repairs',
'emergency.php' => 'Emergency 24/7'
];
$area_urls = [
"locksmiths-$areaname.php",
"services-$areaname.php",
"services-upvc-repairs-$areaname.php",
"services-$areaname-24hour.php"
];
if($type != 'main')
$pages = array_combine($area_urls, $pages);
// Output menu
echo "<h2>Links</h2>\n<ul class='footer-nav'>\n";
foreach($pages as $link => $page)
printf("\t<li><a href='%s'>%s</a></li>\n", $link, $page);
echo "</ul>\n";