PHP If / Else Statement不使用嵌入式HTML代码

时间:2021-10-09 08:46:37

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

我正在尝试编写一个从数据库中获取图像的函数,并根据图像大小将它们显示在不同大小的边框内的网页上。当我尝试在浏览器中解析此代码时,出现意外的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>

6 个解决方案

#1


1  

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.

而且,这是非常混乱的,所有那些打开和关闭输出标签。只需在普通的PHP括号( )中执行整个操作并使用echo。

#2


0  

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>

#3


0  

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.

你只需要在if / else语句中使用“:”,就不需要在每一行上打开和关闭PHP标记。

#4


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>

#5


0  

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.

你需要逃避回声中的双引号。要在字符串中包含引号,您需要使用不同的引号,例如:echo“width ='280'”;或者转义引号,例如:echo“width = \”280 \“”否则,您将结束字符串,使PHP期望代码遵循。

Plus you seem to be trying to embed PHP code in PHP code.

另外,您似乎试图在PHP代码中嵌入PHP代码。

To include a PHP array value in a string you need to write it like this:

要在字符串中包含PHP数组值,您需要像这样编写它:

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.

编辑:只是添加我同意其他人,建立你的字符串的部分(例如。$ filename =“http://www.WEBSITE.co.uk/shop/cakes/ “)确实看起来更整洁,并且如果您以后必须更改某些内容,还可以使代码更容易维护。

#6


0  

Personally I think HEREDOCs make your code look neater and easier to fix, and avoids the quoting problems in your original....

就个人而言,我认为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>

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?

顺便说一句,你的循环在变量使用方面似乎有点混乱。如果您正在计算一次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;

#1


1  

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.

而且,这是非常混乱的,所有那些打开和关闭输出标签。只需在普通的PHP括号( )中执行整个操作并使用echo。

#2


0  

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>

#3


0  

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.

你只需要在if / else语句中使用“:”,就不需要在每一行上打开和关闭PHP标记。

#4


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>

#5


0  

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.

你需要逃避回声中的双引号。要在字符串中包含引号,您需要使用不同的引号,例如:echo“width ='280'”;或者转义引号,例如:echo“width = \”280 \“”否则,您将结束字符串,使PHP期望代码遵循。

Plus you seem to be trying to embed PHP code in PHP code.

另外,您似乎试图在PHP代码中嵌入PHP代码。

To include a PHP array value in a string you need to write it like this:

要在字符串中包含PHP数组值,您需要像这样编写它:

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.

编辑:只是添加我同意其他人,建立你的字符串的部分(例如。$ filename =“http://www.WEBSITE.co.uk/shop/cakes/ “)确实看起来更整洁,并且如果您以后必须更改某些内容,还可以使代码更容易维护。

#6


0  

Personally I think HEREDOCs make your code look neater and easier to fix, and avoids the quoting problems in your original....

就个人而言,我认为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>

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?

顺便说一句,你的循环在变量使用方面似乎有点混乱。如果您正在计算一次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;