Bit of PHP help? Need to make + test a loop var

Miscellaneous Forums/General Discussion/Bit of PHP help? Need to make + test a loop var

Hi all. I really don't know much php, by I'm learning by tinkering. Anyway, I've got this blog which uses wordpress and the main index has this code (I've simplified it a bit):

<?php while (have_posts()) : the_post(); ?>

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<?php endwhile; ?>

As you can see there is a while loop. Well what I'd like to do is on the first iteration through the loop is bung out some extra HTML within the div class="entry" section.

So I figure the way to do this would be to declare a variable outside the loop and init it to 0 and then incremenet it just before the endwhile. Then I can test for it being 0 in the div section and output my html. Simple yeah?

Well anyway, my PHP is so poor I don't know how to declare the var, increment the var, and check the var for 0 and then conditionally output some HTML. If you know PHP this should be a Piece Hof P*ss.

Please advise :-) Huge thanks in advance!

greetings :) if you only need to do it once to can just trip a flag... something like:
<?php $bTripped=false; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="entry">

<?php if (!$bTripped) { ?>
    <?php $bTripped=true; ?>
    <!-- html here --> 
<?php } ?>

<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<?php endwhile; ?>


Ew... ugly ugly

Why not just do it -before- the loop?

Dampes8N: because I've simplified the code and there is more stuff in the loop that needs to be done first before the tripped flag outputs some HTML ;-)

gman: Top man! Looks like exactly what I need. Thanks. I'll try it out soon...

<?php $bTripped=false; ?>
 while (have_posts())
 the_post();
    print('<div class="entry">');
     if (!$bTripped) {
    $bTripped=true;
    print('
                   <!-- html here --> 
          ');
    }
    the_content('Read the rest of this entry &raquo;');
    print('</div>');
} ?>

why making a php script for each line??
use print('... html code herer ...'); on html need.

If it helps you can try out Remote File Engine, it works like Blitz's file functions though it has some limitations. You can't actually upload files(if you don't know what to add to the header (encoding type)), but it's easy to add post/get variables:
file% = OpenRemoteFile%("http://www.server.com/?get_variable=value1", 80, "post_variable=value2")

You can now either close the connection:
if file% then CloseRemoteFile(file%)

or fetch the answer of HTTP server:
If file%
    While Not EORF(file%) ; End Of Remote File
        if RemoteReadAvail(file) Then Print ReadRemoteLine$(file%)
    Wend
    CloseRemoteFile(file%)
EndIf


In PHP you can fetch the variables like this:
$_GET['get_variable'] ; is equal to "value1"
$_POST['post_variable'] ; is equal to "value2"



Ew... ugly ugly


definately :) but i was sticking to the example syntax provided. didnt want to change the syntax any on him if hes not familiar with PHP.

hope it worked out for you grey :)

I didn't know about print.

Gman: where you have written html here, do I need to place it inside the comments <!-- and --> or do I need replace the comments as well?

remove the <!-- --> comment and replace it with your HTML. something like:
<?php $bTripped=false; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="entry">

<?php if (!$bTripped) { ?>
    <?php $bTripped=true; ?>
    <div id="my_stuff">
        <strong>This is really fun :)</strong>
    </div>
<?php } ?>

<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<?php endwhile; ?>


That's great, works brilliantly. Thanks again!

on your codes are at least 7 php scripts!?
just use one php script and
pirint('<div id="...">haha</div');


What he means is you don't have to open and close each line with <?php ?>

You just have to close the PHP block when you do something that isn't php... for example.. You want to put in regular old HTML.. and not "Echo" the html or "Print" it.

<?php ?> on every line is a complete mess!

php should be structured like "real" code - notice the similarities? - Feel Free to steal this code, all it does it provide pagination like you see on digg.com

<?php

