# Random Quoting + User Adding

**URL:** <https://forum.kirupa.com/t/random-quoting-user-adding/185603>\
**Category:** programming\
**Created:** [April 14, 2006, 4:14pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603 "2006-04-14T16:14:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 14, 2006, 4:14pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/1 "2006-04-14T16:14:06Z")

</div>

Im having a hard time doing this. This is basiclly what I want:

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [April 14, 2006, 4:21pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/2 "2006-04-14T16:21:07Z")

</div>

are you trying to do it in flash or no? also, are you going to use a mysql db or text files? preferably you would use a mysql db.

---

<div class="post-metadata">

**Author:** ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)\
**Post date:** [April 14, 2006, 4:41pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/3 "2006-04-14T16:41:24Z")

</div>

AJAX… 🙂 Of course it may not be necessary depending on the setup of your site/page.

Have Javascript grab the quote from the text field (and check it). Send it to PHP (or whatever) to get stored into the database (db would be best but you could use a file - text or XML). Then when you get a response that everything went well, put the quote on the page (or show an error if something went wrong).

---

<div class="post-metadata">

**Author:** ![Sniper\_Jo](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sniper\_Jo](https://forum.kirupa.com/u/Sniper_Jo)\
**Post date:** [April 14, 2006, 4:51pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/4 "2006-04-14T16:51:48Z")

</div>

oh common, u dont need to use AJAX for somthing like that, i think u need to describe what you want better

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 14, 2006, 5:03pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/5 "2006-04-14T17:03:35Z")

</div>

Flatfile or MySQL?

---

<div class="post-metadata">

**Author:** ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)\
**Post date:** [April 14, 2006, 5:21pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/6 "2006-04-14T17:21:11Z")

</div>

> [@Sniper Jo](#):
>
> oh common, u dont need to use AJAX for somthing like that, i think u need to describe what you want better

Depends. If that’s part of a larger page why reload the entire page each time someone submits a quote? Send it via get/post and you’re just reloading the entire page when that’s not necessary - bandwidth gets sucked up when it doesn’t need to. If it’s on a smaller page then yeah, AJAX wouldn’t be needed. But because I don’t know any more details about the set up I was simply offering Crayon-Inc another option. And it’s also why I had put:

> Of course it may not be necessary depending on the setup of your site/page.

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 14, 2006, 5:28pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/7 "2006-04-14T17:28:16Z")

</div>

> [@Ankou](#):
>
> Depends. If that’s part of a larger page why reload the entire page each time someone submits a quote? Send it via get/post and you’re just reloading the entire page when that’s not necessary - bandwidth gets sucked up when it doesn’t need to. If it’s on a smaller page then yeah, AJAX wouldn’t be needed. But because I don’t know any more details about the set up I was simply offering Crayon-Inc another option. And it’s also why I had put:

Either way I’m sure he can manage the front-end the way he’d like it. It’s just the backend that matters, and that’s normally the same ajax or not 🙂 (probably slight differences, but VERY slight).

Oh and I prefer not to use AJAX, too javascripty 🙂

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 14, 2006, 5:55pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/8 "2006-04-14T17:55:39Z")

</div>

I have acsess to mySQL, so yeahhh flatfile or mySQL would be good =)

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 14, 2006, 6:09pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/9 "2006-04-14T18:09:24Z")

</div>

> [@Crayon-Inc](#):
>
> I have acsess to mySQL, so yeahhh flatfile or mySQL would be good =)

I think I have some horrid example of a quote system in flatfile from about a year ago. Let me dig it out.

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 14, 2006, 6:12pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/10 "2006-04-14T18:12:35Z")

</div>

^  
Ok that would be nice…

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 15, 2006, 3:48am UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/11 "2006-04-15T03:48:38Z")

</div>

Anyone else have a clue how to do this?

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [April 15, 2006, 7:43pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/12 "2006-04-15T19:43:34Z")

</div>

here’s basically what you need to learn w/php and mysql:

1. passing variables to php through an html form
2. mysql INSERT queries
3. php rand function
4. mysql SELECT queries

you can search for those items up the wazoo and find tons of info and tutorials. i would try it first, then post code here if you are having trouble.

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 15, 2006, 8:04pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/13 "2006-04-15T20:04:55Z")

</div>

I couldn’t find that thing.

I’ll just spell it out for yaa…

In order to submit, you have the form submit a post to a php file. In the php file, accept the variable, do all of your syntax checking to make sure it’s nice and sexy, and if it is, INSERT into the mysql database. When displaying, you can just use the query SELECT \* FROM `table` ORDER BY rand() LIMIT 1. Then of course display the results.

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 15, 2006, 9:15pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/14 "2006-04-15T21:15:30Z")

</div>

I’ve decided that I was going to do it flat-file, this is what I’ve started…

```auto

<?php
	//Get the stuff in the textbox
    $add = $_GET['bananaphone'];
	//Put it in a var
    $entry_line = "$add n";
	//Open logs.txt
    $fp = fopen("logs.txt", "a");
	//Put the stuff in
    fputs($fp, $entry_line);
	//Close logs.txt
    fclose($fp);
?> 

```

^  
Did I do anything wrong to that?

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 15, 2006, 9:38pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/15 "2006-04-15T21:38:43Z")

</div>

bananaphone 😛

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 15, 2006, 9:59pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/16 "2006-04-15T21:59:49Z")

</div>

Im doing something wrong now…

```auto

<form action="randomstart.php" method="post">
Add your quote: <input type="text" name="bananaphone" />
<input type="submit" name="Submit" value="Submit">

```

When I go to logs.txt it only shows the letter n

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 15, 2006, 10:03pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/17 "2006-04-15T22:03:12Z")

</div>

Yep. You used POST on the form, and GET in the PHP. Change the GET in the PHP code to POST. Also what’s the purpose of the n, you mean new line? If so that’s  
, or  
… not just n 🙂

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 15, 2006, 10:04pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/18 "2006-04-15T22:04:31Z")

</div>

^  
Forgot the slash… thanks 😃  
Lemme test it out

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 15, 2006, 10:07pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/19 "2006-04-15T22:07:01Z")

</div>

w00t it works… now i need to randomize the stuff and then display the quote… any ideas?

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [April 15, 2006, 10:15pm UTC](https://forum.kirupa.com/t/random-quoting-user-adding/185603/20 "2006-04-15T22:15:02Z")

</div>

Easy. [www.php.net/file](http://www.php.net/file)

$array = file(“file.txt”);  
print $array[rand(0,sizeof($array)-2)];

[Next page](https://forum.kirupa.com/t/random-quoting-user-adding/185603.md?page=2)
