couple code ditties

1st Mar 2006 stk

Back to Top Link

Some content gets long. Would be nice to incorporate a "back to top" link. I came up with this one, which seems to work, but I'm wondering if something more elegant and/or simple could be used.

All mods in _main.php

<body id="top">  [color=blue]<--- bookmark at page top[/color]

[color=blue](then, at bottom of the page, usually with the navigation control[/color]
<a class="top" href="#" data-code="?php 
  if ($_SERVER['QUERY_STRING'] == "" ) { echo '/'; } else {
	    echo '/?'.str_replace("&","&amp;",$_SERVER['QUERY_STRING']); }
			?>#top" title=" top of page ">[ Back to Top ]</a>
<?php echo '<a href="'.$ReqURI.'#top">[ '.T_('Back to top').' ]</a>'; ?>

Greyed out Navigation Controls

I've had a pet peeve since I first signed up with b2evo. It's got to do with the navigation controls "<previous || next>". When you're at the end (or the beginning) ... there isn't a "" and then the centered thing bounces around and all you see is the other one.

Anyway ... I solved it on randsco (sidebar) by underlaying an image, not using nav_links(), and previous_posts_link() and next_posts_link() instead. I then positioned the image pixel-perfect, which took a LOT of work ... just so the thing always shows "<prev" and/or "next>", when you're at the end or the beginning AND the other ones stay rock solid, alone or together.

So ... the short of it is, I found A way to test for presence of either the end or the beginning and do it all with TEXT, instead of pixel-perfect images. I quite like it. It's deployed on randsco at the BOTTOM of the page.

The code:

  <div id="bNav">
  <?php [color=blue]if( empty($p) && ($paged < 2) ) {[/color]// this is not the inverse of [color=green]if ( empty( $p ) && ( $paged > 1 ) ) {[/color] ;)
				echo '<span>&nbsp;&lt; Newer Entries&nbsp;</span>'; 
		  } else {				
				previous_posts_link('< Newer Entries '); } 
	?>
|
 	<?php $max_page=0;
	      if (!$max_page) $max_page = $MainList->get_max_paged();
	      if (!$paged) $paged = 1;
	      $nextpage = intval($paged) + 1;
					//echo $max_page;
					//echo $nextpage;
	      if ($nextpage == $max_page+1) {
				    echo '<span>&nbsp;Older Entries &gt;&nbsp;</span>';
       } else { next_posts_link(' Older Entries >'); } ?>
</div>

You have an error in your logic (blue above) and you can compress all the last bit ;)

  <div id="bNav">
  <?php if ( empty( $p ) && ( $paged > 1 ) ) {
				previous_posts_link('< Newer Entries ');
		  } else {				
				echo '<span>&nbsp;&lt; Newer Entries&nbsp;</span>';  } 
	?>
|
 	<?php $max_page = $MainList->get_max_paged();
	      $nextpage = ( !$paged ? 2 : intval( $paged ) + 1 );
	      if ( $nextpage > $max_page ) {
				    echo '<span>&nbsp;Older Entries &gt;&nbsp;</span>';
       } else { next_posts_link(' Older Entries >'); } ?>
</div>

Is there a way to do either, better?

 
 
 
 

Comments

Anonymous
2nd Mar 2006
I've edited your post as <amcode> doesn't work in comments (and I'm going to wait until at least beta before I bother implementing it).

Your link ones obvious so I won't bother explaining it.

Your logical error with this line if ( empty( $p ) && ( $paged > 1 ) ) { what this line says is "if p is empty and paged is greater than one", the inverse of which is "if p is not empty or paged is less than two". I just did it the easy way and used the original "if" statement and reversed the action to take ;)

The compressing of the code wasn't just to be smart, it was to remove redundancy.

$max_page=0; // you've just set max_page to zero
if (!$max_page) // and then you test for it being zero
$max_page = $MainList->get_max_paged();// and then you set $max_page again, which makes both of them redundant ;)

if (!$paged) $paged = 1;
$nextpage = intval($paged) + 1; // far neater to do all this at once, and improves readability

if ($nextpage == $max_page+1) { // you're making the poor alu do a pointless extra bit of maths ;)

¥
 
 

Recent Comments

     
     

    Archives