PHP - Making A Text Adventure In Php-very Basic/beginner
Is this possible? I have around 2 weeks for a computer science project and I want to do this in PHP. However, I have just started learning PHP, and since I'm so short on time I'm going to see if I can learn how to code this while learning basics of PHP.
So basically, can anyone give me a simple and good way of starting this? I have no idea where to begin. I was thinking of doing rooms in a coordinate system with North, South, East, West buttons and a pick up item button and an attack button. So can you guys help? I would also like to know how to make buttons in HTML or anything that goes with Apache 2, I have no idea how that works. Right now I have PHP set up with Apache 2, so I can navigate to the file with the code in the browser and run the code. Similar TutorialsFirst of all hi everyone, this is my first post as you can see. And i have my first task in PHP, now i warn you i have no experience or know-how whatsover in PHP so please bear with me. My first task is to extend some PHP code to add some functions. the code weve been given already is capable of mutiplication of two variables x and y. Here it is anyway. 1. View the code and copy/paste it into an editor and save it to your own workspace. 2. Extend the script to perform the four basic arithmetic functions : +, -, *, / and return a result. 3. Extend the script again to move the calculations into a function and make use of this function. Code: [Select] <?php /* ====================================================== PHP Calculator example using "sticky" form (Version 1) ====================================================== Author : P Chatterjee (adopted from an original example written by C J Wallace) Purpose : To multiply 2 numbers passed from a HTML form and display the result. input: x, y : numbers calc : Calculate button pressed Date: 15 Oct 2007 */ // grab the form values from $_HTTP_POST_VARS hash extract($_GET); // first compute the output, but only if data has been input if(isset($calc)) { // $calc exists as a variable $prod = $x * $y; } else { // set defaults $x=0; $y=0; $prod=0; } ?> <html> <head> <title>PHP Calculator Example</title> </head> <body> <h3>PHP Calculator (Version 1)</h3> <p>Multiply two numbers and output the result</p> <form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>"> x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/> y = <input type="text" name="y" size="5" value="<?php print $y; ?>"/> <input type="submit" name="calc" value="Calculate"/> <input type="reset" name="clear" value="Clear"/> </form> <!-- print the result --> <?php if(isset($calc)) { print "<p>x * y = $prod</p>"; } ?> </body> </html> Firstly how do i run this code, i have Xamp installed if thats any good? Secondly, do i need two seperate files. i.e. calculator.php for the php code and calculator.html for the form? Thirdly i have no idea how to implement the divide, add and subtract functions. should i do a drop down menu where the user should select what operator they want? or is it more simple than that. Any help greatly appreciated. Please let me know if this post is okay. Code: [Select] <?php $db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password if(!$db) die("no db"); if(!mysql_select_db("simple",$db)) if(!mysql_select_db("regis",$db))//database name die("No database selected."); if(isset($_POST['submit'])) //if submit button push has been detected { $message=$_POST['message']; if(strlen($message)<1) { print "You did not input a message"; } else { $message=strip_tags($message); $IP=$_SERVER["REMOTE_ADDR"]; //grabs poster's IP $checkforbanned="SELECT IP from admin where IP='$IP'"; $checkforbanned2=mysql_query($checkforbanned) or die("Could not check for banned IPS"); if(mysql_num_rows($checkforbanned2)>0) //IP is in the banned list { print "You IP is banned from posting."; } else if(strlen($message)>=1) { if (isset($_POST['sub'])) { if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM registration WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } //this makes sure both passwords entered match if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } // here we encrypt the password and add slashes if needed $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['pass'] = addslashes($_POST['pass']); $_POST['username'] = addslashes($_POST['username']); } // now we insert it into the database $insert = "INSERT INTO registration (username, password) VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; $add_member = mysql_query($insert); die("<meta http-equiv=\"refresh\" content=\"0; url=login.php\">"); } else { echo "<form action='login.php' method='post'> <table border='0'> <tr><td>Username:</td><td> <input type='text'name='username' maxlength='60'> </td></tr> <tr><td>Password:</td><td> <input type='password' name='pass' maxlength='10'> </td></tr> <tr><td>Confirm Password:</td><td> <input type='password' name='pass2' maxlength='10'> </td></tr> <tr><th colspan=2><input type='submit' name='sub' value='Register'></th></tr> </table> </form> "; } } /* if($_POST['username'] && $_POST['pass']) { $name = mysql_query("SELECT * FROM Persons"); $thedate = date("U"); //grab date and time of the post $insertmessage="INSERT into mesej (name,IP,postime,message) values('$name','$IP','$thedate','$message')"; mysql_query($insertmessage) or die("Could not insert message"); } */ } } print "<form action='' method='post' name='form'>"; print "Your message:<br>"; print "<textarea name='message' cols='40' rows='2'></textarea><br>"; print "<a onClick=\"addSmiley(':)')\"><img src='smile.gif'></a> "; print "<a onClick=\"addSmiley(':(')\"><img src='blush.gif'></a> "; print "<a onClick=\"addSmiley(';)')\"><img src='images/wink.gif'></a> "; print "<input type='submit' name='submit' value='submit'></form>"; print "<script language=\"Java Script\" type=\"text/javascript\">\n"; print "function addSmiley(a)\n"; print "{\n"; print "document.form.message.value += a;"; print "document.form.message.focus();\n"; print "}\n"; print "</script>\n"; print "<br><br>"; ?> Screenshots After i hit submit button with message inside it, it appear like this : My main problem is when it goes to registration phase im do not want that message text area appear so in my code which is wrong and where im need to redo sir? Hi there, I'm new to PHP about 5month (previously i don't have any programming background), i study the basic PHP through online. i found a tutorial from : http://www.phpwebcommerce.com/ , and there is some error in this tutorial + i need customize this tutorial for my site. I'm here to ask , is the tutorial suitable beginner like me ? is the tutorial consider for advanced used ? i able to solve some error in this tutorial but it take too long. Can give a solution ? should i give up this tutorial on my site ? or just continue find solution ? but i'm already spent almost 2month in this tutorial. so far, i left shipping cost cant find solution...(but i have try do it for 2weeks) Thank you. Hi, I'm trying to set up a mysql database on my laptop, XP, and connect to a test database using php. I've created the database called 'testdb' with user account 'test' and password 'password. The code I'm using to try and connect to the test datase is the following; $dbconnect=mssql_connect("localhost", "test", "password"); But when I open the page in a web browser, I am getting the following error; Fatal error: Call to undefined function mssql_connect() in <file_location> I'm hoping I'm doing something simple here, but, I've looked up forums and this seems to be the code to use to connect. Anyone who could help would be greatly appreciated. Thanks. Hi all messing about with php , trying to get my head round the basics : ok I have a file called test.php with this in it <? $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); ?> when I run this instead of creating a blank text file called testfile.txt all it does is repeat the code in the command box window as below C:\php>php -f c:\php\test\test.php <? $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); ?> C:\php> Anyone got a clue as to why it wouldnt create a text file please? hi there i am a beginner in PHP and i really would like some help with this.....
i need to make use of the date() function to retrieve the current date. Use the split() function to retrieve the day month and the year from the current date. and the calculations to display the age.
if anyone could help me with this it would be amazing.
thank you!!
Attached Files
newagecalc.php 1.56KB
0 downloads First of all, sorry for my bad english. I have this: PHP Code: [Select] $link = file_get_contents("http://www.imdbapi.com/?i=tt1285016"); $json = json_decode($link,true); echo $json["Title"]; and I want to replace tt1285016 with $info["id"]; (this is something from mysql, first time when i use ). If I put echo $info["id"]; it return exactly what i need: tt1421545. How can I do that? thank u very much and sorry again for bad enlighs, not native language. EDIT: Sorry for bad section, first time when i come here. Hello, For all purposes, I am a complete beginner. I just know the basics of passing form data through to an email, and displaying the text of a variable on the next page via ECHO, POST, etc.. My situation is that I do not know what sort of code to use to accomplish the job of what I want done. I have created a form to be used for displaying insurance information. This form allows users to select the following: To Get Started: State You Live In: Car Information: Model Year: Original Listing Price: Your Car Currently Is: Your Information: Do You Rent Or Own? Are You Married? Do You Have Children Under 18 Years Of Age? Gross Annual Household Salary Do You Have Health Insurance? How Much Are You Worth In Total Assets (Savings, Equity, Stocks, 401K, Car, Home, etc...) ------------------------ Next to each question, I have a box from which they can select their options (standard html form code). The form has an action and the method is post. Some code is displayed below.. Now so far I am able to display the text of whatever item they selected, by using Echo $_Post etc and the answer page... Here is my problem... I want to display to the user, "recommended insurance limits" based on the data they select from the drop down boxes. So... If a user lives in Alabama, and has an income of $150K+, I want to display a different answer than someone who only earns $30K and lives in Alabama, and the list goes on, (range of options for each question). I have no idea what sort of code to use to display the appropriate answer. I thought I could use the "if, elseif, else" statement, and simply do hundreds of variations on it for each state, but surely there is a more refined and less bloated code for doing such a thing? I would need to display a different answer for the user, for each separate option they choose. Example) Alabama, Model Year of 1995, Salary of $50K would be DIFFERENT then Alabama, Model Year of 2000, Salary of $50K, etc... Thank you for your time. Here is my php code. Code: [Select] <?php $state = $_POST["state"]; $modelyear = $_POST["modelyear"]; $carprice = $_POST["carprice"]; $carownership = $_POST["carownership"]; $homeownership = $_POST["homeownership"]; $marriage = $_POST["marriage"]; $childrenunder18 = $_POST["childrenunder18"]; $salary = $_POST["salary"]; $healthinsurance = $_POST["healthinsurance"]; $assets = $_POST["assets"]; ?> <?php if ($state=="Alabama") { echo "Alabama requires the following minimum insurance limits:<br /> $25,000 - Liability Per Person<br /> $50,000 - Liability For Total Persons<br /> $25,000 - Property Damage Total"; } elseif ($state=="Alaska") { echo "Alaska requires etc..."; } else { echo "You did not select a state."; } ?> Hi guys, sorry for the beginner issues here. after following a tutorial i come across a little problem! Everything works just fine, no errors but my page source only shows the xml tags and not the actual products <?php $link = mysql_connect("localhost","Joao","password"); mysql_select_db("brimelow_store"); $query = 'SELECT * FROM products'; $results = mysql_query($query); echo "<?xml version=\"1.0\"?>\n"; echo "<products>\n"; while ($line = mysql_fetch_assoc($results)); { echo "<item>" . $line["product"] . "</item>\n"; } echo "</products>\n"; mysql_close($link); ?> source = <?xml version="1.0"?> <products> <item></item> </products> I have checked the database names and they all match... im confused. can i get some help pls? hey guys, I am very new to PHP and wanted to create a simple form script. Somehow it doesnt work... can you help me? Help is highly appreciated!! here is the code: Code: [Select] <?php $admin= 'name@email.com'; // 1. Message to the admin $subject1= 'You have one new subscription'; $message1.= 'Email: '.$email."\n\n"; $message1.= 'Name: '.$name."\n\n"; // Sending mail mail($admin, $subject1, $message1, "From: $email"); header('Location: http://www.youtube.com'); ?> Hi, Basically I just downloaded wamp and got php and mysql etc. I created a database for my "new social network to-be site" which I am trying to develop.. now I want to create a registration page and am trying to follow a guide on another website.. it lets you download a zip with all the php files required. So now, I am totally confused. Here is the guide: http://www.html-form-guide.com/php-form/php-registration-form.html But I dont know what to do with any file or where to put it.. any basic help.. Just to confirm I want to make a login/registration/user database type thing First, I apologize if this is the wrong venue, but it seemed the best place to ask question about code. The code below is very simple most likely, but I am having the hardest time visualizing how it's run. It would be most helpful if someone could point out the error of my thinking that would be awesome. This is literally the first php lesson I have ever taken.
I'm a very visual, hands on kind of learner. It would be most appreciated to get direction into the best way to learn PHP as a visual learner. Thanks!
<?php // I'm following an example online and need an explanation to this. This first part is creating // a class called User. class User { // This is a variable set to public. Now in the lesson I discovered public and private. Why is // public and private important? Could the code just run on $age? public $age; // This is a public function called __construct. Could I name this anything and the function // would still work? What is important about __? public function __construct($age) { // Ok, why is $this used? Is it correct in assuming that any word with a "$" assigned to it in // this class will refer to age? $this->age = $age; } // This is the 3rd function called getAge(). Why is getAge() blank? public function getAge() { echo $this->age; } } // Shoudln't this be at the top? And isn't there a cleaner way to output the age of a new user? // This seems to take up way too much coding just to display the name of a variable? $brad = new User(31); // I'm so confused. $brad->getAge(); Hi all For example if you see this code: public function __set($varName,$varValue) { $this->$varName = $varValue; if ($varName == "varOne") { $this->varTwo = $this->varOne * 9; } } Why somewhere dollar sign exists and somewhere not: $this->$varName , $this->varTwo ? Thanks Hi all, I hope I am in the right place. I'm new to php/mysql so please be patient with me. I am trying to create a login script to validate username and password against a database I have created. I am not getting an error message but even when I enter correct username/password details from the database to test the code I am getting the message at the bottom of the script. I can't figure out why as I thought I had all bases covered, but obviously not. Here is my code. Thanks for any help in advance. P.S. I have checked the names against my form and they are correct. <html> <html lang="en"> <head> <meta charset="utf-8" /> <title>USER LOGIN</title> </head> <body> <?php if(empty($_POST['name'])){ $name=NULL; echo "Sorry, you forgot to enter your username.</br>"; }else{ $name=$_POST['name']; } if(empty($_POST['password'])){ $password=NULL; echo "Sorry, you forgot to enter a password.</br>"; }else{ $password=$_POST['password']; } $connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') OR die("Could not connect to server"); $username = stripslashes($name); $password = stripslashes($password); $username = mysql_real_escape_string($name); $password = mysql_real_escape_string($password); $info = "SELECT 'username', 'password' FROM USERS WHERE 'username'='$username' and 'password'='$password'"; $return=@mysql_query($info); $rows=@mysql_num_rows($return); if($rows==1){ session_register("username"); session_register("password"); header("location:admin.php"); echo "Hi $username. You are now logged in."; }else{ echo "You have entered incorrect details. Please check your login details and try again."; } ?> </body> </html> <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="stylesheet.css"> <meta charset="utf-8"> </head> <body> <?php $array = array(); echo "<table>"; for ($z=0; $z <=9; $z++) { echo "<tr>"; for ($s=0; $s <=9; $s++) { $r = mt_rand (1, 100); array_push ($array, $r); if ($r %2 != 0) { echo "<td bgcolor = red>"; echo $r; echo "</td>"; } else { echo "<td bgcolor= lightgreen>"; echo $r; echo "</td>"; } } echo "</tr>"; } echo "</table>"; echo "<br>"; echo "<table>"; sort($array); $arraysplit = array_chunk($array, count($array)/10); for ($z=0; $z <=9; $z++) { echo "<tr>"; for ($s=0; $s <=9; $s++) { if ($r %2 != 0) { echo "<td bgcolor = red>"; echo json_encode (arraysplit); echo "</td>"; } else { echo "<td bgcolor= lightgreen>"; echo json_encode ($arraysplit); echo "</td>"; } } echo "</tr>"; } ?> </body> </html> Hey absolute beginner here. Im working on a table that gives random numbers out and then colorizes them if they can be devided by 2. That works fine. Now i want to get those randomly generated numbers into a second table but sorted. I already managed to put the numbers i generated into an array and sort it but i cant figure out how to echo them 1 by 1 into the table. Maybe you can help me out. Well I have made a form in html and i need the information which is entered into the form to be shown to the user in a different page so I have a Ticket request page and i have made a Confirmation_Page.php Ticket request page: has this <form id="form1" name="form1" method="get" action="Confirmation_page.php"> The ticket request page has (Just one example so eg. the forename: </p> <p> <label>Forename <input type="text" name="Forname" id="Forname" /> </label> </p> <p> And corresponding to this in the Confirmation_Page.php it is : Your forename is : <?php echo $_GET["Forename"];?><br /> Now the thing is when I enter info in the form inside the ticket request page and click submit It takes me to the Confirmation_Page.php HOWEVER It doesn't show me the information I've inputed it into the form Eg.It only shows Your forename is : please help me I need to hand this in by monday :( thanks to everyone who tries to help me :l Hi, I've been using this tutorial all afternoon, and it's been great, but I wonder if it's outdated... http://www.tizag.com/phpT/index.php I'm using WAMPSERVER 2.2 and PHP 5.3.10 Any recommendations for good up-to-date/relevant tutorials? Thanks I am a total beginner in PHP and i am trying to fix a script i have. I am trying to put filter search boxes over every column (it is a products table). Once the search is made the entire row(s) should be displayed with the search term in it corresponding to the column it was made in. I am attaching a mockup of what i am trying to do. I know i am being a drag in this forum but i am really looking for the PHP code to do this. thanks So, I'm going through a tutorial in my PHP book and we're using the code below. The page shows up and connects to the database (no error reported), but no data comes up. I'm wondering if I'm just missing something small or what? Let me know if you can help <html> <head> <title>Search Results</title> </head> <body> <h1>Book-0-Rama Search Results</h1> <?php //create short variable names $searchType=$_POST['searchType']; $searchTerm=trim($_POST['searchTerm']); if(!$searchType || !$searchTerm){ echo 'You have not entered enough information to process the search'; exit; } if(!get_magic_quotes_gpc()){ $searchType = addslashes($searchType); $searchTerm = addslashes($searchTerm); } echo $searchType; echo $searchTerm; @ $db = new mysqli('localhost','root','root'); if(mysqli_connect_errno()){ echo 'Error: Could not connect to the database man...'; exit; } $query = "SELECT * FROM books WHERE ".$searchType." LIKE '%".searchTerm."%'"; $result = mysqli_query($query); $num_results = mysqli_num_rows($result); echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0;$i<$num_results;$i++){ $row = mysqli_fetch_assoc($result); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br/>Author: "; echo stripslashes($row['author']); echo "<br/>ISBN: "; echo stripslashes($row['isbn']); echo "<br/>Price: "; echo stripslashes($row['price']); echo "</p>"; } $result->free(); $db->close(); ?> </body> </html> |