I'm trying to write a function which takes images from a database and presents them on the webpage within different sized borders depending on the image size. When I try to parse this code within the browser, I get an unexpected T_else error. Can anybody explain where I've gone wrong? Thanks
<?
$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>
Since you're using alternate control structures, you need :
after each command, so:
<? if ($size[0] > $size[1]) ?>
should be
<? if ($size[0] > $size[1]): ?>
and
<? else ?>
should be
<? else: ?>
Moreover, this is pretty messy, all those opening and closing output tags. Just do the whole thing inside a normal PHP bracket (<?php
/ ?>
) and use echo
.
try this
<?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>
Better:
<?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>
You just need ":" in your if/else statement and you don't need to open and close PHP tag on every line.
<?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>
Personally I think HEREDOCs make your code look neater and easier to fix, and avoids the quoting problems in your original....
<?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>
Incidentally, your loop seems a little confused in terms of variable use. If you're working out the URL once, you don't need to do it a second time. Why are using _GET['id'] in one location and an ID from the database in another?
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;
You need to escape the double quotes in your echos. To include quotes inside a string you either need to use a different quote, eg: echo "width='280'";
or escape the quotes, eg: echo "width=\"280\""
Otherwise you are ending the string making the PHP expect code to follow.
Plus you seem to be trying to embed PHP code in PHP code.
To include a PHP array value in a string you need to write it like this:
echo "<a href='http://www.WEBSITE.co.uk/shop/cakes/".$cake['category_p']."'>
So for example, this line should look like:
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>";
EDIT: just to add that I agree with others, building sections of your string (Eg. $filename = "http://www.WEBSITE.co.uk/shop/cakes/<?=$cake['category_p']?>"
) does look neater, plus makes maintaining the code easier, should you have to change something later.