我试图编写一个从数据库中获取图像的功能,并根据图像大小将它们显示在不同大小的边框内的网页上。当我尝试在浏览器中解析此代码时,出现意外的T_else错误。任何人都可以解释我哪里出错了吗?感谢
<?
$cakepicsql = mysql_query("SELECT * from cakes WHERE category = '".$cake['category']."' ORDER BY rand() LIMIT 1") or die(mysql_error());
while($cakepic = mysql_fetch_array( $cakepicsql )) {
$image_path = "http://WEBSITE/products/";
$filename = $image_path + $_GET['id'] + "-1.jpg";
$size = getimagesize($filename);
if ($size[0] > $size[1])
echo "<a href="http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"><img src="http://www.WEBSITE.co.uk/products/<?=$cakepic['id']?>-1.jpg" alt="<?=$cake['name']?>" width="280" height="274" border="0" class="rounded-image" /></a> ";
else
echo "<a href="http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"><img src="http://www.WEBSITE.co.uk/products/<?=$cakepic['id']?>-1.jpg" alt="<?=$cake['name']?>" width="202" height="274" border="0" class="rounded-image" /></a>";
endif;
} ?>
</td>
</tr>
答案 0 :(得分:1)
由于您正在使用备用控制结构,因此在每个命令后需要:
,因此:
<? if ($size[0] > $size[1]) ?>
应该是
<? if ($size[0] > $size[1]): ?>
和
<? else ?>
应该是
<? else: ?>
此外,这是非常混乱的,所有那些打开和关闭输出标签。只需在普通PHP括号(<?php
/ ?>
)内完成整个操作并使用echo
。
答案 1 :(得分:0)
试试这个
<?php
$cakepicsql = mysql_query("SELECT * from cakes WHERE category = '".$cake['category']."' ORDER BY rand() LIMIT 1") or die(mysql_error());
while($cakepic = mysql_fetch_array( $cakepicsql )) {
$image_path = "http://WEBSITE/products/";
$filename = $image_path + $_GET['id'] + "-1.jpg";
$size = getimagesize($filename);
if ($size[0] > $size[1]) {
<a href='http://www.WEBSITE.co.uk/shop/cakes/<?php echo $cake["category_p2"];?>'><img src="http://www.WEBSITE.co.uk/products/<?php echo $cakepic['id']; ?>-1.jpg" alt="<?php echo $cake['name'];?>" width="280" height="274" border="0" class="rounded-image" /></a>
} else{
<a href="http://www.WEBSITE.co.uk/shop/cakes/<?php echo $cake['category_p'];?>"><img src="http://www.WEBSITE.co.uk/products/<?php echo $cakepic['id'];?>-1.jpg" alt="<?php echo $cake['name'];?>" width="202" height="274" border="0" class="rounded-image" /></a>
}
}
?>
</td>
</tr>
答案 2 :(得分:0)
更好:
<?php
$cakepicsql = mysql_query("SELECT * from cakes WHERE category = '".$cake['category']."' ORDER BY rand() LIMIT 1") or die(mysql_error());
while($cakepic = mysql_fetch_array( $cakepicsql )) {
$image_path = "http://WEBSITE/products/";
$filename = $image_path + $_GET['id'] + "-1.jpg";
$size = getimagesize($filename);
if ($size[0] > $size[1]):
?>
<a href="http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"><img src="http://www.WEBSITE.co.uk/products/<?=$cakepic['id']?>-1.jpg" alt="<?=$cake['name']?>" width="280" height="274" border="0" class="rounded-image" /></a>
<?php
else:
?>
<a href="http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"><img src="http://www.WEBSITE.co.uk/products/<?=$cakepic['id']?>-1.jpg" alt="<?=$cake['name']?>" width="202" height="274" border="0" class="rounded-image" /></a>
<?php endif;
}
?>
</td>
</tr>
你只需要&#34;:&#34;在if / else语句中,您不需要在每一行上打开和关闭PHP标记。
答案 3 :(得分:0)
<?php
$cakepicsql = mysql_query("SELECT * from cakes WHERE category = '".$cake['category']."' ORDER BY rand() LIMIT 1") or die(mysql_error());
while($cakepic = mysql_fetch_array( $cakepicsql )) {
$image_path = "http://WEBSITE/products/";
$filename = $image_path + $_GET['id'] + "-1.jpg";
$size = getimagesize($filename);
if ($size[0] > $size[1])
echo "<a href='http://www.WEBSITE.co.uk/shop/cakes/".$cake['category_p']."'><img src='http://www.WEBSITE.co.uk/products/'".$cakepic['id']."-1.jpg' alt='".$cake['name']."' width='280' height='274' border='0' class='rounded-image' /></a>";
else
echo "<a href='http://www.WEBSITE.co.uk/shop/cakes/".$cake['category_p']."'><img src='http://www.WEBSITE.co.uk/products/".$cakepic['id']."-1.jpg' alt='".$cake['name']."' width='202' height='274' border='0' class='rounded-image' /></a>";
}
?>
</td>
</tr>
答案 4 :(得分:0)
就我个人而言,我认为HEREDOC使您的代码看起来更整洁,更容易修复,并避免原始引用问题.......
<?php
$sql = <<<EOF
SELECT * from cakes
WHERE category = '{$cake['category']}'
ORDER BY rand() LIMIT 1
EOF;
$cakepicquery = mysql_query($sql) or die(mysql_error());
while($cakepic = mysql_fetch_array( $cakepicquery )) {
$image_path = "http://WEBSITE/products/";
$filename = $image_path + $_GET['id'] + "-1.jpg";
$size = getimagesize($filename);
$height = 274;
$width = ($size[0] > $size[1]) ? 280 : 202;
echo <<<EOF
<a href="http://www.WEBSITE.co.uk/shop/cakes/{$cake['category_p']}">
<img src="http://www.WEBSITE.co.uk/products/{$cakepic['id']}-1.jpg" alt="{$cake['name']}" width="{$width}" height="{$height}" border="0" class="rounded-image" />
</a>
EOF;
} ?>
</td>
</tr>
顺便提一下,你的循环在变量使用方面似乎有点混乱。如果您正在编制URL一次,则无需再次执行此操作。为什么在一个位置使用_GET ['id']而在另一个位置使用数据库中的ID?
echo <<<
<a href="http://www.WEBSITE.co.uk/shop/cakes/{$cake['category_p']}">
<img src="{$filename}" alt="{$cake['name']}" width="{$width}" height="{$height}" border="0" class="rounded-image" />
</a>
EOF;
答案 5 :(得分:0)
您需要在回声中转义双引号。要在字符串中包含引号,您需要使用不同的引号,例如:echo "width='280'";
或转义引号,例如:echo "width=\"280\""
否则您将结束字符串,使PHP期望代码遵循。
另外,您似乎正在尝试在PHP代码中嵌入PHP代码。
要在字符串中包含PHP数组值,您需要像这样编写它:
echo "<a href='http://www.WEBSITE.co.uk/shop/cakes/".$cake['category_p']."'>
因此,例如,此行应如下所示:
echo "<a href='http://www.WEBSITE.co.uk/shop/cakes/".$cake['category_p']."'><img src='http://www.WEBSITE.co.uk/products/".$cakepic['id']."-1.jpg' alt='".<?=$cake['name']."' width='280' height='274' border='0' class='rounded-image' /></a>";
编辑:只是添加我同意其他人,构建字符串的部分(例如$filename = "http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"
)确实看起来更整洁,并且如果您以后必须更改某些内容,则会使代码更容易维护。