WordPress.org

Ready to get started?Download WordPress

Forums

Getting data from a custom database table? (13 posts)

  1. jcontraros
    Member
    Posted 3 years ago #

    I only have a very basic understanding of PHP/MySQL so if someone could help me out here that would be greatly appreciated.

    I created a "Random Philosophy Fact" box at the top of my website (http://awwyah.com). What I am trying to do is have a random "philosopher" and "description" pair selected and printed out in this box.

    I've created a database table (wp_philosophy_philosopher) with the columns "philosopher" and "about".

    How would I go about randomly selecting a row from this table and placing the two columns into a variable so that I can echo them in the HTML?

  2. Ganners
    Member
    Posted 3 years ago #

    There are a few ways you could do this, how I might do it would be:

    function randomPhilosopher() {
    global $wpdb;
    $randomFact = $wpdb->get_row("SELECT * FROM wp_philosophy_philosopher, ARRAY_N);
    
    $NumRows = count($randomFact);
    $RandNum = rand(0, $NumRows);
    
    print $randomFact[$RandNum]->philosopher;
    print $randomFact[$RandNum]->about;
    }

    I think that might do it off the top of my head though. Let me know if it works!

  3. jcontraros
    Member
    Posted 3 years ago #

    Thanks, I'll give that code a try and get back to you.

  4. jcontraros
    Member
    Posted 3 years ago #

    Okay so I'm running into this error:

    Parse error: syntax error, unexpected T_CLASS in /home/content/j/c/o/jcontraros/html/logic/wp-content/themes/thesis_18/custom/custom_functions.php on line 46

    Now I'm using the Thesis theme, so all my custom PHP is in the file "custom_functions.php" - So the line numbers here won't match up because there is other code in that file before it, but here is my modified code trying to use yours. Perhaps I'm doing something horribly wrong here haha.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. jcontraros
    Member
    Posted 3 years ago #

    Okay so there was a missing double quote in the database get_row line. Now the script runs, but is not writing the variables into the html...

  6. jcontraros
    Member
    Posted 3 years ago #

    Okay so the problem lies in this piece of code:

    echo ($randomFact[$RandNum]->philosopher);

    Nothing is inserted with this snippet, however, when I use this code:

    echo ("$randomFact[$RandNum]->philosopher");

    It echos to the screen "Plato->philosopher" Plato being the random "philosopher" it pulled from the database. But its printing "->philsopher" too.

    Not sure where I should be or not be putting quotes or something here.

  7. jcontraros
    Member
    Posted 3 years ago #

    Using this it works, but only echos the first character of the philosophers name:

    print ($randomFact[$RandNum][philosopher]);

  8. jcontraros
    Member
    Posted 3 years ago #

    It is also printing out the same thing in both the "philosopher" and "about" section.

  9. Ganners
    Member
    Posted 3 years ago #

    Ah sorry my mistake, forgot it was an array! This should work for you:

    function randomPhilosopher() {
    global $wpdb;
    $philosopher_table = $wpdb->prefix . 'philosophy_philosopher'; //Good practice
    $randomFact = $wpdb->get_results( "SELECT * FROM $philosopher_table");
    
    $NumRows = count((array) $randomFact);
    $RandNum = rand(0, $NumRows); 
    
    print $randomFact[$RandNum]->philosopher;
    print $randomFact[$RandNum]->about;
    //or return $randomFact[$RandNum]; ?
    }
  10. jcontraros
    Member
    Posted 3 years ago #

    Ah wonderful! Thank you so much, no I can use this code and modify it to output a random lexicon and quote too. Thanks again! Much appreciated.

  11. jcontraros
    Member
    Posted 3 years ago #

    Only thing is sometimes a field or more then one field is left blank. This was happening with the other script as well. Its almost like its adding 1 blank row into the randomization mix or something? Any idea why this might be happening?

  12. Ganners
    Member
    Posted 3 years ago #

    Hmm perhaps

    $RandNum = rand(0, $NumRows-1);

    Maybe the randomly generated number is outside of the count? Not sure. Else you could perhaps loop it all with a while loop which checks if both the philosopher and about returned are not null.

  13. jcontraros
    Member
    Posted 3 years ago #

    Yep that did the trick. Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic