my Qbee PHP script

I'm a member of the Quilting Bee and I use a very simple PHP script I wrote for displaying my quilt. I figure someone else might get some use out of it, so here it is for your coding pleasure:

PHP

<?php
// The relative URL to the directory containing my patches
$img_dir = '/wp-images/qbee/';

// The patches in my quilt, arranged in an array of arrays
// such that each inner array contains the link URL, the
// patch file name, and the title of the patch (e.g. a
// member's name).
$patches = array(
  array( 'http://www.theqbee.net', 'qbee.gif', 'Qbee home' ),
  array( '/', 'mine.gif', 'My patch' ),
  array( 'http://www.theqbee.net', '164.gif', 'My number' )
);

// The total patches in my quilt
$num_patches = count( $patches );

// Decide how many patches to show in a row.  The idea
// is to show the patches arranged in a square; that is,
// each side should have the same number of patches.
$num_per_side = ceil( sqrt( $num_patches ) );

// Since each patch is 40 pixels wide and 40 pixels tall,
// we can decide how wide to make the quilt container,
// based on that and how many patches we want per side.
$quilt_width = 40 * $num_per_side;

// Contain the quilt within a paragraph of the determined
// container width.  Also give it a position of 'relative' and
// an auto margin on either side so that it centers itself
// within the page.  Embedded CSS, run away!!1
echo '<p style="width: ' . $quilt_width . 'px; margin: 10px auto; position: relative;">';

// Go through the patches and stick them in the paragraph.
foreach ( $patches as $count => $patch ) {
  // Give more verbose variable names to each part of the
  // current patch.
  $url = $patch[0];
  $image_name = $patch[1];
  $title = $patch[2];

  // Define the whole path to the patch image.
  $image_url = $img_dir . $image_name;

  // Display each patch
  echo "<a href=\"$url\" title=\"$title\"><img src=\"$image_url\" alt=\"$title - $image_name\" width=\"40\" height=\"40\" /></a>";

  // Stick in a line break if we've now got the right number
  // of patches on this line.
  if ( ($count + 1) % $num_per_side == 0 )
    echo '<br />';
}

// Close our containing paragraph.
echo '</p>';

// Display the stats of how many patches are in the quilt.
echo "<p><em>$num_patches patches in my quilt</em></p>";
?>

I just embed the above code in my Qbee page. Since I use WordPress, I have to enable PHP for that page using the runPHP plugin.

This entry was posted in Programming and tagged , | Current music Frontier Psychiatrist by The Avalanches. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Posted 24 March 2008 at 9:06 AM | Permalink

    Oh neat script, Sarah!

  2. Posted 23 June 2008 at 7:35 PM | Permalink

    I used this on my trading card game log as well; it works great to display puzzles and the like! :) Thanks muchly.

  3. Posted 25 October 2008 at 1:53 PM | Permalink

    I was wondering if you could help me. I’ve installed the runPHP plugin, but the code above appears as text on my page. What have I done wrong?

  4. Posted 25 October 2008 at 4:49 PM | Permalink

    Hanna: with runPHP, even though you have the plugin installed and activated, it doesn’t automatically work on all pages/posts. You have to turn it on on individual posts. Look on the post page for a section titled “runPHP” and with a checkbox saying “run PHP code?”. Mine shows up below the entry textbox just above ‘Advanced Options’. You have to check that checkbox and save the entry again before runPHP will work on that post.

  5. Posted 25 October 2008 at 7:15 PM | Permalink

    I have checked it =( I even tried to uncheck it, save, check it and then save again… But it still doesn’t work.

  6. Posted 3 November 2008 at 6:15 AM | Permalink

    Hanna: Did you add the php tags at the beginning and end? If you type;

    at the end of everything, then it should work :)

  7. Posted 3 November 2008 at 6:17 AM | Permalink

    Argh! Sorry, it didn’t work. The codes are:

    [?php

    and

    ?]

    but replace [ and ] with html tags. If that made any sense…

  8. Posted 11 February 2010 at 11:41 AM | Permalink

    That is so nifty :D

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>