Class Paginate
{
	var $total_pages;
	var $limit;
	var $ajax_component;
	var $sort_method;
	var $page;
	var $tags;
	var $adjacents = 3;
	
	function GetLinks()
	{
		if ($this->page == 0) $this->page = 1;
		$prev 		= $this->page - 1;
		$next 		= $this->page + 1;
		$lastpage 	= ceil($this->total_pages/$this->limit);
		$lpm1		= $lastpage - 1;
		
		$pagination = "";
		
		if($lastpage > 1)
		{	
			$pagination .= "<div class="pagination">";
			//previous button
			if ($this->page > 1) 
				$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$prev."', '".$this->tags."')">&laquo; prev</a>";
			else
				$pagination.= "<span class="disabled">&laquo; prev</span>";	
			
			//pages	
			if ($lastpage < 7 + ($this->adjacents * 2))	//not enough pages to bother breaking it up
			{	
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $this->page)
						$pagination.= "<span class="current">$counter</span>";
					else
						$pagination.= "<a href="javascript://"  onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$counter."', '".$this->tags."')">$counter</a>";
				}
			}
			elseif($lastpage > 5 + ($this->adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($this->page < 1 + ($this->adjacents * 2))		
				{
					for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++)
					{
						if ($counter == $this->page)
							$pagination.= "<span class="current">$counter</span>";
						else
							$pagination.= "<a href="javascript://"  onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$counter."', '".$this->tags."')">$counter</a>";
					}
					$pagination.= "...";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$lpm1."', '".$this->tags."')">$lpm1</a>";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$lastpage."', '".$this->tags."')">$lastpage</a>";		
				}
				//in middle; hide some front and some back
				elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2))
				{
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'1', '".$this->tags."')">1</a>";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'2', '".$this->tags."')">2</a>";
					$pagination.= "...";
					for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
					{
						if ($counter == $this->page)
							$pagination.= "<span class="current">$counter</span>";
						else
							$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$counter."', '".$this->tags."')">$counter</a>";					
					}
					$pagination.= "...";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$lpm1."', '".$this->tags."')">$lpm1</a>";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$lastpage."', '".$this->tags."')">$lastpage</a>";		
				}
				//close to end; only hide early pages
				else
				{
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'1', '".$this->tags."')">1</a>";
					$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'2', '".$this->tags."')">2</a>";
					$pagination.= "...";
					for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $this->page)
							$pagination.= "<span class="current">$counter</span>";
						else
							$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$counter."', '".$this->tags."')">$counter</a>";					
					}
				}
			}
			
			//next button
			if ($this->page < $counter - 1) 
				$pagination.= "<a href="javascript://" onclick="ajax_component['".$this->ajax_component."'].GetData('".$this->sort_method."'	,'".$next."', '".$this->tags."')">next &raquo;</a>";
			else
				$pagination.= "<span class="disabled">next &raquo;</span>";
				
			$pagination.= "</div>\n";
			return $pagination;
		}
	}
}

?>


definately not looking for a syntax war :) there are numerous ways to do the same thing here and DCs code is just fine. i was simply sticking to what i felt was syntax Grey was familiar with based on what was provided. i didnt want to use print or echo because i didnt know what HTML editor he is using. also Grey mentioned that there was a lot more to the loop. compacting it down into a better formatted block may cause him to break it back out anyway. while it is not how i would have written it, i felt it was the friendliest given the situation.

No one is saying your code was bad. I understand exactly why you did it. But it is also good to point out an easier to read way to do it... Or at least to ensure he understands that he doesn't need it on each line. :)

Interesting thanks and gman did good keeping it the same way so I got it.

Anyway that code is actually from the default wordpress template! It's not mine, it's like that all the way through. Mabe there is a reason they did it like that?

Wow.. the only reason I can think of to do that is so people who don't know PHP won't fooker things up if they add lines in the middle and add in HTML...

That.. Or they WANTED it to be unreadable... or maybe some software auto-gens it like that cause it sucks.

the only reason I can think of to do that is so people who don't know PHP won't fooker things up if they add lines in the middle and add in HTML...
ha, yeah like me.