Wordpress additional DB table and code
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 posted this at 04:24 — 27th March 2009.
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 posted this at 09:55 — 27th March 2009.
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 posted this at 11:27 — 27th March 2009.
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 posted this at 15:24 — 27th March 2009.
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 posted this at 15:52 — 27th March 2009.
He has: 1,581 posts
Joined: Nov 2005
Same would apply with putting all quotes in an array and choosing a random key number.
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 posted this at 15:52 — 27th March 2009.
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.