我正在尝试编辑PHP IF语句,但根据此在线验证器存在语法问题:
http://www.piliapp.com/php-syntax-check/
以下是代码:
<?php
if (strpos($row['caption'],'DIGITAL') !== false)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php
elseif (strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
这是我想要实现的目标:
如果“标题”以文本“DIGITAL”开头,则“添加到购物车”按钮应处于活动状态。 如果“标题不以文本开头”DIGITAL“并且”Price“不是NULL或0,那么购物车按钮应该是活动的。在所有其他情况下,它应该是非活动的。
以下是完整的源代码:
<?php include_once('admin/config/config.php'); ?>
<?php include_once('admin/libs/functions.php'); ?>
<?php $obj = new Functions(); ?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<link rel="stylesheet" type="text/css" href="reset.css">
<link rel="stylesheet" type="text/css" href="text.css">
<link rel="stylesheet" type="text/css" href="960.css">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<link rel="stylesheet" type="text/css" href="assets/css/nivo-lightbox.css">
<link rel="stylesheet" type="text/css" href="assets/css/default.css">
<script type="text/javascript" src="assets/js/jquery-2.1.1.js"></script>
<script type="text/javascript" src="assets/js/nivo-lightbox.js"></script>
<script type="text/javascript">
$(function(){
$(".view-img").nivoLightbox();
});
</script>
<script type="text/javascript" src="assets/js/shoppingcart.js"></script>
<script type="text/javascript" src="assets/js/general.js"></script>
<meta charset="utf-8">
<meta name="description" content="CENSORED Photography provides the best quality photography in the SENSORED Region. Whether you are looking for a photographer for a wedding, portrait, event or literally anything else, Let your story begin with SENSORED Photography. ">
<meta name="keywords" content="Photography, , Videography, , Photo, Portrait, Best">
<title>SENSORED Photography</title>
<script type="text/javascript">
$(function(){
$(".item_size").change(function(){
item_val = $(this).val();
n = $(this).attr("custval");
if(item_val != "" && item_val != null) {
$("#s"+n).removeAttr("disabled");
}
else {
$("#s"+n).attr("disabled","disabled");
}
});
});
</script>
</head>
<!-- oncontextmenu="return false;"-->
<body>
<div id="wrapper" class="container_12">
<div id="sidebar" class="grid_3">
<img src="images/sidebar_02.png" alt="sidebar"/>
<ul>
<li class="home"><a href="index.html">HOME</a></li>
<li class="about"><a href="about.html">ABOUT</a></li>
<li class="contact"><a href="contact.html">CONTACT</a></li>
<li class="gallery" style="color:#AD6FC4;"><a href="gallery.php">GALLERY</a></li>
</ul>
<p>© Copyright SENSORED 2013</p>
</div><!--end sidebar-->
<div id="content" class="grid_9">
<div id="contact" class="grid_3 alpha push_6">
</div><!--end contact-->
<div id="shop_cart">
<span class="shop_cart_quantity"></span> items
<span class="totalfront"></span><span class="shop_cart_total"></span>
<a href="#" id="viewcart" class="viewcart">View Details</a>
<a href="javascript:;" class="shop_cart_checkout">Checkout</a>
<a href="javascript:;" class="shop_cart_empty">Empty Cart</a>
</div>
<div id="cart_overview" style="background: #ffffff; position: absolute; top: 10px; left: 10px; display: none">
<div id="cart_overviewitems" class="shop_cart_items"></div>
<div style="clear:left"></div>
</div>
<div id="action_overview" style="position: absolute; top: 10px; left: 10px; display: none">
<!-- Product Added -->
<div style="clear:left"></div>
</div>
<div id="content2" class="omega grid_9 content2 newpages">
<?php if(isset($_GET['cat']) && $_GET['cat']!= NULL): ?>
<?php
$query = "SELECT * FROM categories WHERE id={$_GET['cat']}";
$data = $obj->select($query);
?>
<h1 class="normalh1 newpagesnew"><?php echo strtoupper($data['name']); ?></h1>
<?php
if($data['protected'] == '1'):
?>
<?php
$cid = $_GET['cat'];
$cookName = "cat".$cid;
if(!isset($_COOKIE[$cookName]) || $_COOKIE[$cookName] != "checked"):
?>
<div id="formWrap">
<form action="category-login.php" method="post" id="category_login">
<?php if(isset($_GET['mess']) && $_GET['mess'] == "err"): ?>
<p>Incorrect Password. Please try again.</p>
<?php else: ?>
<p>This gallery is protected. Please enter the password.</p>
<?php endif; ?>
<input type="hidden" name="cid" value="<?echo $cid; ?>" />
<div class="row">
<div class="input">
<input type="password" id="password" class="detail" name="password" placeholder="Password" required />
</div><!-- end .input -->
</div><!-- end .row -->
<div class="submit">
<input type="submit" id="submit" name="submit" value="login"/>
</div><!-- end .submit -->
</form>
</div>
<?php else: ?>
<?php
$sql = "SELECT * FROM categories WHERE parent_id = {$_GET['cat']} ORDER BY id DESC";
$res = $obj->selectAll($sql);
if($res):
?>
<?php $flag = 1; ?>
<?php foreach($res as $dat): ?>
<div class="grid_2 gal product">
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>">
<?php
$sql1 = "SELECT * FROM images WHERE category_id={$dat['id']} AND cover=1";
$dat1 = $obj->select($sql1);
if($dat1):
?>
<img src="gal/thumbs/<?php echo $dat1['filename']; ?>" />
<?php else:?>
<img src="images/nopic.png" />
<?php endif; ?>
</a>
<span>
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>"><?php echo $dat['name']; ?>
</a>
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if($flag == 1): ?>
<hr />
<?php endif; ?>
<?php
$n=1;
$query1 = "SELECT * FROM images WHERE category_id={$data['id']} ORDER BY id DESC";
$result = $obj->selectAll($query1);
if($result):
?>
<?php foreach($result as $row): ?>
<div class="grid_2 gal product">
<div class="product_item">
<a class="view-img" href="gal/<?php echo $row['filename']; ?>" data-lightbox-gallery="gallery1" <?php echo $row['caption'] != NULL ? 'title="'.$row['caption'].'"' : ''; ?>>
<img src="gal/thumbs/<?php echo $row['filename']; ?>">
</a>
<span class="item_name" style="display:none">Photo <?php echo $data['id']."-".$row['id']; ?></span>
<?php if($row['price'] != NULL && $row['price'] != '0'): ?><span style="margin-bottom: 0">Price: <i class="item_price"><?php echo $row['priceunit'].$row['price']; ?></i></span> <?php endif; ?>
<input type="hidden" class="item_quantity" value="1" style="width:20px;">
<?php if($row['price'] != NULL && $row['price'] != '0'): ?>
<?php
$sql = "SELECT * FROM sizes";
$res = $obj->selectAll($sql);
?>
<select name="item_size" class="item_size" custval="<?php echo $n; ?>" required>
<option value="">Select Size</option>
<?php foreach($res as $line): ?>
<option value="<?php echo $line['width'].' x '.$line['height']; ?>"><?php echo $line['width'].' x '.$line['height']; ?></option>
<?php endforeach; ?>
</select>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart" disabled />
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>
</div>
</div>
<?php $n++; ?>
<?php endforeach; ?>
<?php else: ?>
<?php if($flag != 1): ?>
No Image avilable.
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
<?php
$sql = "SELECT * FROM categories WHERE parent_id = {$_GET['cat']} ORDER BY id DESC";
$res = $obj->selectAll($sql);
if($res):
?>
<?php $flag = 1; ?>
<?php foreach($res as $dat): ?>
<div class="grid_2 gal product">
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>">
<?php
$sql1 = "SELECT * FROM images WHERE category_id={$dat['id']} AND cover=1";
$dat1 = $obj->select($sql1);
if($dat1):
?>
<img src="gal/thumbs/<?php echo $dat1['filename']; ?>" />
<?php else:?>
<img src="images/nopic.png" />
<?php endif; ?>
</a>
<span>
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>"><?php echo $dat['name']; ?>
</a>
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if($flag == 1): ?>
<hr />
<?php endif; ?>
<?php
$n=1;
$query1 = "SELECT * FROM images WHERE category_id={$data['id']} ORDER BY id DESC";
$result = $obj->selectAll($query1);
if($result):
?>
<?php foreach($result as $row): ?>
<div class="grid_2 gal product">
<div class="product_item">
<a class="view-img" href="gal/<?php echo $row['filename']; ?>" data-lightbox-gallery="gallery1" <?php echo $row['caption'] != NULL ? 'title="'.$row['caption'].'"' : ''; ?>>
<img src="gal/thumbs/<?php echo $row['filename']; ?>">
</a>
<span class="item_name" style="display:none">Photo <?php echo $data['id']."-".$row['id']; ?></span>
<?php if($row['price'] != NULL && $row['price'] != '0'): ?><span style="margin-bottom: 0">Price: <i class="item_price"><?php echo $row['priceunit'].$row['price']; ?></i></span> <?php endif; ?>
<input type="hidden" class="item_quantity" value="1" style="width:20px;">
<?php if($row['price'] != NULL && $row['price'] != '0'): ?>
<?php
$sql = "SELECT * FROM sizes";
$res = $obj->selectAll($sql);
?>
<select name="item_size" class="item_size" custval="<?php echo $n; ?>" required <?php if (strpos($row['caption'],'DIGITAL') !== false) {?>disabled="disabled"<?php }?>>
<option value="">Select Size</option>
<?php foreach($res as $line): ?>
<option value="<?php echo $line['width'].' x '.$line['height']; ?>"><?php echo $line['width'].' x '.$line['height']; ?></option>
<?php endforeach; ?>
</select>
<?php
if (strpos($row['caption'],'DIGITAL') !== false)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php
elseif (strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>
</div>
</div>
<?php $n++; ?>
<?php endforeach; ?>
<?php else: ?>
<?php if($flag != 1): ?>
No Image avilable.
<?php endif; ?>
<?php endif; ?>
<?php endif;?>
<?php else: ?><!-- post -->
<h1 class="normalh1">Invalid action.</h1>
<?php endif;?>
</div><!--end content2-->
</div><!--end content-->
</div><!--end wrapper-->
<script type="text/javascript" src="assets/js/docevents.js"></script>
</body>
</html>
<!-- Localized -->
有人可以建议我为什么无法让我的代码验证?它对我来说很好看。 验证器在第271行的CODE中指出“解析错误:语法错误,意外”{“。 错误解析CODE“
如果我运行它,代码将返回500内部服务器错误。
答案 0 :(得分:2)
错误属于elseif
条件
(strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
^^ ^
|missing ")" missing 2nd argument
missing 2nd argument
我甚至不知道你在这里想做什么。另外,strpos
永远不会返回布尔值true
,因此您在此行末尾的检查将永远不会成功。
我还建议在混合PHP和HTML时使用PHP的alternative syntax for control structure。像这样......
<?php if (strpos($row['caption'],'DIGITAL') !== false) : ?>
<input type="button" class="item_add" id="s<?= $n ?>" value="Add to cart"/>
<?php elseif (/* some other logic */) : ?>
<input type="button" class="item_add" id="s<?= $n ?>" value="Add to cart"/>
<!-- this line is exactly the same as the previous one?!? -->
<?php endif ?>
答案 1 :(得分:1)
您可以在http://www.piliapp.com/php-syntax-check/网址中查看以下代码进行验证并获取&#34;在CODE中未检测到语法错误&#34;消息。强>
您可以使用此代码代替您的代码。
<?php if (strpos($row['caption'],'DIGITAL') !== false){ ?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } elseif ($row['price'] != NULL && $row['price'] != '0' && strpos($row['caption'],'DIGITAL') !== true) {?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
答案 2 :(得分:0)
这使代码工作:
<?php if (strpos($row['caption'],'DIGITAL') !== false){ ?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php }
elseif ($row['price'] != NULL && $row['price'] != '0' && strpos($row['caption'],'DIGITAL') !== true) {?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>