Forums

[resolved] Random Image (30 posts)

  1. macart
    Member
    Posted 2 years ago #

    Hi Guys, Trying to get this to work:

    http://demo.marcofolio.net/php_random_image_rotation/

    Not sure what Im doing wrong. Im coding this wrong, so can someone tell me the correct way. Thanks.

    Where Im placing it.

    <div class="contentBanner"></div>

    My CSS for Div.

    }
    .contentBanner {
    height: 250px;
    width: 788px;
    float: left;
    background: #003 url(images/banner.png);
    margin-top: 10px;
    margin-bottom: 20px;
    }

  2. @mercime
    Member
    Posted 2 years ago #

    You may add a div to control it, but you need to add info below within the div tags e.g.

    <div class="contentBanner">
    <img src="images/rotate.php" alt="Rotating Image" width="788" height="250" />
    </div>
  3. viceng
    Member
    Posted 2 years ago #

    I believe you want

    <div id="contentBanner"></div>

    #contentBanner {
    height: 250px;
    width: 788px;
    float: left;
    background: #003 url(images/banner.png);
    margin-top: 10px;
    margin-bottom: 20px;
    }

    You want id's not classes

  4. macart
    Member
    Posted 2 years ago #

    Thanks for the responses.

    I tried this:

    <div id="contentBanner"><img src="rotate.php" alt="Rotating Image" width="788" height="250" /></div>

    }
    #contentBanner {
    height: 250px;
    width: 788px;
    background: url(images/rotate);
    float: left;
    margin-top: 10px;
    margin-bottom: 20px;
    }

    I just have a big blank box that says "Rotating Image"

    Thanks for any help

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    If you just want to rotate header images you'd be better off with a simple function in your theme file...

    If you name the images you want to rotate in a numeric sequence it's only a few lines of code to write...

    So let's say you had images like this... (doesn't matter where they're placed - as in the folder)..

    image-1.jpg
    image-2.jpg
    image-3.jpg

    As long as you keep a naming scheme simple you could essentially do that with minimal amounts of code..

    Here's a basic number randomising function..

    <?php
    function randnum() {
    	// Range creates an array from a range (in this case 1 - 10)
    	$b = range(1,10);
    	// So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    	shuffle($b);
    	$r = array_values($b);
    	echo $r[rand(1,10)];
    }
    ?>

    Then you'd place a call to the function wherever, here's 2 examples..

    On an image.
    <img src="/some/path/to/images/myimage-<?php randnum(); ?>.jpg" alt="" />
    Using an element and CSS.
    <div class="myclass" id="myimage-<?php randnum(); ?>">

    With the second option you'd set the background properties using the class, minus the image...

    .myclass {
    background-position:top left;
    background-color:transparent;
    background-repeat:no-repeat;
    }
    #myimage-1 {
    background-image:url(images/someimage1.jpg);
    }
    #myimage-2 {
    background-image:url(images/someimage1.jpg);
    }
    /* And so on up to 10 (assuming 10 is the limit) */

    Just alternate ideas... ;)

    And my examples are crude, so don't take them as shining examples!.. lol.. it's just to give you an idea of how simple a random function can be.... no need for additional scripts...

  6. macart
    Member
    Posted 2 years ago #

    Thanks for everyone for the help, I know this is not that complicated, but still not working.

    As soon as I put this in my functions.php:

    <?php function randnum() {
    // Range creates an array from a range (in this case 1 - 10)
    $b = range(1,10);
    // So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    shuffle($b);
    $r = array_values($b);
    echo $r[rand(1,10)];
    }
    ?>

    I got this:

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:6) in /home/content/html/wordpress/wp-includes/functions.php on line 784

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:6) in /home/content/html/wordpress/wp-includes/functions.php on line 785

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    It's proberly incorrect nesting of the PHP tags..

    Revert back to how it was (i assumed you backed it up or reverted the changes)..

    And paste just this part..

    function randnum() {
    // Range creates an array from a range (in this case 1 - 10)
    $b = range(1,10);
    // So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    shuffle($b);
    $r = array_values($b);
    echo $r[rand(1,10)];
    }

    After..
    <?php at the top of the file (the next line is fine).

    The issue (i think) is the incorrect nesting, which might look like..

    <?php
    some code blah blah blah
    <?php

    or..

    ?>
    some code blah blah blah
    ?>

    No opening tags should follow one another, and likewise for closing tags...

    So based on the 2 bits above, to fix those, they'd look like this..

    <?php
    some code blah blah blah
    ?>
    <?php

    and..

    ?>
    <?php
    some code blah blah blah
    ?>

    Hopefully that's the problem anyway, i did test the code before i posted it.

  8. macart
    Member
    Posted 2 years ago #

    Thanks for the help.

    My functions.php

    <?php
    if (function_exists('register_sidebar'))
    register_sidebar();?>

    <?php
    function randnum() {
    // Range creates an array from a range (in this case 1 - 10)
    $b = range(1,10);
    // So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    shuffle($b);
    $r = array_values($b);
    echo $r[rand(1,10)];
    }
    ?>

    Got this:

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:5) in /home/content/m/a/c/macart/html/wordpress/wp-includes/functions.php on line 784

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:5) in /home/content/html/wordpress/wp-content/themes/mytheme.php on line 785

  9. macart
    Member
    Posted 2 years ago #

    My header code for the fun of it.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php if (is_home () ) {
    bloginfo('name');
    } elseif ( is_category() ) {
    single_cat_title(); echo ' - ' ; bloginfo('name');
    } elseif (is_single() ) {
    single_post_title();
    } elseif (is_page() ) {
    bloginfo('name'); echo ': '; single_post_title();
    } else {
    wp_title('',true, left);
    } ?>
    </title>

    <link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet" type="text/css" />
    <link href="<?php bloginfo('pingback_url'); ?>" rel="pingback" />
    <?php wp_head(); ?>
    </head>

    <body class="twoColFixRtHdr">
    <div id="container">
    <!-- end #header --></div>

  10. Mark / t31os
    Moderator
    Posted 2 years ago #

    From this bit of code you posted above..

    <?php
    if (function_exists('register_sidebar'))
    register_sidebar();?>
    
    <?php
    function randnum() {
    // Range creates an array from a range (in this case 1 - 10)
    $b = range(1,10);
    // So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    shuffle($b);
    $r = array_values($b);
    echo $r[rand(1,10)];
    }
    ?>

    If you remove the bottom most ?> does that then resolve the problem?

    I don't need the header stuff..

    You've not even placed a call to that function anywhere yet, and the code works here, so i can only assume this being a code placement issue..

  11. macart
    Member
    Posted 2 years ago #

    Thanks again for the help.
    I removed the bottom ?>
    Got slammed with same errors.

  12. Mark / t31os
    Moderator
    Posted 2 years ago #

    Late one for me, off to bed now, might be easier to simply just provide the complete code from the file.

    Use a pastebin though please.
    http://wordpress.pastebin.com/

    Perhaps you're on an older version of PHP that doesn't like something in what i've done above, hard to say without seeing more of your code though.

    I'll check back after some sleep.. ;)

  13. compguru910
    Member
    Posted 2 years ago #

    You need to start object buffering in order to do that, thats what the error is. In the header, before any headers are inputted (like sessions or whatnot) you need to put in the code

    <?
    ob_start;
    ?>

    at the very very top of the header (before the <head> even)
    and at the bottom of the footer put

    <?
    ob_end_flush;
    ?>

    Figured that would help

  14. lindsayanng
    Member
    Posted 2 years ago #

    heasders already sent error is USUALLY the cause of white space at the begining or end of the file.. PHP files do NOT Like any blank spaces before the first <?php tag or after the last ?> tag

    So check your file that you were editing, if you can move your cursor before the first or after the last, DELETE until you can not move before the first or after the last

  15. lindsayanng
    Member
    Posted 2 years ago #

    oh yea.. heres a link to a page with common causes of errors. It explains the headers already sent error
    CLICK HERE

  16. compguru910
    Member
    Posted 2 years ago #

    If you put in the ob_start tag, your errors will go away. Its object buffering and kills most problems, but creates a bit of headway on the server, but nothing notable with newer server technologies.

  17. macart
    Member
    Posted 2 years ago #

    I followed the instructions above, placing above the header and below the footer. Slammed same exact errors! Is this a 2.8.1 bug?

  18. macart
    Member
    Posted 2 years ago #

    Thanks again for everyones help. Wondering if anyone can get the image rotation described above to work on 2.8.1, or is it just me.

  19. macart
    Member
    Posted 2 years ago #

    Tried it again this am, got the same errors above and this one:

    function randnum() { // Range creates an array from a range (in this case 1 - 10) $b = range(1,10); // So the array looks like - array(1,2,3,4,5,6,7,8,9,10) shuffle($b); $r = array_values($b); echo $r[rand(1,10)]; }

  20. Mark / t31os
    Moderator
    Posted 2 years ago #

    Open the wp-config.php file and check there's no extra lines or spaces after the ?> at the bottom..

    You shouldn't need the output buffering in this case, so is there a particular reason you believe it will help?

    In would help to know what is in this file and on that line.
    wp-content/themes/mytheme.php on line 785

    Most common cause of the headers not sent error is white-space where it shouldn't be. Check the config file first, as this is a common culprit..

    Regarding image rotation, there's ton of scripts for the job, you only need a PHP script, take you pick.

    There are more then enough users here that are capable of providing you with one, i've given you one of many examples of random images...
    If you search the forum you'll find other example code..

    A more basic random number function.

    <?php
    $numbers = range(1, 10);
    $numbers = array_rand($numbers,1);
    print $numbers;
    ?>

    Or with non-numeric array items...

    <?php
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input);
    echo $input[$rand_keys] . "\n";
    ?>
  21. macart
    Member
    Posted 2 years ago #

    Removed output buffering.

    Nice Catch! wp-config.php did have a white space at bottom. Corrected that and uploaded. Added code again to functions.php, slammed same errors.

    Not sure how to find line 784 and 785? Page source is only 100 lines of code.

  22. Mark / t31os
    Moderator
    Posted 2 years ago #

    Put your functions.php code into pastebin....
    http://wordpress.pastebin.com/

    Then post a link here.

  23. macart
    Member
    Posted 2 years ago #

    NO ERRORS! With this functions.php code.

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'before_widget' => '<div class="sidepanel">',
    'after_widget' => '</div>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    ?>
    <?php
    function randnum() {
    // Range creates an array from a range (in this case 1 - 10)
    $b = range(1,10);
    // So the array looks like - array(1,2,3,4,5,6,7,8,9,10)
    shuffle($b);
    $r = array_values($b);
    echo $r[rand(1,10)];
    }
    ?>

  24. macart
    Member
    Posted 2 years ago #

    sorry newbie mistake

  25. macart
    Member
    Posted 2 years ago #

    NO ERRORS! But still not working.

    http://wordpress.pastebin.com/m163e1bf7

  26. macart
    Member
    Posted 2 years ago #

    image_2.jpg will show up eventually if I refresh several times. The rest of the time, the area is blank.

  27. Mark / t31os
    Moderator
    Posted 2 years ago #

    That's because you need to match the ID's in the CSS to the total amount of images... look at the example i gave before, note this bit...

    /* And so on up to 10 (assuming 10 is the limit) */

    #myimage-1 {
    background-image:url(images/rotate/image_1.jpg);
    }
    #myimage-2 {
    background-image:url(images/rotate/image_2.jpg);
    }

    You need 10 of those if you have 10 images to rotate.... here's the next 2 as an example..

    #myimage-3 {
    background-image:url(images/rotate/image_3.jpg);
    }
    #myimage-4 {
    background-image:url(images/rotate/image_4.jpg);
    }

    Right now that function is producing a number from 1-10, so you need to account for each instance...

    Or reduce the total amount it randomises.. (the 2 occurances of "10" in the function code).

  28. macart
    Member
    Posted 2 years ago #

    My Bad. Thanks very much for all the help!

  29. macart
    Member
    Posted 2 years ago #

    Working perfectly! Thanks again

  30. Mark / t31os
    Moderator
    Posted 2 years ago #

    You're welcome.. ;)

    If it's not flexible enough or not quite what you want we can always modify it..

    :)

Topic Closed

This topic has been closed to new replies.

About this Topic