Canadiengland Posted March 16, 2007 Share Posted March 16, 2007 probably simple, but im a noob to php, and the only way ive been figuring stuff out is by looking at other people's work and looking at how its scripted... what im making is an online game, and i want each player to have an individual profile that other players can view. ive got a mysql db with the players id, name, attack, defence, blah blah - i know how to print all that... but heres what im thinking: .php?id=1 would somehow be interpreted by the page, and the id would fill in the blanks with the corresponding id in the mysql... therefore id=1 would have all the stats and the name of user 1 from the game... right? (and id=2 would have player 2 and so forth?) if im on the right track thats good, ive learnt a bit then... but yeah if you have any clue as to what im talkin' about please help! thanks! ADD+++++ also, i know this is off topic but its another problem i have... i dont know if anyones familiar with the game OUTWAR, but in the old version of it the attacks would look like this: you attack for 23890234 damage! (wait 1 second) you are hit for 23834 damage! (wait 1 second) youve won the fight! each "attack" would show up one after another, 1 second apart. i tried searching around for how to do this, the closest i got was the "sleep" function, but that just waits the amount of seconds before loading the entire page, not just the 1 line of text. if youve got answers for either questions, i thank you a million and one. cheers Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 16, 2007 Share Posted March 16, 2007 For the profiles: if(!$_GET['u']) { print "Invalid use of file"; } else { $q=mysql_query("SELECT * FROM users WHERE id={$_GET['u']}",$c); if(mysql_num_rows($q) == 0) { print "Sorry, we could not find a user with that ID, check your source."; } else { $r=mysql_fetch_array($q); print " ".$r['name']." s Profile:";} the link to the profiles would be: $id1 = $_SESSION['id']; $id = mysql_query("SELECT id FROM users WHERE id=$id1"); print "<a href='blahblah.php?u=$id'>View $id s Profile</a>"; and for the attacking.. Its done in javascript I think, dont know how your gonna do that. Quote Link to comment Share on other sites More sharing options...
Canadiengland Posted March 16, 2007 Author Share Posted March 16, 2007 thanks so much eh - so my code right now looks like this: <?php //killmonster amin index include 'connect.php'; session_start(); ?> <link rel="stylesheet" href="style.css" type="text/css"> <?php $_GET['id']; if (isset($_SESSION['player'])) { $player=$_SESSION['player']; $userstats="SELECT * from km_users where ID='$id'"; $userstats2=mysql_query($userstats) or die("Could not get user stats"); $userstats3=mysql_fetch_array($userstats2); this isnt finding the users attack and so on... this is when my url is .php?id=5 Quote Link to comment Share on other sites More sharing options...
only one Posted March 16, 2007 Share Posted March 16, 2007 $player= mysql_query("SELECT * FROM km_users WHERE ID='$id'"); while ($userstats2 = mysql_fetch_array($info)) { just use a while loop... Quote Link to comment Share on other sites More sharing options...
Canadiengland Posted March 16, 2007 Author Share Posted March 16, 2007 instead of the whole $player=$_SESSION['player']; $userstats="SELECT * from km_users where ID='$id'"; $userstats2=mysql_query($userstats) or die("Could not get user stats"); $userstats3=mysql_fetch_array($userstats2);?? Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted March 16, 2007 Share Posted March 16, 2007 thanks so much eh - so my code right now looks like this: Please be aware that your output is not valid HTML or XHTML. This is fine if you're just noodling around and the output is satisfactory, but a serious application should adhere to standards (or at least have a rational for violating them). $_GET['id']; This line does nothing and $_SESSION['player'] will never get set. It's not at all clear to me why the previous poster recommended using a session variable for this purpose. Here's what I would suggest: <?php // ... assuming database connection has already been made ... // set content type as a lazy way to maintain whitespace formatting of var_dump() // header() must be called before outputting any content to the browser, unless buffering is used. header('Content-type: text/plain'); // check that we have been given an ID and that it is a numeric value if (isset($_GET['id']) && is_numeric($_GET['id'])) { // query the database; I'm using mysqli here so it looks slightly different than the mysql extension // note the special formatting of the variable $statsResult = $database->query("SELECT * FROM km_users WHERE ID = {$_GET['id']}"; // this returns an associative array $userStats = $statsResult->fetch_assoc(); // make an ugly dump of the row; you can clean it up later var_dump($userStats); } else { echo "<p>Error: no user ID provided.</p>"; } ?> I elected to give an example using mysqli because the mysql extension is deprecated and is not even bundled in the standard PHP configuration for 5.2.x. mysqli is a lot more powerful too, supporting stuff like prepared statements. Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted March 16, 2007 Share Posted March 16, 2007 $player= mysql_query("SELECT * FROM km_users WHERE ID='$id'"); while ($userstats2 = mysql_fetch_array($info)) { just use a while loop... I believe you misunderstood. I believe the OP is attempting to retrieve a single row from the database, not multiple rows. I believe the OP was disappointed because the code written did not provide fields, and I do not see how it possibly could have. Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted March 16, 2007 Share Posted March 16, 2007 i dont know if anyones familiar with the game OUTWAR, but in the old version of it the attacks would look like this: you attack for 23890234 damage! (wait 1 second) you are hit for 23834 damage! (wait 1 second) youve won the fight! each "attack" would show up one after another, 1 second apart. i tried searching around for how to do this, the closest i got was the "sleep" function, but that just waits the amount of seconds before loading the entire page, not just the 1 line of text. if youve got answers for either questions, i thank you a million and one. cheers It's not that sleep() delays the display of the page. It's that the output is going into Apache's buffer and is only being flushed at the end of the transaction. What you want is to flush the buffer after each status change, which you can do with the flush() function. sleep() delays program execution, not output. Consider one possible implementation: while ($fighter1->getHitpoints() > 0 && $fighter2->getHitpoints > 0) { echo $fighter1->attack($fighter2); // you attack for 23890234 damage! flush(); // push the output rather than waiting for script to complete sleep(1); // sleep for a second echo $fighter2->attack($fighter1); // you are hit for 23834 damage! flush(); sleep(1); } // evaluate fight result if ($fighter1->getHitpoints > $fighter2->getHitpoints()) echo "youve won the fight!"; else echo "oh oh, you lost!"; There are reasons flush() could seem not to produce the desired result depending on your environment. You should consult the manual for additional information about flushing and buffering. http://ca.php.net/manual/en/function.flush.php Quote Link to comment Share on other sites More sharing options...
Canadiengland Posted March 16, 2007 Author Share Posted March 16, 2007 yeah it didnt like the whole ->fetch array... <br /> <b>Fatal error</b>: Call to a member function fetch_assoc() on a non-object in <b>C:\wamp\www\killmonster\Profile.php</b> on line <b>21</b><br /> Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted March 16, 2007 Share Posted March 16, 2007 yeah it didnt like the whole ->fetch array... <br /> <b>Fatal error</b>: Call to a member function fetch_assoc() on a non-object in <b>C:\wamp\www\killmonster\Profile.php</b> on line <b>21</b><br /> I did not write all the code for you, just the section you were asking about. The code I wrote will not run as-is; it's incomplete. Nor will it work properly with the mysql extension. I recommend you switch to the mysqli extension. fetch_assoc() is a method of the mysqli_result object, so $statsResult must be of that type in order to call that method on it. If you insist on using the mysql extension, you'd need this instead: $statsResult = mysql_query("SELECT * FROM km_users WHERE ID = {$_GET['id']}"); $userStats = mysql_fetch_assoc($statsResult); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.