WordPress.org

Ready to get started?Download WordPress

Forums

array_rand help (14 posts)

  1. Jaxia
    Member
    Posted 9 years ago #

    This isn't a WP question yet, but it will be :)

    I'm trying to write a plugin, and I am almost there. But, I keep getting these weird errors and was wondering if someone could help:

    array_rand(): Second argument has to be between 1 and the number of elements in the array

    This is the array bit of code:

    $rand_key=array_rand($quote, 1);
    list($words, $author, $image, $link) = explode("|", trim($quote[$rand_key]));

    Hopefully, I'm just missing something obvious.
    Thank you so much!

  2. ColdForged
    Member
    Posted 9 years ago #

    Hard to tell without a definition of $quote. Is it possible that $quote has zero elements?

  3. Jaxia
    Member
    Posted 9 years ago #

    Sorry. I'm so new I don't even know what's important!

    $quotefile="quotes.txt";
    $dir=dirname($_SERVER['SCRIPT_FILENAME']);
    $url=dirname($_SERVER['PHP_SELF']);

    if (file_exists($dir."/".$quotefile))
    $quote=file($dir."/".$quotefile);

    And yes, the file has data in it.

    Thanks!

  4. ColdForged
    Member
    Posted 9 years ago #

    Try doing a var_dump() of $quote right before your array_rand() call. Then, try removing the 1 parameter from the call just to see if that works.

  5. Jaxia
    Member
    Posted 9 years ago #

    Hmm

    When I do that, it only prints this:
    array(0) { }

    That's it.

    This is what the whole thing looks like right now:

    $quotetitle="Words of Wisdom";
    $quotefile="quotes.txt";

    $dir=dirname($_SERVER['SCRIPT_FILENAME']);
    $url=dirname($_SERVER['PHP_SELF']);

    function STBquote() {
    if (file_exists($dir."/".$quotefile))
    $quote=file($dir."/".$quotefile);
    var_dump($quote);
    $rand_key=array_rand($quote);
    list($text, $author, $link, $linktext) = explode("|", trim($quote[$rand_key]));

    echo "<div>";
    echo "<h2>".$quotetitle."</h2>";
    if (isset($text) && $text!="") { echo "<p class=\"pullquote\">".$text.""; }
    if (isset($author) && $author!="") { echo "".$author.""; }
    if (isset($link) && $link!="" && isset($linktext) && $linktext!="") { echo "".$linktext.""; }
    echo "</div>";
    }

    ?>

    Ideas? I really appreciate your help!

  6. ColdForged
    Member
    Posted 9 years ago #

    You've defined $quotetitle, $quotefile, $dir, and $url in the global namespace, but you haven't provided references from within your function to the global variables. Either move those existing declarations within the function or create global references (e.g. global $url, $dir, ...) at the top of your function.

  7. Jaxia
    Member
    Posted 9 years ago #

    Ah. Okay. I moved them within the function. Now, it's getting closer because it outputs the title, but I still get this error:

    First argument has to be an array

    I tried removing the var dump and adding the 1 back in and it didn't help. Does this mean there is a problem with my txt file?

  8. ColdForged
    Member
    Posted 9 years ago #

    What did you get for the var_dump when it was in? And first argument to what has to be an array?

  9. Jaxia
    Member
    Posted 9 years ago #

    Oh! It says NULL. (Honestly, I didn't understand what the var_dump was doing before)

    array_rand(): First argument has to be an array

    But I suppose the NULL explains that. I guess I don't have my file defined right...?

  10. ColdForged
    Member
    Posted 9 years ago #

    Bear in mind, if that if (file_exists($dir."/".$quotefile)) condition fails, $quote will be NULL as well. Right now that's just, to be honest, poorly structured. You should at the minimum have that condition bound the entire logic block or simply return outright. But, this isn't PHP 101 either :).

  11. Jaxia
    Member
    Posted 9 years ago #

    hehe - Thank you.
    I will look at it some more tonight and see if I can figure it out. Right now, I have to go take a few finals.

    Thanks again!

  12. ColdForged
    Member
    Posted 9 years ago #

    Good luck, I'm sure you'll get it.

  13. Jaxia
    Member
    Posted 9 years ago #

    ColdForged, if you're around -- I got it! I did TWO explodes. I needed to explode by line, and then on the pipes.

    Thanks for telling me about var_dump -- it helped a lot!

  14. ColdForged
    Member
    Posted 9 years ago #

    Great news, glad you got it sorted out.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.