Kimler Sidebar Menu
Kimler Adventure Pages: Journal Entries
Random Picture
Random Sidebar Image linked to Blog Entry
Concept
We wanted to add a random picture to our sidebar, but we took it a step further because we like to add 'depth'. A static picture is lifeless, so we linked the picture to the journal entry where the photo could be found.
As far as changes to b2evolution go, it turned out to be a fairly easy. There are, no doubt, other ways of accomplishing the same task, but we chose the method outlined below. The nice part about this modification is that it only affects one file in your custom skins folder (_main.php), which means that there's no fear of overwriting the modification next time you upgrade the software.
This modification was implemented and is currently (Feb05) used on this site, with version 0.9.0.11. (The "At Random" section of the sidebar, on the right.)
Method
What the code below does is create a temporary b2evo table, which we fill with blog entries that contain any <img> tags. We then count the total number of entries that contain images and randomly choose one. Then we count how many images are contained within the randomly chosen entry and randomly choose one of those. Then a link to the randomly chosen blog entry is made and the image is displayed. Formatting of the image (width, border, margin and so on) and <div> containing the image is done externally, via the CSS file.
Code
<?php
// create a temp table & fill it w/posts having pics (blog #7 only)
mysql_query('create temporary table evo_temp ( ID integer(4) auto_increment, post_id integer(4), content text, primary key (ID) )');
$imgBlogs = mysql_query ("insert into evo_temp (post_id,content) select ID,post_content from $tableposts join $tablecategories on (cat_ID=post_category) where cat_blog_ID=7 AND post_status='published' AND post_content like '%<img%yyy%'");
// get total num of blogs w/images & randomly select one
$r = mysql_query ("select * from evo_temp");
$maxEntry = mysql_num_rows($r);
$randomPost=mt_rand(1,$maxEntry);
$r = mysql_query ("select ID,post_id,content from evo_temp where ID=$randomPost");
$randomContent = mysql_fetch_row($r);
// find total # of pics & randomly select one
$numImgs = explode('<img class="yyy" ',"$randomContent[2]");
$randomImg = mt_rand(1,count($numImgs)-1);
$randomImg = $numImgs[$randomImg];
// extract pic location
$randomImg = substr($randomImg,strpos($randomImg,'yyy" src="')+10);
$randomImg = substr($randomImg,0,strpos($randomImg,'"'));
// add the link and the image
echo '<a class="randPicLnk" href="'.$baseurl.'/?p='.$randomContent[1].'" title=" Click to read this entry "><img src="'.$randomImg.'" alt="random image" /></a>';
?>
Implementation
To use this random image code, all you have to do is cut-n-paste it into your _main.php file, wherever you want the random image to show up, change the blog number to your own, add style="yyy" (immediately before src="[whatever]") in any image files you want to include.
Note: Items in blue, you must do. and Items in mint, are up to you.
Change the blog number to one (or more) of your blogs that you want to be considered for pulling random images. Near the bottom of the code, feel free to alter the class, title and alt text, in the <a href> tag, to your liking.
The yyy text is touchy and let me explain what it does. When I finished the intial trial, I tested it out and quickly realized that not ALL images were worthy of including in a random selection. Hmmm. How do I indicate a choice?
Unfortunately, the way the code works, images are chosen by inclusion, so I couldn't say, "Exclude these." Instead, I have to say, "Include all <img> tags with (and I chose) a bogus class name of 'yyy'." The reason I settled on "yyy" because I had chosen "yes" at first, but then realized that there are words that end in "y-e-s" that MIGHT cause a problem. Because I'd already done all the string placement using a 3-letter word, I just kept it at 3 letters and chose the most odd letter combination: "yyy".
IMPORTANT: Only pictures with an <img> tag of the following contstruction will be chosen as random pictures.
<img class="[other class names ]yyy" src="whatever... ...>
In the current configuration, the class attribute must immediately preceed the src attribute AND end with a bogus class name of "yyy". You may change "yyy" to be whatever you like, but keep in mind that if what you choose is longer (or shorter) than 3 characters, the +10 number in the substr function will need to be increased or decreased accordingly.
I have made a quick-tag toolbar button in the back office that generates my <img> tag code (and includes a style="yyy" by default). Easy.
Credits
The original b2evo forum post for this concept can be found HERE. I have a fellow b2evo user, by the name of ¥åßßå, to thank for getting me pointed in the right direction and helping me over a few rough spots. (If it weren't for him, I'd never have managed!) Thanks ¥åßßå! He's from Blackpool, England and his website can be found HERE.
If you use the code, I'd appreciate some feedback. If you have problems getting it going or have questions, feel free to drop me a line.

















I think we both learnt a lot about php and mysql to get this working
¥
seems to be a terrific script... but I can´t get it to work; see http://forums.b2evolution.net/viewtopic.php?t=3147&start=20
Can you help out?
Thanks
Daniel
I offered to help, but you were unwilling to allow me access.
While I understand your hesitation, I can only say that I'm here to help. If you cannot trust, I cannot help.
Cheers,
-stk