PDA

Click to See Complete Forum and Search --> : A bit of help plz


Paul_so40
Mar 22nd, 2009, 05:10 PM
Hi all,

To start of with ill exsplain what im doing.

I am making a game walkthrough submitter script.

How ever i have come across a few things i need some help with doing.

Ok, so to start of with there are 3 text boxes

The 1st one they type there username into, the second They write the game title in and 3rd one they add the walkthrough to.

Then there is a drop down box with 5 selections

1) Action & Adventure
2) Driving & Racing
3) Role-Playing Games
4) War & Shoot 'Em Ups
5) Sport

On submit, it creates a page from a pre made template at walkthrough/templates/file1.html

wich consists of ( for example )
<html>
<head>
<title>{REPLACE_TITLE}</title>
</head>
<body>
MADE BY <b> {REPLACE_WITH_USERNAME} </b>
IN CATAGORY <b> {REPLACE_CATAGORY} </b>
<b> {REPLACE_DATA} </b>
</body>
</html>

so what i need is to modify the following script to do this, im just not sure on the script to fufill my needs. :( Also i would like it if it gives the file a random name ( so the same one is never used twice )

<?php
if(empty($_POST)) {
?>
<form method="post">
File Name <input type="text" name="name" size="20"><p>

Game Name <input type="text" name="title" size="22"></p>
<p>

&nbsp;Walkthrough - use as much space as you like<br>
&nbsp;<textarea rows="16" name="content" cols="51"></textarea><br>

<input type="submit" value="Create"> </p>
</form>
<?php
die();
}

$name = $_POST['name'];
$title = $_POST['title'];
$content = $_POST['content'];

$sanitized_name = preg_replace('/[^0-9a-z\.\_\-]/i','',$name);
if ($sanitized_name == $name) {

$data = file_get_contents("walkthrough/templates/file1.html");
if(get_magic_quotes_gpc())
$data = stripslashes($data);
$data = str_replace("{REPLACE_TITLE}",$title,$data);
$data = str_replace("{REPLACE_DATA}",$content,$data);

write_file("walkthrough/$name.php",$data);



} else {
echo "Filename problems. Try ".$sanitized_name;
exit;
}

function write_file($filename,$data) {
$f = fopen($filename,"w");
fwrite($f,$data);
fclose($f);
}



?>

also this script creates the script to the folder /walkthrough/ and i would like it to create it into the folder depending on what category they place it under.

Another thing that would be great ( if anyone knows how to do it ) is to also add a link to the created page, in the form of a table with the text being the game title. ( so its like a menu, that would be great ).

Any questions just ask

Thanx - all help is apreceated.

- Paul

kows
Mar 22nd, 2009, 08:14 PM
the script you posted already does what you want, you just need to add entries to replace the username/category. look at how str_replace() is used in the script you posted to find out how to do that.

if you want to change the path to where the file is saved, look at the path being used when write_file() is called. change that to include the category, like: "walkthrough/$category/$name.php".

for a random name that will NEVER be used again, you could define $name as the function time(). this will be unique to a point, but is probably not the best way to do it. the filename will be a number representing the time. you could use md5() too, but that would be 32 characters long. and even worse looking for a filename.

and finally, if you want the created file to have extra stuff in it, you need to add whatever you'd want to the template file (walkthrough/templates/file1.html). the script doesn't do that.

as a last note, doing what you're trying to accomplish using files is ugly and very difficult to maintain. you could accomplish this much, much easier using a database solution.

Paul_so40
Mar 23rd, 2009, 11:12 AM
the script you posted already does what you want, you just need to add entries to replace the username/category. look at how str_replace() is used in the script you posted to find out how to do that.

if you want to change the path to where the file is saved, look at the path being used when write_file() is called. change that to include the category, like: "walkthrough/$category/$name.php".

for a random name that will NEVER be used again, you could define $name as the function time(). this will be unique to a point, but is probably not the best way to do it. the filename will be a number representing the time. you could use md5() too, but that would be 32 characters long. and even worse looking for a filename.

and finally, if you want the created file to have extra stuff in it, you need to add whatever you'd want to the template file (walkthrough/templates/file1.html). the script doesn't do that.

as a last note, doing what you're trying to accomplish using files is ugly and very difficult to maintain. you could accomplish this much, much easier using a database solution.

Yh, the code above was written my a past admin of the site. tbh im not a php coder im a vb one. And i understand just how enoying it is when someone just posts asking for you to do all the work for themwith no sighn of attempt them selves, yet on this occashion. . . i realy am just conhuffled on doing it. and not knowing the language dosent help .. .

If someone could do me this id be realy happy lol, and trust me when i say, i will learn allot from it, and will analyse the script so i understand it, and wont have probloms with it again

Thanks

- Paul

kows
Mar 23rd, 2009, 04:56 PM
in my previous post, I loosely explained what you needed to do, and even gave you function names so that you could search and find them more easier. if you are even half accustomed to programming in VB, then copying and pasting a few function calls and changing the parameters of two functions, and declaring a few variables, should not be difficult.

in php, variables are named with scalars ($), meaning "$category" or "$name" refers to a variable. to declare or define a variable, you do it like any language would: "$var = value;", though unlike VB, you need to terminate (with the semi-colon). since you're posting to this script, you will want to make use of the super global variable $_POST, which is an array containing all of the data that you have posted to said script. if your form had an <input> tag named "category," you would refer to the value of that <input> tag by calling $_POST['category']. you can do this for your category and username.

if you won't even try, then I'm not sure what to tell you.

Paul_so40
Mar 24th, 2009, 12:01 PM
in my previous post, I loosely explained what you needed to do, and even gave you function names so that you could search and find them more easier. if you are even half accustomed to programming in VB, then copying and pasting a few function calls and changing the parameters of two functions, and declaring a few variables, should not be difficult.

in php, variables are named with scalars ($), meaning "$category" or "$name" refers to a variable. to declare or define a variable, you do it like any language would: "$var = value;", though unlike VB, you need to terminate (with the semi-colon). since you're posting to this script, you will want to make use of the super global variable $_POST, which is an array containing all of the data that you have posted to said script. if your form had an <input> tag named "category," you would refer to the value of that <input> tag by calling $_POST['category']. you can do this for your category and username.

if you won't even try, then I'm not sure what to tell you.

Do u have an example of one that would work by the use of a database?

kows
Mar 24th, 2009, 05:07 PM
edit. pressed save before I was finished. brb!!
------

no, I don't have any examples. but, I can give you something to start with:

first, you'll need to make MySQL table. if your web host has phpmyadmin, then all the better. you'll need to make a table that has about 5 fields; the ID (which should be a primary key with auto_increment), the title (varchar with a length of however long you'd like), category (either an enum [which lets you explicitly define the available categories] or a varchar of whatever length you'd like), username (another varchar), content (text), and possibly a field or two for the date added (int), and date modified (if you will allow modification -- int again). sql query to create table:
CREATE TABLE IF NOT EXISTS `game_walkthroughs` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(50) NOT NULL,
`category` varchar(30) NOT NULL,
`username` varchar(30) NOT NULL,
`content` text NOT NULL,
`date_created` int(11) unsigned NOT NULL default '0',
`date_modified` int(11) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

this is a file that would insert things (taking most of your script above):
<?php
//if the user has submitted to this script via POST, we need to insert something into our database.
if($_SERVER['REQUEST_METHOD'] == "POST"){

//we use mysql_real_escape_string() function to make sure these user variables are safe for inserting
$name = mysql_real_escape_string($_POST['name']);
$title = mysql_real_escape_string($_POST['title']);
$content = mysql_real_escape_string($_POST['content']);

/**** you need to add and define category/username yourself! *****/
//$username =
//category =

//time() is a function that will output the current time in seconds
$date_created = time();

//make an insert query to put all of this in the database
mysql_query("INSERT INTO game_walkthroughs (title, category, username, content, date_created) VALUES('$title', '$category', '$username', '$content', '$date_created');");
?>
<h1>posted successfully</h1>
<?php
}else{
//this script wasn't posted to, so that means we need to show our form
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
File Name <input type="text" name="name" size="20"><p>

Game Name <input type="text" name="title" size="22"></p>
<p>

&nbsp;Walkthrough - use as much space as you like<br>
&nbsp;<textarea rows="16" name="content" cols="51"></textarea><br>

<input type="submit" value="Create"> </p>
</form>
<?php } ?>

and this would be the file you would use to display things. you would request it using: this_file.php?id=N, where N is the ID in your database of whatever row you want to display.
<?php
if(isset($_GET['id'])){
//we don't do anything unless the "id" variable in the query string ($_GET) is defined.

//never trust user input! use mysql_real_escape_string() again.
$id = mysql_real_escape_string($_GET);

//check if this ID is valid (eg. exists)
$sql = mysql_query("SELECT * FROM game_walkthroughs WHERE id='$id' LIMIT 1");

//we use mysql_num_rows() to see how many rows were returned; if the ID is valid, mysql_num_rows() will return with a value over 1 (making it true):
if(mysql_num_rows($sql)){
//we fetch the row.
$row = mysql_fetch_assoc($sql);

/*
* we now have an associative array containing all of the information for this ID
* we can access it by referring to it like you would a $_GET or $_POST variable, but $row in this case:
* $row['id'] contains the ID
* $row['title'] contains the title
* etc.
*/

//now, spit out the contents of file1.htm into this, and replace as necessary
//again, I did two replaces; look at how I did them and finish up the rest yourself!
//I've added a call to the date() function, which will give you a nice, human readable version of the date this document was created, too.
?>
<html>
<head>
<title><?php echo $row['title']; ?></title>
</head>
<body>
Created on <strong><?php echo date("l, F js, Y", $row['date_created']; ?></strong><br />
MADE BY <b> {REPLACE_WITH_USERNAME} </b>
IN CATAGORY <b> {REPLACE_CATAGORY} </b>
<b> <?php echo $row['content']; ?> </b>
</body>
</html>
<?php } } ?>
post any questions you have, and as you'll see, you still need to add a few things yourself! I tried to be as informative as possible with comments while I was writing this up, so pay attention and read them if you're confused. comments are displayed as ORANGE in this post.

edit: had a small error in first script

Paul_so40
Mar 25th, 2009, 01:48 PM
Hi, And this is very usefull / intresting. however i have run into a problom.

Let me exsplain where i am.

So far i have run the mysql queary, and it worked a bute. all tables are now created.

i then made 2 new web pages (.php) with the code you suplied, i ran the submit file, wich after submiting posted the text "posted successfully" wich must be good.

But how do i now veiw that file?

And how can the link to the walkthrough be added to a menu on a seperate page?

Thanks

P.S Please bare with me . . . i doing something new hear with a language i dont even know lol :)

kows
Mar 25th, 2009, 04:20 PM
like I said in the first post, you can open each ID in the display page by adding "?id=N" to the URI (a query string), where N is the ID. in this case, the first item you added will have an ID of 1. so, adding ?id=1 will display the first ID.

to loop through the database and display all of the variables, you'll need a SELECT query. a SELECT statement will, well, select things from your database depending on the criteria you supply. if you want to display every single row in your table, you can do this:
SELECT * FROM table_name
but you can also add a "WHERE" clause and even a LIMIT, because you may want to only display the last 10 items in your table, or have certain criteria (like showing only items from one category).
SELECT * FROM table_name WHERE field_name='some-value' LIMIT 10
you're also able to change the way items are sorted by adding the "ORDER BY" command. you can order things either ascending or descending, and depending on any of the fields available.
SELECT * FROM table_name WHERE field_name='some-value' ORDER BY field_name DESC LIMIT 10
this query is useless to a user, though. you will need to make a WHILE loop to go through your table and display this information:
<?php
$sql = "--your query here--";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
/* now you can reference the $row array;
* it contains all of the data in your table.
* $row['id'], $row['title'], etc.
*/
//this will display a simple link.
echo '<a href="display.php?id=' . $row['id'] . '">' . $row['title'] . '</a><br />' . "\n";
}
?>

