Jump to content

displaying a users details on the site


kingb00zer

Recommended Posts

Hi im working on a small browser game and so far I have the registration working fine, now im just making the basic layout for it and came to a part where I need more help. I have a table on my site which will show certain gamestats like "money, weed, thugs, guns and turns" and I have the html side of it complete but kind of unsure how to display these stats for when I log in.

<table align="center" border="3" width="700"><tr bgcolor="green"><td align="center">YOU HAVE:</td></tr><tr><td align="center">Money:</td></tr><tr><td align="center">Weed:</td></tr><tr><td align="center">Thugs:</td></tr><tr><td align="center">Pistols:</td></tr><tr><td align="center">Turns:</td></tr></table>

All of the above is from the same table in my databse and I want to display the stats from the database next to the text in the code above like "Money: $100,000" etc... How would I do this?

Link to comment
Share on other sites

Are you saying you are not sure how to extract and then output data from your database? Example #1 here is useful. Note especiaslly the while loop toward the bottom. That's the part that does the heavy lifting if you have multiple users to be displayed. If not, skip the loop and execute mysql_fetch_assoc just once.You will need a way to embed the resulting values in the table structure. The simplest might be something like this:

echo "<tr><td align=\"center\">Money:</td><td>{$row['money']}</td></tr>";

Note that in this quotation pattern, the curly braces are not optional, but they will not be displayed, either.

Link to comment
Share on other sites

I gave that example a go and so far I've got nothing working. what could i have got wrong? It gives me this error: Parse error: syntax error, unexpected T_VARIABLE in /home/schulzy/public_html/phptestmenu.php on line 13I'm thinkin the $_POST[username] part is what I think is wrong but I cant seem to figure it out.

phptestmenu.php<html><body><h1 align="center"> Thug Fights </h1><table align="center" border="3" width="700"><tr bgcolor="green"><td align="center">YOU HAVE:</td></tr><?phperror_reporting(E_ALL);  ini_set('display_errors', 1);include('connection.php')$sql= "SELECT id as username, money, weed, thugs, pistols, turns FROM gamestats WHERE username= '$_POST[username]'";		$result= mysql_query($sql);if (!$result) {	echo "Could not successfully run query ($sql) from DB: " . mysql_error();	exit;}if (mysql_num_rows($result) == 0) {	echo "No rows found, nothing to print so am exiting";	exit;}while ($row = mysql_fetch_assoc($result)) {echo "<tr><td align=\"center\">Money:</td><td>{$row['money']}</td></tr>";echo "<tr><td align=\"center\">Weed:</td><td>{$row['weed']}</td></tr>";echo "<tr><td align=\"center\">Thugs:</td><td>{$row['thugs']}</td></tr>";echo "<tr><td align=\"center\">Pistols:</td><td>{$row['pistols']}</td></tr>";echo "<tr><td align=\"center\">Turns:</td><td>{$row['turns']}</td></tr>";}mysql_free_result($result);</table><table align="left" bgcolor="green"><tr><td align="center"><b>MENU</b></td></tr><tr><td><a href="buyweed.php" align="center">Buy Weed</a></td></tr><tr><td><a href="trainskills.php" align="center">Train Skills</a></td></tr><tr><td><a href="recruit.php" align="center">Recruit Thugs</a></td></tr><tr><td><a href="attackroom.php" align="center">Attack Room</a></td></tr><tr><td><a href="gunstore.php" align="center">Gun Store</a></td></tr><tr><td><a href="ranks.php" align="center">Ranks</a></td><</tr><tr><td><a href="logout.php" align="center">Logout</a></td></tr></table><br><br><br></body></html>?>

Link to comment
Share on other sites

You're missing a semicolon after the include() call on line 11. Also, the correct way to interpolate arrays is to surround them by braces:

$sql= "SELECT id as username, money, weed, thugs, pistols, turns FROM gamestats WHERE username= '{$_POST['username']}'";

Link to comment
Share on other sites

thanks its getting further thru without error but now i am getting this:Notice: Undefined index: username in /home/schulzy/public_html/phptestmenu.php on line 15Could not successfully run query (SELECT id as username, money, weed, thugs, pistols, turns FROM gamestats WHERE username= '') from DB: Unknown column 'id' in 'field list'EDIT: I took out the id as bit in the sql= part and now I have the undefined index problem.

Link to comment
Share on other sites

by form do you mean the form on the login page? because I'm trying to get data from the user logged in displayed on the page you go to once logged in "phptestmenu.php" which hasnt got any forms.

Link to comment
Share on other sites

Well, $_POST only stores data that was sent to the page with a POST request. If you want to store something for longer, you'll have to use something like sessions.

Link to comment
Share on other sites

when I start a session should I start it at the end of the last php file I was running to log in? or at the beggining of the script on the page you get taken to after you log in?EDIT: I noticed on the login successful file I have it already has sessionstart() do I have to start a session again with the page it takes me to?

login_succes.php<?session_start();if(!session_is_registered(myusername)){header("location:index.php");}else{header ("location:phptestmenu.php");}?>

Link to comment
Share on other sites

You need to call it at the start of every page you would like to access the session. However, you need to use the $_SESSION superglobal array to store (and retrieve) the things you would like available for the session.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...