Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

James Bennett

For Ray

In response to his question here, assuming your content is in a string called $content:

$num_paras = substr_count($content, '<p>'); 
switch ($num_paras) {
  case 1:
    $image = 'one.jpg';
    break;
  case 2:
    $image = 'two.jpg';
    break;
  case 3:
    $image = 'three.jpg';
    break;
  default:
    $image = '';
    break;
}

mod_rewrite rules to serve application/xhtml+xml

The following mod_rewrite rules will serve HTML files as application/xhtml+xml to supporting browsers.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]


Source: Mark Pilgrim, The Road to XHTML 2.0: MIME Types.

Textpattern form for Paypal shopping-cart integration

A site I've been working on has a retail area which uses Paypal's shopping cart; since the whole site is Textpattern powered, the necessary HTML for each item is generated by a form. This code is based on 1.0rc1 and using the rei_show_custom plugin. I know that in rc3 there's probably something built in to pull out arbitrary custom fields, so just substitute that.

To use, add each product as an article; in the first custom field put the name you want passed to the cart, and in the second custom field put the price of the item. Then make sure this is called by the article form:

<fieldset>
<legend>Buy this product:</legend>
<p>Our fine <txp:rei_show_custom /> is available for the low, low price of $<txp:rei_show_custom customid="2" />.</p>
<p><label for="os0">What sort of <txp:rei_show_custom /> would you like?</label></p>
<form action="https://www.paypal.com" method="post">
<p><input type="hidden" name="on0" value="variable" /><select name="os0" id="os0">
<option selected="selected" value="type1">Type 1</option>
<option value="type2">Type 2</option>
<option value="type2">Type 3</option>
</select>
</p>
<p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="yourbiz@yourdomain" />
<input type="hidden" name="item_name" value="<txp:rei_show_custom />" />
<input type="hidden" name="amount" value="<txp:rei_show_custom customid="2" />" />
<input type="image" id="add_to_cart" src="/path/to/your_cart_image" name="submit" alt="Add this item to your cart" />
<input type="hidden" name="add" value="1" />
</p>
</form>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="yourbiz@yourdomain" />
<input type="image" id="view_cart" src="/path/to/your_view_cart_image.png" name="submit" alt="View all items currently in your cart" />
<input type="hidden" name="display" value="1" />
</p>
</form>
</fieldset>


That'll display both an "add to cart" and a "view cart" button.

You'll want to fill in your own Paypal account address and product options, of course.

Improved admin_tools for Scoop

If you've ever built a site with Scoop, you know that all that messy hard-coded table-based HTML can be a pain. In the course of building several Scoop sites I've been reworking some of the common boxes to use cleaner, easily-styled, semantic markup, and this seems as good a place as any to post the code.

Here's a reworked admin_tools which outputs as an unordered list (and puts the "edit user" form in a definition list):

my $content;
my @tools = sort { $a->{pos} <=> $b->{pos} } values %{ $S->{ADMIN_TOOLS} };
foreach my $t (@tools) {
	if ( $S->have_perm($t->{perm}) ) {
		$content .= qq\|
	<LI><A href="|rootdir|/admin/$t->{tool}">$t->{menuname}</A></LI>\|;
	}
}
if ($S->have_perm('edit_user')) {
 $content .= qq{<LI>
 <FORM NAME="uedit" METHOD="GET" ACTION="|rootdir|/">
 <DL>
 <DT>Edit User:</DT>
 <DD>
 <INPUT TYPE="hidden" NAME="op" VALUE="user">
 <INPUT TYPE="text" NAME="nick" VALUE="" SIZE=10>
 <INPUT TYPE="SUBMIT" NAME="tool" VALUE="prefs">
 <INPUT TYPE="SUBMIT" NAME="tool" VALUE="info">
 </DD>
 </DL>
 </FORM>
 </LI>};
}
return '' unless $content;
return{content=>qq{<UL>$content</UL>}};

Improved user_box for Scoop

If you've ever built a site with Scoop, you know that all that messy hard-coded table-based HTML can be a pain. In the course of building several Scoop sites I've been reworking some of the common boxes to use cleaner, easily-styled, semantic markup, and this seems as good a place as any to post the code.

Here's a reworked user_box which outputs the tools menu as an unordered list:

my $content;
if ($S->{UID} > 0) {
  if ($S->have_perm('moderate')) {
    my ($nstories, $tstories) = $S->_count_new_sub();
    my $color = '';
    my $c_end = '';
    if ($nstories) {
       $color = '<strong style="color: #f00;">';
       $c_end = '</strong>';
    }
    $content = qq\|<ul>
    <li><a href="|rootdir|/modsub">Vote on Submissions</a> ($tstories/$color$nstories$c_end new)</li>\|;
  }

  if ($S->{TRUSTLEV} == 2 \|\| $S->have_perm('super_mojo')) {
    $content .= qq{<li><a href="|rootdir|/search?type=comment;hidden_comments=show">Review Hidden Comments</a></li>};
  }
  
  my $urlnick = $S->urlify($S->{NICK});
  my $diary_link = $S->{UI}->{VARS}->{use_diaries} ? 
		qq{
      <li><a href="|rootdir|/user/$urlnick/diary">Your Diary</a></li>
      <li><a href="|rootdir|/submitstory/Diary">New Diary Entry</a></li> 
		} : '';
	my $upload_link = ($S->{UI}->{VARS}->{allow_uploads} && ($S->have_perm('upload_user') \|\| $S->have_perm('upload_admin'))) ?
		qq{<li><a href="|rootdir|/user/$urlnick/files">Your Files</a></li>} : '';

	my $ad_link = $S->{UI}->{VARS}->{use_ads} ?
		qq{
      <li><a class="light" href="|rootdir|/user/$urlnick/ads">Your Advertisements</a></li>
	  <li><a href="|rootdir|/submitad">Submit Ad</a></li>
		} : '';

  $content .= qq\|
      <li><a href="|rootdir|/user/$urlnick">User Info</a></li>
      <li><a href="|rootdir|/user/$urlnick/comments">Your Comments</a></li>
      <li><a href="|rootdir|/user/$urlnick/stories">Your Stories</a></li>
      $diary_link
	  $ad_link
      $upload_link
      <li><a href="|rootdir|/user/$urlnick/prefs">User Preferences</a></li>
      <li><a href="|rootdir|/my/prefs/Interface">Display Preferences</a></li>
      <li><a href="|rootdir|/my/prefs/Comments">Comment Preferences</a></li>
      <li><a href="|rootdir|/logout">Logout $S->{NICK}</a></li></ul>\|;

    $title = $S->{NICK};
} else {
    $content = $S->{UI}->{BLOCKS}->{login_box};
    $content =~ s/|LOGIN_ERROR|/$S->{LOGIN_ERROR}/;
}
return {content => $content, title => $title };

Feed different CSS rules to IE5.0, 5.5 and 6.0

Sometimes you just have to resort to the hacks; this bit of code will let you feed three separate values for the same property to each of the three common flavors of IE on Windows:

* html whatevertherestofyourselectorwouldbe {
property/**/: value;
property: /**/ value;
p\roperty: value;
}


The first value will be applied by IE5.5, the second by IE5.0 and the third by IE6.0. The order is important; mix them up and it won't work. The "* html" on the selector ensures that only IE on Windows will see any of the values (since it mistakenly thinks there's a higher root element above html).

This was constructed from the table of CSS hacks at dithered.