Paul_so40
Mar 25th, 2009, 05:06 PM
ok i am getting confused now.

ive uploaded the submit page to

http://walkthrough.united-gamerz.com/postwalkthrough.php

and the display code provided to

http://walkthrough.united-gamerz.com/index.php

I just cant get it to work.

the mysql queary run fine and the tables were created

could you give me a fully working code? that i could download and upload to the site.

Please dont think that i havent taken notice of your code, ive read through it and tried/thought i understand it, just i cant get it to work. and would like to learn from a working script that i could use and learn from, then try modify to my site template template etc


Thanks allot.

- Paul

P.S your being a great help to both me, and my site. Thanks

kows
Mar 25th, 2009, 06:38 PM
I've already given you (nearly) completely working scripts; you're going to have to post what you've got and what you've changed (and what errors you're getting) and I can help walk you through what you might have gotten wrong.

Paul_so40
Mar 26th, 2009, 04:28 AM
I've already given you (nearly) completely working scripts; you're going to have to post what you've got and what you've changed (and what errors you're getting) and I can help walk you through what you might have gotten wrong.

im getting no errors, i am using exactly the script that you provided. I dont have much understanding of php, and i am not sure what it is i need to edit, nor what to edit it to.

- Paul

kows
Mar 26th, 2009, 04:58 AM
well, none of the scripts require much editing. you obviously didn't read them over if it's this much of an issue! read the comments. I've commented out a few variables that you can uncomment (both of which you need to define), and I only did 2-3 replaces on the display page. lines beginning with /* or // are comments; I've tried to make it very instructional.

I feel like I'm teaching grade 9 HTML or something. I'm just here to help, not just hand you everything ;)

Paul_so40
Mar 26th, 2009, 12:39 PM
well, none of the scripts require much editing. you obviously didn't read them over if it's this much of an issue! read the comments. I've commented out a few variables that you can uncomment (both of which you need to define), and I only did 2-3 replaces on the display page. lines beginning with /* or // are comments; I've tried to make it very instructional.

I feel like I'm teaching grade 9 HTML or something. I'm just here to help, not just hand you everything ;)

i now know how it feels when i tell people i help with vb to go over what i say and go through all there probloms with them. . . thinking that im doing them good, teaching them the way it should be . . and it seems they just want everything on a plate for them.

Though i now know that thats not always what they want, and doing it there way ( just giving it to them ) your doing for them what they want of you, not what you exspect of them.

ITs not as easy as you may think. . . and though i need the script, i dont have the time to learn a new language . . .and if i find time i want to dedicate it to c++

it would be a great favor if this time you could make an exception and simpley just "hand it to me on a plate" so i could move onto getting the site to be what i want it to be.

However i will look over what u give me ( if you would be kind enough to do so ) and ill add it to my code bank for further use.

Thanks

- Paul

kows
Mar 26th, 2009, 02:28 PM
honestly, you aren't learning a new language by just reading through some code and deciphering what a function or two does. from what you've told me, (that you'll just look at the "completed" script and figure it out from there) there should be no issue; you'd be doing this same thing in the end, anyway. if I were to add anything to what I've given you already, I would just be reiterating definitions and printing of variables -- there is really nothing more to it. I've even told you specifically, in the comments, which lines to reiterate.

your to-do list:

add username and category fields to the previous HTML you provided for insert script (use <input> tags)
define $username and $category in insert script (use $_POST variables)
replace the "replacement text" in the display page following the guidelines in the comments for that script. look at the text between <title> and </title> for how to do this.


I'm sorry, but in the two days since I posted help for you, you could have taken 5 minutes to at least read the comments in the code I've written. if you are unwilling to do that and put in a solid effort, then I feel very reluctant to help you at this point in time.

Paul_so40
Mar 26th, 2009, 04:17 PM
honestly, you aren't learning a new language by just reading through some code and deciphering what a function or two does. from what you've told me, (that you'll just look at the "completed" script and figure it out from there) there should be no issue; you'd be doing this same thing in the end, anyway. if I were to add anything to what I've given you already, I would just be reiterating definitions and printing of variables -- there is really nothing more to it. I've even told you specifically, in the comments, which lines to reiterate.

your to-do list:

add username and category fields to the previous HTML you provided for insert script (use <input> tags)
define $username and $category in insert script (use $_POST variables)
replace the "replacement text" in the display page following the guidelines in the comments for that script. look at the text between <title> and </title> for how to do this.


I'm sorry, but in the two days since I posted help for you, you could have taken 5 minutes to at least read the comments in the code I've written. if you are unwilling to do that and put in a solid effort, then I feel very reluctant to help you at this point in time.

Ive read through the comments, but its asking me to do something i dont know how to do.

i totaly understand how your seeing things, i probly wouldent want to help someone asking for so much my self.

I think ill just have to go back to using writing files, since its the only working way i can do it.

Thanks for your time, and your scripting was great.

kows
Mar 26th, 2009, 07:42 PM
well, this is a forum for asking for help. you're not asking for help, you're just asking me to do it for you. if you have any problems understanding what's going on in the script, then I am here to provide more information so that you can understand it better. simply looking at it, saying you don't get it, and then asking me to do it for you isn't going to help you get any further.

the way you were writing files (with what you wanted to add to it) was just as incomplete as my scripts were (on purpose).

if you are having so much trouble with it, you could probably hire someone to do it for you.