9 Replies - 465 Views - Last Post: 08 March 2013 - 02:12 PM Rate Topic: -----

#1 bribin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-September 12

Php for loop with li listing

Posted 07 March 2013 - 07:08 AM

<div class="pagination">
    <ul>
   
        <?php
	//Pagination Numbers
	for($i=1; $i<=$pages; $i++)
	{	 ?>
	
    <li> <?php echo $i;?></li>
    <?
	}
	?>
        
        
    </ul>
</div>


$end error geting while runing
Is This A Good Question/Topic? 0
  • +

Replies To: Php for loop with li listing

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Php for loop with li listing

Posted 07 March 2013 - 07:13 AM

There doesn't seem to be anything wrong there. That snippet should run fine. (Assuming short tags are enabled on the server.)

Can you elaborate on the error you are getting. It helps to see the entire error message, not just a vague rephrasing.
Was This Post Helpful? 0
  • +
  • -

#3 bribin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-September 12

Re: Php for loop with li listing

Posted 07 March 2013 - 07:17 AM

View PostAtli, on 07 March 2013 - 07:13 AM, said:

There doesn't seem to be anything wrong there. That snippet should run fine. (Assuming short tags are enabled on the server.)

Can you elaborate on the error you are getting. It helps to see the entire error message, not just a vague rephrasing.

thnks for the replay

Parse error: syntax error, unexpected $end in D:\wamp\www\sama\administrator\catagories.php on line 151

this is my error
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Php for loop with li listing

Posted 07 March 2013 - 08:06 AM

Ok, I see. The code you posted does not explain why that would be happening. It's syntax is perfectly sound.

This type of error is caused by misaligned code blocks; missing end brackets or even missing quote marks. Go through the code and make sure all open brackets have a matching end bracket. It will help enormously if your code is properly indented and you are using an IDE that will highlight the code and highlight end brackets when you click opening brackets. (Like Netbeans or PHPStorm.)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3247
  • View blog
  • Posts: 10,890
  • Joined: 12-December 12

Re: Php for loop with li listing

Posted 07 March 2013 - 08:14 AM

PHP short tags usually start with <?= rather than <?. (I believe PHP 5.4 enables both.) Personally, I don't use them.

Added: Actually, to correct myself, both <? and <?= (in place of <?php echo ..) should work from PHP 5.4, or if short-tags are enabled.

This post has been edited by andrewsw: 07 March 2013 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Php for loop with li listing

Posted 07 March 2013 - 09:01 AM

You got it half right, andrewsw.

The <? tag will only work if short tags are enabled, regardless of what version you are using. With PHP 5.4, however, the <?= tag is enabled regardless of the short tags settings.

The reason for that is that the <? tag is a valid XHTML tag, so using it for PHP blocks became confusing when XHTML became somewhat popular. The <?= tag doesn't suffer from that problem.

I'd still not recommend using either, unless you are absolutely certain under which version your code will be running, and your code isn't going to be shared with others. Otherwise it's safer to use the full <?php tags.
Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Php for loop with li listing

Posted 07 March 2013 - 09:09 PM

<?php
// You might read this from a file:
$list =<<<here
<div class="pagination">
    <ul>
    {items}
    </ul>
</div>
here;
// This too:
$item ='<li>{item}</li>';

$pages = 5;

$replace1 = '{item}';
$replace2 = '{items}';

$items = '';
for($i = 1; $i<  $pages; $i++) {
	$items .= str_replace($replace1, $i, $item);
}

echo str_replace($replace2, $items, $list);


This style is more readable because the languages can be read one at a time.
Was This Post Helpful? 0
  • +
  • -

#8 Koyume  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 89
  • Joined: 19-February 13

Re: Php for loop with li listing

Posted 08 March 2013 - 01:24 PM

If you want to use what you have, this is what I usually do.

	<div class="pagination">
02	    <ul>
03	    
04	        <?php
05	    //Pagination Numbers
06	    for($i=1; $i<=$pages; $i++)
07	    {
08	    
09	    echo "<li> $i</li>";
10	    
11	    }
12	    
13	         ?>
14	         
15	    </ul>
16	</div>


That always worked for me. But the <li>$i</li> can be replaced with <li>".$1."</li> as well.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3247
  • View blog
  • Posts: 10,890
  • Joined: 12-December 12

Re: Php for loop with li listing

Posted 08 March 2013 - 01:31 PM

View PostAtli, on 07 March 2013 - 09:01 AM, said:

You got it half right, andrewsw.

The <? tag will only work if short tags are enabled, regardless of what version you are using. With PHP 5.4, however, the <?= tag is enabled regardless of the short tags settings.

The reason for that is that the <? tag is a valid XHTML tag, so using it for PHP blocks became confusing when XHTML became somewhat popular. The <?= tag doesn't suffer from that problem.

I'd still not recommend using either, unless you are absolutely certain under which version your code will be running, and your code isn't going to be shared with others. Otherwise it's safer to use the full <?php tags.

I suppose this is why I never use short tags :). [It is also handy to be able to search for 'php' (if embedded in HTML).]
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Php for loop with li listing

Posted 08 March 2013 - 02:12 PM

View Postandrewsw, on 08 March 2013 - 04:31 PM, said:

It is also handy to be able to search for 'php' (if embedded in HTML).]

That need is why I don't embed, and why I think beginners should never do it. When they do it's only a matter of time before they get messed up by the difference between client and server.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1