Wordpress additional DB table and code

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I am making a quote box on my wordpress theme which basically will be a bunch of random quotes like at the top here (TWF).
What's the best way to store and retrieve the quotes I have?

I thought something like:
About 30 quotes will be selected at random from the DB, stored in a session array then one displayed at random from the array on the page header, new one each page load.

I'll need an input form to add/edit/delete them too.
Someone suggested I make a plugin, which is fine, and sounds possible, but maybe over kill for what I am doing. Or would coding a new function, DB query and page with form into the main site files be better?

Cheers

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

if all you're doing is storing a list of quotes (text) why bother with a db at all?

use PHP to read/write to a flat file, load the file with PHP include or JavaScript, randomly pick a line/member, bingo!

something along the lines of my links admin, but much much simpler!

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I agree - the database is probably not needed, unless you want to change the quotes in the admin area or something. See if you have the plugin "Hello Dolly" in your Wordpress installation. This is meant to serve as a sample plugin, but it also displays random phrases like you are trying to do.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Yeah, you're both right.

And it makes it a million times easier too.

Cheers

EDIT
hmm.

How would I have a bunch of quotes with a reference to each allowing a random one to be selected from them?
I thought of a var for each quote i.e.

<?php
//set random
$total_quote = 3;
$number_chosen = rand(1, $total_quote);
//set quotes
$quote_1 = "first quote";
$quote_2 = "second quote";
$quote_3 = "third quote";

//print chosen var
print ${'quote_'.$number_chosen};
?>

Problem is it creates a load of vars that will never get used.
Same would apply with putting all quotes in an array and choosing a random key number.

Any ideas?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

instead of separate variables you could create an array from a single text variable string containing all the quotes using explode()

<?php
$str
= "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>



The output of the code above will be:Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)

[HINT] make your separator an unusual character such as a pipe "|"

You could use also use array_rand():

<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r(array_rand($a,1));
?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

greg wrote:
Problem is it creates a load of vars that will never get used.
Same would apply with putting all quotes in an array and choosing a random key number.
I know how to work with arrays, that's not the issue. Either variables or an array will have a huge amount of data in it, and only a tiny amount used.

Quotes will be 150 chars max, and eventually I will have hundreds of quotes.
I don't want to set a huge array or hundreds of variables for only one quote.

So for example, with only FOUR quotes

<?php
$array
= array("Do not worry about your difficulties in Mathematics.
I can assure you mine are still greater"
,
"I know not with what weapons World War III will be fought, but
World War IV will be fought with sticks and stones"
,
"As far as the laws of mathematics refer to reality, they are not
certain; and as far as they are certain, they do not refer to reality"
,
"The significant problems we have cannot be solved at the same level of
thinking with which we created them"
);
?>

As I will actually only USE one, even 4 looks nasty and inefficient, with hundreds of quotes and hundreds of visitors, I wouldn't like to think about the resource usages.

This cannot be the most efficient way to do this.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Something like this would be best.

<?php
//set quotes
$quotes[] = "first quote";
$quotes[] = "second quote";
$quotes[] = "third quote";

// get the random element index
$index = array_rand($quotes);

//print chosen var
print $quotes[$index];
?>

To add another quote, just add another element to the array:

<?php
$quotes
[] = "fourth quote";
?>

Edit: so you mention that you will have hundreds...maybe a table would be the best option. This page will help you with interacting with the Wordpress database.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.