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
// 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.
8 Comments
Oh neat script, Sarah!
I used this on my trading card game log as well; it works great to display puzzles and the like! :) Thanks muchly.
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?
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.
I have checked it =( I even tried to uncheck it, save, check it and then save again… But it still doesn’t work.
Hanna: Did you add the php tags at the beginning and end? If you type;
at the end of everything, then it should work :)
Argh! Sorry, it didn’t work. The codes are:
[?php
and
?]
but replace [ and ] with html tags. If that made any sense…
That is so nifty :D