Forum Moderators: coopster

Message Too Old, No Replies

Nested PHP In If Statement?

         

steww

9:54 am on Aug 6, 2010 (gmt 0)

10+ Year Member



Hey there, hope you can help, Im trying to nest some PHP within an IF statement, and Im using WP:

------------------------------------------------------
<div class="homebox">
<?php $recent = new WP_Query("cat=3&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

<?php
$valid = yes;
?>

<?php
if ( $valid == yes ) {

<?php get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ); ?>

<b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b>

<?php the_content_limit(60, ""); ?>

} ?>

<div style="border-bottom:1px dotted #C0C0C0; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>

<?php endwhile; ?>

</div>
------------------------------------------------------

The current error Im getting is
"Parse error: syntax error, unexpected '<' " and this is refering to the line that starts: <?php get_the_image

Any ideas? Im driving myself nuts with this!

Many Thnaks

lammert

10:29 am on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<?php 
if ( $valid == yes ) {
<?php


Once you have opened a PHP code section with <?php, you can add PHP statements until you close it again with ?>. There is no need to add a second <?php open tag for every new line or nest level.

steww

11:13 am on Aug 6, 2010 (gmt 0)

10+ Year Member



Hey there many thanks for pointing me in the right direction here, I removed all opening <?php and closing ?> tages once within my curly brackets {}, the code is below.

Im now getting the same error (Parse error: syntax error, unexpected '<' " ) but relating to the following line:

<b><a href="the_permalink()" rel="bookmark">the_title();</a></b>


------------------------------------------------------
<div class="homebox">
<?php $recent = new WP_Query("cat=3&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

<?php
$valid = yes;
?>

<?php

if ( $valid == yes ) {

get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) );

<b><a href="the_permalink()" rel="bookmark">the_title();</a></b>

the_content_limit(60, "");

} ?>

<div style="border-bottom:1px dotted #C0C0C0; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>

<?php endwhile; ?>

</div>

-------------------------------

Any ideas?

Thanks

Matthew1980

11:18 am on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Personally I only like to break in and out of php when in a while/for/foreach and for bigger if/else clauses.

Other than that, the way as you will code will shape how you structure things, try to seperate php from HTML if you can as this will make editing a whole lot easier (do this by using includes :))

And yes, as lammert points out your missing the ?> after the curly brace of that if.

[EDIT]
Your still mixing the php and html this will definitely error look at the php in that piece of html and you will see where the error is :)

get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) );?>

<b><a href="the_permalink()" rel="bookmark"><?php echo the_title(); ?></a></b>

<?php
the_content_limit(60, "");


Cheers,
MRb

lammert

11:23 am on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi steww, you now removed too much :) The <b><a href... part is inline HTML. The PHP parser only recognizes it as such if there is an ending ?> before the in-line HTML starts.

In general:
  • All script code which must be parsed and executed by the PHP parser before it is sent to the browser must be encapsulated in a <?php and ?> pair.
  • All HTML code which should be copied unmodified to the browser must be outside <?php and ?>.

steww

11:38 am on Aug 6, 2010 (gmt 0)

10+ Year Member



Ahh OK, thanks both for going through that, I think I've eliminated the Parse error - as now have a new error! Although may be getting somewhere,

The new error is:
Parse error: syntax error, unexpected T_ENDWHILE

and it is reffering to this line "<?php endwhile; ?>"

My updated code is below:

----------------------------------------
<div class="homebox">
<?php $recent = new WP_Query("cat=3&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

<?php
$valid = yes;
?>

<?php
if ( $valid == yes ) {

get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ); ?>

<b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b>

<?php the_content_limit(60, ""); ?>

} ?>

<div style="border-bottom:1px dotted #C0C0C0; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>

<?php endwhile; ?>

</div>
------------------------------------------

Not sure why it would be questioning the endwhile?