Driving a random quote like functionality with database

I want to have functionality like this:
A random quotes appears in particular section of the website (say sidebar) after a particular amount of time(say everyday).

I want to power it up using a new table in the database.

So maybe I will have these columns-

  • Quote_Text
  • Quote_Timestamp
  • Quote_weblink (if any)
  • Quoted_by

etc.

Can you help me get started on the code?

I know sql and can learn requisite php (I know other languages like java, c++ etc). But I don’t have any experience with CSS (apart from some basic syntax).

Solutions Collecting From Web of "Driving a random quote like functionality with database"

From your comments I don’t really know why you should not make use of custom post types and custom fields.

So what is you have extra flexibility from the above. IMHO, extra flexibility is always a plus when attempting something like this. The other plus points here are that you don’t have to make use of extra functions to create these functionalities, neither would you need to create extra db fields (which I will not recommend you to do, I really don’t see the need for extra db tables here)

I think the problem here is a lack of implementation. Here is a basic idea:

Your first step will be to register a custom post type. This will just be a basic custom post type which you can decide to exclude from search and having a separate single view.

The important part here would be to enable custom-fields in the supports argument

Here is a modified example from the codex

function codex_custom_init() {
    $args = array(
      'supports' => array( 'custom-fields' ),
      'label'    => 'Quotes'
    );
    register_post_type( 'quotes', $args );
}
add_action( 'init', 'codex_custom_init' );

Please see the link given above for more examples and information on registering custom post types

When you create a new quote, you can now create and set your custom fields accordigly. Name your custom fields according to the fields in your question. The values will be the information that you will enter that you will need to display. Also, have a good look at the link provided to learn how to use custom fields. You might also be interested in this post

When it comes to displaying, you will need to make use of a custom query and transients. We will make use of WP_Query for the custom query which will display the quotes and the Transient API which will rotate the quote on a daily basis. Again, go and read and play with examples from the links given.

Here is a concept query (modified from the codex, untested)

// Check for transient. If none, then execute WP_Query
if ( false === ( $quotes = get_transient( 'daily_quotes' ) ) ) {

      $quotes = new WP_Query(
   array(
'post_type' => 'quotes',
'posts_per_page' => 1,
'orderby' = 'rand'
   ));

// Put the results in a transient. Expire after 24 hours.
set_transient( 'daily_quotes', $quotes, 24 * HOUR_IN_SECONDS );
} 

// Run the loop as normal
if ( $quotes->have_posts() ) {

   while ( $quotes->have_posts() ) { 
   $quotes->the_post(); 
       $data = get_post_meta( $post->ID );

       echo 'Quote Text: '. $data['Quote_Text'][0] . '</br>';
       echo 'Quote Timestamp: ' . $data['Quote_Timestamp'][0] . '</br>';
       // Continue with the rest

   }
   wp_reset_postdata();
}

I found a plugin and it used a custom database table which was what I was looking for. I will customize its code as the license allows to do so