reading a random quote

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

reading a random quote

Post by Mini-Me »

Alright, I'm writing a quote submission script. Actually, I'm finished with that part.
http://minime001.net/temp/quotes.php
it writes to: http://minime001.net/temp/quotes.txt

What I want to do now is to read a random quote from the text file. How could I search for the (%%:) part and read what's after it? I mean I figure I'll use the fread() function, but I just don't see how. ^^;

Here's the code for the submission page, though:

Code: Select all

<?php
  $quote  = (isset($_REQUEST['quote']))  ? $_REQUEST['quote']  : "";
  $submit = (isset($_REQUEST['submit'])) ? $_REQUEST['submit'] : "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Emerald Sword Quote System</title>
</head>
<body bgcolor="#b0b0b0">
<h1>Emerald Sword Quote System</h1>
<hr />
<?php if ($submit == Preview) { print('<pre><p>' . $quote . '</p></pre><hr />'); }
      else if ($submit == Submit) {
        print ('<h3>Your quote has been submitted:</h3>
                <pre><p>' . $quote . '</p></pre>');
        $fp = fopen( "quotes.txt", "a");
        fwrite($fp, "\n(%%:)\n" . $quote);
        fclose($fp);
      }
      if ($submit != Submit) { ?>
<form>
<p><textarea rows="15" cols="100" name="quote"><?php print( $quote ); ?></textarea></p>
<p><input type="submit" name="submit" value="Preview" /> <input type="submit" name="submit" value="Submit" /></p>
</form>
<?php } ?>
<hr />
<?php if ($submit == Submit) {
      print('<form><p><input type="button" value="Submit Another Quote" onclick="location=''?''" /></p></form>'); } ?>
</body>
</html>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

explode()
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

wow, alright. But I'm missing something... First of all, why won't this work:

Code: Select all

<?php
  $fp = fopen("quotes.txt", "rb");
  $qblah = fread($fp, filesize($fp));
  $qlist = explode("(%%:)", $qblah);
  echo $qlist[2];
  fclose($fp);
?>
Second, how would I get a random quote? I know it'd be something equivalent to javascripts qlist.length thing. i.e. How do I get the length of the array?
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

oh. $qblah = fread($fp, filesize("quotes.txt"));
lol ^^;

But I still need the length of the array...
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

try the [php_man]count[/php_man] function.

e.g.

Code: Select all

$num_lines = count($qlist);
Hope this helps
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

DOH! nvm... count()

Edit: didn't see last message. Thanks. :P
User avatar
emperor
Forum Newbie
Posts: 16
Joined: Tue Mar 02, 2004 11:20 am
Location: UK

Post by emperor »

Check out [php_man]array_rand[/php_man] which picks out the required number of random entries from an array
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

hmm, ok. I'm having a problem, though...
ahem... GAHWTF!!! I've looked and looked and can't find a single damn error. But for some reason, nothing loads. Period.

Code: Select all

<?php
  $page   = (isset($_REQUEST['page']))   ? $_REQUEST['page']   : "";
  $quote  = (isset($_REQUEST['quote']))  ? $_REQUEST['quote']  : "";
  $submit = (isset($_REQUEST['submit'])) ? $_REQUEST['submit'] : "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
<title>Emerald Sword Quote System</title>
<style type="text/css">
body {
  background-image: url('http://img.photobucket.com/albums/v201/MiniMe001/emerald_sword/scraped_metal2.png');
}
.main {
  padding: 5px;
  width: 100%;
  border: 1px solid #c0c0c0;
  margin: 10px;
  background-color: #f0f0f0;
}
a:link, a:visited, a:active {
  text-decoration: underline;
  color: #000000;
}
a:hover {
  color: #555555;
  text-decoration:underline;
}
</style>
</head>
<body>
<h1>Emerald Sword Quote System</h1>
<table class="main"><tr>
<td><a href="?page=list">List Quotes</a> &bull; <a href="?page=random">Random Quote</a> &bull; <a href="?page=add">Add Quote</a></td>
</tr></table>
<table class="main"><tr>
<td><?php
  if ($page == list) {
    $fp = fopen("temp/quotes.txt", "rb");
    $fr = fread($fp, filesize("temp/quotes.txt"));
    $fa = array("(%%:)" => "<hr />");
    echo strtr($fr, $fa);
    fclose($fp);
  }
  else if ($page == random) || ($page == NULL) {
    $fp = fopen("temp/quotes.txt", "rb");
    $qblah = fread($fp, filesize("temp/quotes.txt"));
    $qlist = explode("(%%:)", $qblah);
    $qlength = count($qlist);
    $qrand = rand(0,$qlength);
    print('<pre>' . $qlist[$qrand] . '</pre>');
    fclose($fp);
  }
  else if ($page == add) {
    if ($submit == Preview) { print('<pre><p>' . $quote . '</p></pre><hr />'); }
    else if ($submit == Submit) {
      print ('<h3>Your quote has been submitted:</h3><pre><p>' . $quote . '</p></pre>');
      print('<form><p><input type="button" value="Submit Another Quote" onclick="location=''?page=add''" /></p></form>');
      $fp = fopen("temp/quotes.txt", "a");
      fwrite($fp, "\n(%%:)\n" . $quote);
      fclose($fp);
    } 
    if ($submit != Submit) { ?>
      <form>
        <p><textarea rows="15" cols="100" name="quote"><?php print( $quote ); ?></textarea></p>
        <p><input type="submit" name="submit" value="Preview" /> <input type="submit" name="submit" value="Submit" /></p>
      </form>
<?php }
  } ?>
<hr /></td>
</tr></table>
</body>
</html>
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Line 39 should be

Code: Select all

if ('list' == $page)

Request parameters are strings, so you should be testing against strings. There are a few other lines with the same error as line 39.

Hope this helps
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

when you say "nothing loads", do you mean there is no output from the script? or that the script is outputting the html properly, but its not opening/outputting any of the files in your if() statements?


btw- take a look at file_get_contents();

it is faster and will make your code smaller, and more readable.

thats not your problem, just a tip.

Code: Select all

if ($page == 'list') {
    $content = file_get_contents("temp/quotes.txt");
    echo str_replace('(%%:)', '<hr />', $contents);
  }
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

nothing = absolutely nothing.
Source: <html><body></body></html>
I figured out where the problem was. I never figured out what it was, but I took out the list section and redid it and it worked.

file_get_contents(); VERY cool.
str_replace(); cool too.

thanks, guys.
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

alright, I have another problem now. I was going to add basic bulletin board codes and for some reason it acts as if it's ignoreing them altogether..

Code: Select all

$qcode = array("<" => "<", ">" => ">", "{i}" => "<i>", "{/i}" => "</i>", "{b}" => "<b>", "{/b}" => "</b>");
$qreal = strtr($quote, $qcode);
print('<pre><p>' . $qreal . '</p></pre>');
print('<form><p><input type="button" value="Submit Another Quote" onclick="location=''?page=add''" /></p></form>');
$fp = fopen("test.txt", "a");
fwrite($fp, "(%%:)" . $qreal);
fclose($fp);
Edit: The brakets aren't turning up. ^^; Wherever {letter} is, it's supposed to have the square brackets. ;)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

how about:

Code: Select all

<?php
// set string
$variable="{b}bold{/b} <b>not bold</b>";

// dis-allow html
$variable = htmlspecialchars($variable);
// bb code support
$variable = str_replace("{b}", "<b>", $variable);
$variable = str_replace("{/b}", "</b>", $variable);
echo $variable;
?>
I had to put {b} and {/b} because if i attempt to type them with brackets [] it actually bolds my source code for me due to the fact we are talking on a bb-code enabled forum
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

dammit, goofy me. I just didn't notice where I was putting it. I put the translation stuff in a separate "if ($submit != NULL) {}" container. ^^;
Well, I like your way better anyway--cleaner.
Post Reply