November 5, 201214 yr Hi all, I am currently developing a website that will have a testimonial page as well as a snippet of 4-5 testimonials in a right hand sidebar on each other page (much like mentioned http://www.webdesignerforum.co.uk/topic/51133-php-testimonials-system/page__hl__%2Brandom+%2Btestimonial). However, even though I have found examples of php testimonial rotation I can't seem to find anything that won't show duplicates when showing the 4 or 5 on the sidebar. The thread linked above was pretty much exactly what I was looking for but I couldn't quite piece it together to make a working script. I am happy to store the testimonials in text a text document if easier and it would be an added bonus if the testimonials page also pulls each testimonial one by one but not essential as it can be a static page. Can anybody possible point me in the right direction please? Many Thanks, Andy
November 5, 201214 yr Add the testimonials to a mysql database then load five random ones into an array, then loop out the array. Will solve your problem on duplicates as well.
November 5, 201214 yr Author Thanks for the speedy reply Aaron. Any examples on how to do this? PHP is not a strong point for me and even though I'm finding plenty of examples, I'm not having any luck tweaking them for my own use. I've also thought of one more thing that would need to be added, an image with each quote. I guess this can be done by adding an image to a folder on the server and calling the url? I will be able to setup the db ok (I think), just a case of calling it to the page. I am searching around every where but there just doesn't seem to be an answer I can use.
November 5, 201214 yr If this is the only dynamic thing you want, you can go for a very simple solution, like having all the quotes on one text file. There are more than one ways to do this with PHP, here is one: next to your index.php create folder "pics" for your pictures and a text file "quotes.txt" with this content: first quote --- autor1 --- picture.jpg second quote --- autor2 --- pic.gif third quote --- autor3 --- image.png and in your index.php you can put this: <?php function printTestimonials($t,$img){ $pieces = explode("---", $t); $text = nl2br($pieces[0]); $author = $pieces[1]; $picture = "pics/" . $pieces[2]; echo "<blockquote>"; if($img) echo "<img src='{$picture}'>"; echo "<p>{$text}</p>"; echo "<footer>{$author}</footer>"; echo "</blockquote>"; } $file = file_get_contents("quotes.txt"); //on the testimonials page //we call the function with true in the second parameter to print the image $testimonials = explode("\n\n", $file); foreach($testimonials as $t){ printTestimonials($t,true); } //on the sidebar //we call the function with false in the second parameter to ignore the image shuffle($testimonials); foreach(array_slice($testimonials, 0, 5) as $t){ printTestimonials($t,false); } Edited November 5, 201214 yr by ocorreiododiogo
November 5, 201214 yr I don't see a use for a flat files when sql is so simple and robust. Google random row from database and you'll find several examples; Depending on the amount of rows in the database I'd maybe take a look at this article for more information on the sql command. Then do something like (untested): SQL: CREATE TABLE testimonials ( Name text, Text text ) PHP: <?php $mysqli= new mysqli("localhost", "username", "password", "database"); // Select Random Row (read the link above for the best approach to random) $query = "SELECT * FROM `testimonials` ORDER BY RAND() LIMIT 1;" // Run Query $result = $mysqli->query($query) // Get Row $row = $result->fetch_row(); // echo name and testimonial echo $row[0]." - ".$row[1]; // Free The Result mysqli_free_result($result); // Close Connection mysqli_close($link); ?> Hope that helps, may not work out of the box but you get the idea! Edited November 5, 201214 yr by Aaron Clifford
November 6, 201214 yr Author Great stuff guys, appreciate the input. Will give this a go this morning.
Create an account or sign in to comment