Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,

I am a complete newbie in the programming world. I have enjoyed the book but I really wish it provided guidence with the pursue questions. I find them extremely frustrating and have constantly had to search the forum for help.

 

I am currently stumped on the chpt 11 pursue question where it asks to change add_quote.php to separate the quote and attribute inputs, write them separately to a file and then modify view_quote.php so it displays both pieces of data. I have separated the two quotes but I have no idea how to retrieve them together. I don't see where the book tells me how to do this. Any help would be greatly appreciated. Thank you.

Link to post
Share on other sites

"Change add_quote.php so that it takes the quotation and the attribution as separate inputs and writes them separately to the text file."

 

I will be honest with you, I've just taken a look myself but i have no idea what the attribution part of data is you are meant to take, i only see the quote in the text area??

Link to post
Share on other sites

So you would start by creating two form inputs: one for the attribution (the person whose quote it is) and another for the quote itself. Then you'd want to store this in a text file on a single line (i.e., as a single record), using some sort of unique demarcation character. I *believe* the book discusses that concept. Then the script that displays the quotes would read in the line, divide the record on that demarcation character, and be able to display the quote and the attribution separately.

  • Upvote 1
Link to post
Share on other sites

Larry, I really appreciate you writing back. I understand conceptually what you are saying but I am still stumped. I've reread the chapter, scaned the book and tried using google for a a more precise answer as to how to implement the solution. If possible, can you spell it out for me or perhaps point me to the chapter where I am going to find the info I need. Thank you very much.

Link to post
Share on other sites

First off I just want to again say thank you so much for writing back. I can't tell you how much I appreciate it.

 

To answer your question, yes, I created one input field named quote and one named attribute. I made them both sticky. At this point they are writing to the quotes.txt doc on seperate lines. I'm not sure how to get them to be written on the same line or how to read them back as seperate values.

 

<form action="add_quote_pursue.php" method="post">

<textarea name="quote" rows="5" cols="30"><?php if (isset($_POST['quote'])) {

print htmlspecialchars($_POST['quote']);

} else { print 'Enter your quotation here.';} ?></textarea><br /><br />

<textarea name="attribute" rows="1" cols="30"><?php if (isset($_POST['quote'])) {

print htmlspecialchars($_POST['attribute']);

} else { print 'Attributed by';} ?></textarea><br /><br />

<input type="submit" name="submit" value="Add This Quote!" />

</form>

Link to post
Share on other sites

Okay, good. Now to have them be written on one line is easier than you think: don't put a \n between them, just put one at the end of the line to be written. But use some sort of unique character between them, like |. So you would write quote|attribution\n to the quotes doc.

Link to post
Share on other sites

Okay, I got the file writing and reading the quote and attribute together!! Unfortunately, I'm still not sure how I would display multiple random quote/attribute pairs. Here is what I did to get the files to write and read:

 

write:

file_put_contents($file, $_POST['quote'] . '|' . $_POST['attribute'] . PHP_EOL, FILE_APPEND | LOCK_EX);

read:

$file = 'txt/quotes.txt';

$fp = fopen($file, 'rb');

$array = fgetcsv($fp, 200, '|');

print 'quote: ' . $array[0] . ' attributed by: ' . $array[1];

Link to post
Share on other sites

That's a good start. However, if you read in only 200 characters, you're not likely to grab an entire line. I would use file() to grab the entire text file contents. Then pick a random line from that array. Then split that line into its pieces.

Link to post
Share on other sites

I believe I need to use fgetcsv to separate the data from the random line. I don't know how to this:

 

//Read the files contents into an array

$data = file('txt/quotes.txt');

//count items in array

$n = count($data);

// Pick a random item

$rand = rand(0, ($n - 1));

// Here is where I get lost. I believe to separate the data I need to use fgetcsv. This requires a file pointer which requires a variable for the file location. I don't know how to use fgetcsv with the $rand array.

$file = 'txt/quotes.txt';

$fp = fopen($file, 'rb');

$array = fgetcsv($fp, 2000, '|');

print 'quote: ' . $array[0] . ' attributed by: ' . $array[1];

Link to post
Share on other sites

I got it!! I actually forgot that I could use explode. I was thinking the only way to separate the data was to use fgetcsv(). I'm reading the book again but I am going to try to continue with the next pursue question as well. Thank you so much for your help!

 

<?php // view quote pursue.php

//Read the files contents into an array

$data = file('txt/quotes.txt');

//count items in array

$n = count($data);

// Pick a random number

$rand = rand(0, ($n - 1));

// Pick random line

$line = $data[$rand];

//Separate the data

$quoteattribute = explode('|', $line);

//Print the data

print 'quote: ' . $quoteattribute[0] . '<br />attributed by: ' . $quoteattribute[1];

?>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...