• 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 (https://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?

Viewing 12 replies - 1 through 12 (of 12 total)
  • 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!

    Thread Starter jcontraros

    (@jcontraros)

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

    Thread Starter jcontraros

    (@jcontraros)

    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]

    Thread Starter jcontraros

    (@jcontraros)

    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…

    Thread Starter jcontraros

    (@jcontraros)

    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.

    Thread Starter jcontraros

    (@jcontraros)

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

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

    Thread Starter jcontraros

    (@jcontraros)

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

    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]; ?
    }

    Thread Starter jcontraros

    (@jcontraros)

    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.

    Thread Starter jcontraros

    (@jcontraros)

    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?

    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.

    Thread Starter jcontraros

    (@jcontraros)

    Yep that did the trick. Thanks again.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Getting data from a custom database table?’ is closed to new replies.