User Tools

Site Tools


Put a random quote on your zen cart template

Create the Table

First, create a table in the DB that will hold all of your quotes. In our case, we wanted the quote, the author, a link to the author, and the date it was added, so a query like this did fine.

CREATE TABLE `quotes` (
`quotes_text` TEXT NOT NULL ,
`quotes_author` TEXT NOT NULL ,
`quotes_link` VARCHAR(255) NOT NULL,
`quotes_active` INT NOT NULL DEFAULT 0,
`quotes_date_added` DATE NOT NULL

Pro tip: VARCHAR(255) is used commonly for things like URLs, but in reality some URLs are longer than 255 characters. Make sure you don't allow URLs that are too long to store, or else make this field bigger.

You can go ahead and insert some quotes in this stage if you like.

Add to Template

Find the place in the template where you'd like to insert the random quote, and add these lines:

in file includes/templates/YOUR_TEMPLATE/tpl_SOME_FILE.php

$quote_sql = $db->Execute("SELECT * from quotes WHERE quotes_active = 1 ORDER BY RAND() LIMIT 1;");
$quote_content = '<div>"' . stripslashes($quote_sql->fields['quotes_text']) . '"</div> <br /><div> - <a href="'.stripslashes($quote_sql->fields['quotes_link']).'"  target="_blank">' . stripslashes($quote_sql->fields['quotes_author']). '</div></a></div>';
echo $quote_content;

Obviously you should modify this to match your own template, and add whatever CSS would make it look good.

Test it Out

Add a few quotes into your database and test it out!

Helper File

You don't want to have to manually add quotes in the db through mysql every time, that would be a pain! Instead you should make yourself a nice helper file in php that you can use through the zencart admin.

Here's an example of a simple file we use to make managing the quote a little easier:

/home/ladyada/public_html/wiki/data/pages/tutorials/zencartmods/quotes.html.txt · Last modified: 2016/01/28 18:05 (external edit)