How to show random picture?

2

Comments

  • Then all you need is this snippet from the module section in the stopdesign theme (replaces right module section)

    <div class="module">
    <h2>Random Image</h2>

    <?php
    $randomImage = getRandomImages();
    $randomImageURL = getURL($randomImage);
    print "<center><img src='".$randomImage->getSizedImage('120')."' alt='".$randomImage->getTitle()."'></center>";
    ?>

    </div>
  • Don't forget to use a require at the top to call the function. <?php require ('customfunctions.php'); ?>

    That way, any upgrades to zenphoto won't wipe out your custom work, and you're not modifying any of the core code.

    Craig.
  • Daxeno Member
    Thank You Very Much ThinkDreams... but there seems to be a problem with the database/MySQL. it says : MySQL Query ( SELECT images.filename, images.title, albums.folder FROM images INNER JOIN albums ON images.albumid = albums.id ORDER BY RAND() LIMIT 1 ) Failed. Error: Table 'daxeno.images' doesn't exist
  • daxeno-

    It could be that my query is not structured for the way your db is set up. Are you using a prefix (ie zen_ ) ?
  • Daxeno Member
    yes. im using the prefix zen.
  • Daxeno-

    You might try looking in earlier posts in this thread. I'm by no means an expert, so it may be that script is failing since I set mine up with a separate MySql instance without the zen prefix. Anybody on this thread care to illuminate a bit better that I? I'll see what else I can find out online as well.

    Thinkdreams.
  • Daxeno-

    Try:

    1. There is a function in functions-db.php that looks like this:

    function prefix($tablename) {
    return zp_conf('mysql_prefix').$tablename;
    }

    2. Using a similar syntax: prefix("tablename") put that in place of images in the query. You can get a good idea of what a query might look like by looking in classes.php.

    Does that make sense?

    Thinkdreams.
  • Daxeno Member
    TYVM again ThinkDreams. all i did is reset the DB and removed those prefixes and it works. :D

    Daxeno
  • Daxeno Member
    The only problem i encountered now is that i cant click on the image to view the whole photo. :(
  • Daxeno Member
    Anyway ive fixed it, Check this code : <?php
    $randomImage = getRandomImages();
    $randomImageURL = getURL($randomImage);
    print "<center><img src='".$randomImage->getSizedImage('120')."' alt='".$randomImage->getTitle()."'></center>";
    ?>
  • You're most welcome Daxeno.
  • Can either of you explain a bit more in detail what I need to do to fix the mysql error that daxeno was getting

    "Error: Table 'akclimber.images' doesn't exist"
    I'm getting the same error but don't quite understand how to remedy it.

    Also, should my table prefixes be _zen? Currently they are zp_

    Thanks for your help and for creating this function!
  • climberusa-

    first off, i can't take credit for the scripts. they were all obtained mostly from this thread.

    daxeno rebuilt his db. by default the db uses a prefix of zen_ (i think). I usually set up a separate db, but some people don't have that luxury.

    I believe you have to use the prefix function to place the prefix before every query in the sql statement. you could hardcode it to i suppose, but that might lead to problems if you ever share your scripts out to others.

    anybody from the dev team want to comment? am i close, or way off?
  • Daxeno Member
    climberusa, all you need to do is remove the prefix zp_. But doing this, you need to reset your database.
    Go to config.php, and edit it, look for this $conf['mysql_prefix'] = "zp_", then just delete the said prefix. it will then look like this $conf['mysql_prefix'] = "". Save it afterwards.
    Now to reset you DB, delete your database from your phpmyadmin. place the setup.php back to the folder zen, then reinstall it.
  • Thanks! I got it working. Would it be easy to have three random, but different photo's? I tried the obviou copy/paste but it just gives me the same three images each time. This is very cool.
  • Skwid Member
    The prefix problem has been fixed in the Thinkdreams Theme release. It uses the prefix() function to query the right tables.
  • My wordpress and Zenphoto use the same database but different prefix, so I can easily make my plugin, and insert the following code into sidebar.php winthin folder of /wp-content/themes/default/.

    -----------------------------------------
    <?php
    function showrandompic()
    {
    global $wpdb;
    $sql = "SELECT zen_images.filename, zen_images.title, zen_albums.folder FROM zen_images INNER JOIN zen_albums ON zen_images.albumid = zen_albums.id ORDER BY RAND() LIMIT 1";
    $comment = $wpdb->get_row($sql);
    if ( !$comment )
    return;
    else {
    echo '<img src="http://lidan.info/photo/cache/'.$comment->folder.'/'.$comment->filename.'_100_cw85_ch85.jpg" style="padding:1px;border:1px solid gray" />';
    }
    }
    showrandompic();
    ?>
    -------------------------------------------
    there are 3 parameters you should pay attention to.
    1.zen_
    2.http://lidan.info/photo/
    3._100_cw85_ch85.jpg

    demo: http://lidan.info (see leftbar)

    Good luck.
  • up1m, thanks for your code. I'm also trying to get a random image from my zenphoto albums to appear in my sidebar, but my wordpress and zenphoto installs DON'T use the same db.

    Is this possible? Most of the posts above offer instructions of how to align wordpress to the zenphoto code, changing dbs en route, but not vice versa i.e. switching dbs from within the wordpress file structure.

    I've tried to `require_once(--hostdetails--/zen/functions.php)` but this causes a fatal error message.
  • Hey - I gave up with going the hard way round, and installed my tables in the same db. Now it works great. Check out The Daily Pixel at http://www.strangerpixel.com. Thanks again up1m!
  • As an aside, you can also use Zenshow to integrate random image functionality into wordpress, and is available on the wiki at:

    http://www.zenphoto.org/trac/wiki/ZenphotoPlugins

    It works fine on my wordpress install, which is 2.1 currently.
  • Hi all

    Pardon my less learned abilities but trying to get a random image to come up in a static HTML page but keep getting mysql error messages - and really looking though the forum I am not really sure what code I should be using - any help appreciated.

    Cheers
  • I see this thread is quite old, but I managed to get the random.php script working.

    The problem is, I can not call the script from within another page using either javascript or img src , but if you click on the box empty box that shows up. It will display the image. I think it is almost working, can someone please tell me what I am doing wrong.

    I have tried calling the random.php like this:
    img src=""http://www.foolishcat.com/gallery/zen/random.php"">

    and like this:
    <script type="text/javascript" src="http://www.foolishcat.com/gallery/zen/random.php"></script>

    I would really like to use the img src - I think the problem is the last part of the script with the url encode. - here is the random.php script I am using:

    <?php
    require_once('classes.php');

    $imageSize = (isset($_GET['s']) ? $_GET['s'] : 200);
    $randomImage = getRandomImage();
    $imageLinkURL = getImageLinkURL($randomImage);

    print "<img src='".$randomImage->getSizedImage($imageSize)."' alt='".$randomImage->getTitle()."'>";

    function getRandomImage() {
    global $_zp_current_image;
    if(db_connect()) {
    $random=query_single_row('SELECT zenphoto_images.filename, zenphoto_images.title, zenphoto_albums.folder FROM zenphoto_images INNER JOIN zenphoto_albums ON zenphoto_images.albumid = zenphoto_albums.id ORDER BY RAND() LIMIT 1');
    $image = new Image(new Album(new Gallery(), $random['folder']), $random['filename']);

    return $image;

    } else {
    return false;
    }
    }

    function getImageLinkURL($image) {
    if (zp_conf('mod_rewrite')) {
    return WEBPATH . "/" . urlencode($image->getAlbumName()) . "/" . urlencode($image->name);
    } else {
    return WEBPATH . "/index.php?album=" . urlencode($image->getAlbumName()) . "&image=" . urlencode($image->name);
    }
    }
    ?>
  • zucye Member
    This does not seem to work. Can anyone post an update on this mod??... Pleeeaaaseee!

    I am using version 1.0.8.2, and it works wonderfully (www.zucye.net) and I would love to display a random picture (or pictures) of ONLY one category in my main blog page (I am NOT using word press, but sNews)
  • I've installed the ZenShow plugin off the Wiki, but it's not working... anyone else have trouble getting it to display properly? I keep getting errors which say the ZenShow functions are not defined, and when I edit the ZenShow config, it seems buggy and my changes don't stick...

    Might be a problem with using Wordpress 2.2...
  • Well, it looks like others are able to use ZenShow with WP 2.2, so I'm not sure where I went wrong. thinkdreams, how do you call the plugin in your Wordpress pages? I've tried simply issuing a call to randomPhotos(); but I get a "function undefined" fatal error.

    The plugin seemed to install okay, but it's as if it isn't making it's methods available to my WP installation. Is there any additional manual config needed?
  • I'm calling the zenshow function with `<?php RMCZS::randomPhotos(1); ?>`

    You are correct in that it works on my Zenphoto and Wordpress installation just fine. Running SVN Zenphoto and WP 2.2.1. http://www.bushwoodworking.com

    I'll be happy to help you work through it if you need help. Zenshow has always been fairly stable for me.
  • thanks thinkdreams for put how calling the zenshow function
  • You're welcome. That function hasn't had a lot of documentation, so I've tried to keep it alive as long as I can.
  • Using ZenShow's randomPhotos() function works great for displaying a random thumbnail on my WordPress (v2.3.1) blog... The problem, though, is that no image is showing, only the alt text. I suspect it has something to do with weirdness with the .htaccess file in either (both?) my WP or ZenPhoto (v1.1.2) installations.

    Setup info: I'm doing this in a local dev environment (MAMP, Mac OS X 10.4.10), where the domain is localhost:8888 for both WP and ZenPhoto.
    # Apache 2.0.59
    # MySQL 5.0.37
    # PHP 5.2.1

    One thing I notice under the WP options for ZenShow is that Table Prefix is always "zen_", which I cannot seem to change. In my case, my zenphoto tables are in their own db, and don't have a prefix --afaik, as my SQL knowledge is rather limited. Not sure if this would prevent the image from displaying.

    Thanks in advance!
  • I updated the plugin to use Thinkdreams' modified version from yesterday, and lo! the thumbnail now appears. Still getting the odd size mentioned in this thread, and the other plugin admin UI weirdness, though.

    Nevertheless, thanks for maintaining this plugin, Thinkdreams!
Sign In or Register to comment.