Solved

PHP random quote generator with priority

Posted on 2008-06-17
7
1,239 Views
Last Modified: 2013-12-13
I am trying to create a script that will randomly pick from a list of quotes and display them on a web page, but will give some of the quotes a higher "weight" based on a priority number. I have pieced together several PHP scripts, but since I don't really know PHP, I have missed something along the way and the script isn't working. Can someone please take a look at this and let me know what I am doing wrong.

Note: after I get this part working, I need to add in the references somehow - can I just add them in at the end of the line with html code wrapped around them to apply the different CSS for them?

Thanks in advance!!
<html>
<head>
</head>
<body>
<p>
<?php
 
$show_quote = 1;
 
$separator = " ";
 
	$quotes[0][quote] = "Quote A";
	$quotes[0][priority] = 1;
	$quotes[1][quote] = "Quote B";
	$quotes[1][priority] = 8;
	$quotes[2][quote] = "Quote C";
	$quotes[2][priority] = 2;
 
if($quotes) {
	for($i = -1; $i < count($quotes); $i++) {
		for($j = 0; $j < $quotes[$i][priority]; $j++) {
			$newquotesindex = count($newquotes) + 1;
			$newquotes[$newquotesindex] = $quotes[$i][quote];
		}
	}
} 
	
if($show_quote >= count($newquotes))
		$show_quote = count($newquotes);
 
$showarray[0] = "";
$showindex = 0;
 
	do {
		$randindex = rand(0, count($newquotes));
		if(!in_array($newquotes[$randindex], $showarray) && $newquotes[$randindex] != "") {
			$showarray[$showindex] = $newquotes[$randindex];
			$showindex++;
		}
	}
	while(count($showarray) <= $show_quote);
 
 
	for($i = 0; $i < $show_quote; $i++ ) {
		if($show_quote == 1 || (($i + 1) == $show_quote))
			echo $showarray[$i]);
		else
			echo $showarray[$i].$separator;
	}
 
}
?>
</p>
</body>
</html>

Open in new window

0
Comment
Question by:techbots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21808948
the outer for needs to start at 0 not -1
for($i = 0; $i < count($quotes); $i++) {
0
 
LVL 11

Accepted Solution

by:
spoxox earned 400 total points
ID: 21809117
If I understand your requirements, I'd recommend a somewhat different approach. The weights are represented in the values in the if statements. Quote A has 10%, B has 15% and C has 75%. Adding quotes will mean updating the quote array and the if statement.

Not sure what this means:
"Note: after I get this part working, I need to add in the references somehow - "
<?php
 
$quotes[0] = "Quote A";
$quotes[1] = "Quote B";
$quotes[2] = "Quote C";
 
$randindex = rand(1, 100);
 
if ($randindex < 10)
  $quoteIndex = 0;
elseif ($randindex < 25)
  $quoteIndex = 1;
else
  $quoteIndex = 2;
 
echo $quotes[$quoteIndex];
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:techbots
ID: 21809121
Another note: I don't need the script to ever display more than one quote, so that part of the script can be removed - I just don't know what is safe to delete so I left it in there.  It woudl be great if someone wants to simplify this for me!

hielo, I tried just changing that but it didn't do anything. I must still have an error somewhere else.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:techbots
ID: 21809189
Brilliant spoxox!!

Exactly what I was looking for - and thank you for such a speedy and SIMPLE answer!

This is what I meant by adding references - my idea worked so I already have the answer to that.

Thank you - now I can sleep tonight!!
<html>
<head>
<style type="text/css">
<!--
.name {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-style: italic;
	font-weight: bold;
}
-->
</style>
</head>
<body>
<p>
 <?php
 
$quotes[0] = "Quote A<br/><span class=\"name\">- John Doe</span>";
$quotes[1] = "Quote B<br/><span class=\"name\">- Jane Smith</span>";
$quotes[2] = "Quote C<br/><span class=\"name\">- Fred Flintstone</span>";
 
$randindex = rand(1, 100);
 
if ($randindex < 10)
  $quoteIndex = 0;
elseif ($randindex < 25)
  $quoteIndex = 2;
else
  $quoteIndex = 1;
 
echo $quotes[$quoteIndex];
 
?>
 
</p>
</body>
</html>

Open in new window

0
 
LVL 11

Expert Comment

by:spoxox
ID: 21809241
Happy dreams!
0
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 100 total points
ID: 21810317
Another approach, based on spoxox's design:
<?php
 
$quotes[0]['text'] = "Quote A";
$quotes[0]['priority'] = 8;
$quotes[1]['text'] = "Quote B";
$quotes[1]['priority'] = 2;
$quotes[2]['text'] = "Quote C";
$quotes[2]['priority'] = 1;
 
$allquote = array();
foreach ($quotes as $onequote) {
  for ($x=0;$x<$onequote['priority'];$x++) {
    $allquote[] = $onequote['text'];
  }
}
 
$randindex = rand(1,count($allquote));
 
echo $allquote[$randindex]; 
?>

Open in new window

0
 
LVL 1

Author Comment

by:techbots
ID: 21813013
routinet -

I like your way of handling priorities better, it's a lot simpler for me to keep track of even though both work.

You both helped tremendously. Thank you all for your swift answers - what would I do without EE?!

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question