file_get_contents

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

Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

file_get_contents

Post by Mightywayne »

First off, I'm sorry I've been asking so many questions. I don't really like to unless I have to, and I've been doing research and stuff.

So, since (points to sig), I figured I wanted to do this:

When people sign up, they choose a character name from a generated list. So I figured I could just make a .txt file, and read the names available, from there. So I'd put each one on a seperate line. Dandy, and I came across this function.

file_get_contents

I'm preeeeetty sure that's the one I want. But I don't understand how I'd make it read from a certain line and STOP reading at that exact point. Also, I was wondering how I would get that input into an output and have people select that (like using checkboxes) but if you feel overwhelmed and just want to answer the first, that'd be fine. :) :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try file().
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Thanks, though I still amn't getting how exactly to get the correct line, I'm sure I'll figure it out when I get home.

Now, about retrieving from the output...?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Retrieving from the output?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Well, you know, it gives them the name, and I guess it'd be like a checkbox as to the one they want, and then they hit enter and it sends the output to the next file. But if the output's always changing- wow. Wait I think I got it.

So then it would just set the input form's... name/value to what their name is? Is that correct? If so, perfecto. :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can this be handled with a database instead of a file?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Well, yeah I guess... but why would I choose random things from a database, when I can just do a file...? That'd even be an extra query. I guess it'd be easier, but that's really no excuse unless this file thing is absolutely extreme.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The amount of time it takes to execute the query to get the data (and resources related to the fetch) versus the time it takes to traverse a file system and handle conditonal checks seems like a swing in favor of using a database. Plus, it is less code.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

You know. You're right. I don't even WANT to make the damn file, really. Okay. So basically you suggest that I manually (with a script) add values to a database and just reference them, right?

So how would I just randomly pick things from a database? I figure it like setting a variable to rand(1,200) and then finding entry # according to that.

Usually I wouldn't ask but I've got so many other things to code tonight, I don't want to spend so much time on this one part. You guys are great. <3

Edit: Oh fine. I got it. ;) Tankies.

Edit2: Oh wait no I don't. :/ I'm such a mess. I still don't know how to pick something random from the database.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

SELECT * FROM `table` ORDER BY RAND() LIMIT 1;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: file_get_contents

Post by John Cartwright »

Mightywayne wrote:Also, I was wondering how I would get that input into an output and have people select that (like using checkboxes)
If your only have a couple names you want to store, I don't see anything wrong with using a textfile -- although this stuff is generally done in a database.

To generate the html output of text boxes, you would do something along the lines of:

Code: Select all

//load file into array
$names = file('characters.txt');
//check our $names array is populated
if (count($names)) {
   //loop the names to generate radio buttons
   foreach ($names as $name) { 
      echo '<input type="radio" name="character" value="'. $name .'"> '. $name .'<br />';
   }
}
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Everah wrote:

Code: Select all

SELECT * FROM `table` ORDER BY RAND() LIMIT 1;
Ah, the simplest of problems can be solved so easily. Thanks m'man.

And JCart, it's over 200 names!!! What happens is, I gotta have 200 male first names, 200 female first names, and 300 last names. They combine very sexily. So I suppose Everah's suggestion is just a lot better in this case.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Hey sorry to bother you guys again, I know it must seem like the problem is already solved. And frankly, it is. But one thing remains. I'm typing this out, right, and it looks like this. (I'm using a PHP script)

Code: Select all

$sql4 = "CREATE TABLE girlnames
(
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
name1 varchar(40),
And I'm thinking to myself, "Yeah, 200 names isn't that bad."

But I gotta do that for lastnames, too. Of course I can copy and paste, but another thing that irks me is that I'm also going to have to make another script which adds all these names in! Like "set value (name1, name2)" and typing all that out, and then on top of that, ENTERING all... 700 names!

I'm basically wondering if there's an easier way to do this, or if I should order me and the girl and pizza tonight. o.o;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why would you have to enter all the names? You have them in text files already, right?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

No. :X If I did, I definitely would've done the text file thing.

Why, are you saying that if I did have them in a text file, I could do this easier?!

Edit: Perhaps it's possible to, when making the table, add a value to it instantly?
Last edited by Mightywayne on Sat Mar 10, 2007 5:59 pm, edited 1 time in total.
Post Reply