twilitegxa Posted September 27, 2009 Share Posted September 27, 2009 I have the following file that allows the user to choose a character to play or train from a list of characters that they have registered with their username. I would like to create a way to store the character they have chosen to display once they have chosen one. How can I do this? I guess I need it to be similar to a log in script, where it checks if the character is logged in and displays the character name on the page after they choose one. How would I do this? Here is the page they choose the character on: choose_character.php <?php session_start(); if(!isset($_SESSION['loggedIn'])) { header("Location: login.php"); } ?> <html> <head> <title>Choose Character</title> <style type="text/css" media="screen"> /*<![CDATA[*/ @import url(global.css); /*]]>*/ </style> <script type="text/javascript" src="selectuser.js"></script> </head> <body> <!-- HEADER --> <h1 class="logo">Sailor Moon RPG</h1> <!-- /HEADER --> <?php include("topnav.php"); ?> <div id="main"> <?php include("includes/log.php"); ?> <?php include("mainnav.php"); ?> <h1>Sailor Moon RPG - Choose Character <h3>Which character would you like to play?</h3> <form> <?php include("connect_db.php"); ?> <?php $verify_category = "select id, identity, username from scouts where username = '".$_SESSION['userName']."'"; $verify_category_res = mysql_query($verify_category, $conn) or die(mysql_error()); if (mysql_num_rows($verify_category_res) < 1) { echo "<i>You have no Scouts created.</i> <a href=creationform.php>Create one</a>?<br>"; } else { echo "<b>Scouts:</b><br> <select name=users onchange=showUser(this.value)> <option>select character</option>"; while ($list_scouts = mysql_fetch_array($verify_category_res)) { $identity = ucwords($list_scouts['identity']); $scout_id = $list_scouts['id']; echo "<option value=$scout_id>$identity</option>"; } } echo "</select>"; echo "<br><br>"; //verify Knights exist in category and list if applicable $verify_category = "select id, identity, username from knights where username = '".$_SESSION['userName']."'"; $verify_category_res = mysql_query($verify_category, $conn) or die(mysql_error()); if (mysql_num_rows($verify_category_res) < 1) { echo " "; } else { echo "<b>Knights:</b><br> <select name=users onchange=showUser(this.value)> <option>select character</option>"; while ($list_knights = mysql_fetch_array($verify_category_res)) { $identity = ucwords($list_knights['identity']); $scout_id = $list_knights['id']; echo "<option value=$scout_id>$identity</option>"; } } echo "</select>"; echo "<br>"; //verify Female Dark Warriors exist in category and list if applicable $verify_category = "select id, identity, username from fdark_warrior where username = '".$_SESSION['userName']."'"; $verify_category_res = mysql_query($verify_category, $conn) or die(mysql_error()); if (mysql_num_rows($verify_category_res) < 1) { echo " "; } else { echo "<b>Female Dark Warriors:</b><br> <select name=users onchange=showUser(this.value)> <option>select character</option>"; while ($list_fdark_warriors = mysql_fetch_array($verify_category_res)) { $identity = ucwords($list_fdark_warriors['identity']); $scout_id = $list_fdark_warriors['id']; echo "<option value=$scout_id>$identity</option>"; } } echo "</select>"; echo "<br>"; //verify Male Dark Warriors exist in category and list if applicable $verify_category = "select id, identity, username from mdark_warrior where username = '".$_SESSION['userName']."'"; $verify_category_res = mysql_query($verify_category, $conn) or die(mysql_error()); if (mysql_num_rows($verify_category_res) < 1) { echo " "; } else { echo "<b>Male Dark Warriors:</b><br> <select name=users onchange=showUser(this.value)> <option>select character</option>"; while ($list_mdark_warriors = mysql_fetch_array($verify_category_res)) { $identity = ucwords($list_mdark_warriors['identity']); $scout_id = $list_mdark_warriors['id']; echo "<option value=$scout_id>$identity</option>"; } } echo "</select>"; ?> </form> <br /> <div id="txtHint" class="choose_character"> </div> </div> <?php include("bottomnav.php"); ?> <!-- FOOTER --> <div id="footer_wrapper"> <div id="footer"> <p>Sailor Moon and all characters are<br> trademarks of Naoko Takeuchi.</p> <p>Copyright © 2009 Liz Kula. All rights reserved.<br> A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p> <div id="foot-nav"><!-- <ul> <li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li> <li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li> </ul> --></div> </div> </div> <!-- /FOOTER --> </body> </html> </body> </html> selectuser.js: var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="getuser.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } getuser.php: <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("smrpg", $con); $get_identity = "select * from scouts where id = '".$q."'"; $get_identity_res = mysql_query($get_identity); while ($scout_identity = mysql_fetch_array($get_identity_res)) { $identity = $scout_identity['identity']; echo "<h2>$identity</h2>"; } $sql="SELECT * FROM stats WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='0' width=34%>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><b>Body:</b></td> <td>" . $row['body'] . "</td></tr>"; echo "<tr><td><b>Mind:</b></td> <td>" . $row['mind'] . "</td></tr>"; echo "<tr><td><b>Soul:</b></td> <td>" . $row['soul'] . "</td>"; echo "</tr> <tr><td> </td> <td> </td></tr>"; } //derived values $sql="SELECT * FROM derived_values WHERE id = '".$q."'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><b>Health Points:</b></td> <td>" . $row['health'] . "</td></tr>"; echo "<tr><td><b>Energy Points:</b></td> <td>" . $row['energy'] . "</td></tr>"; echo "<tr><td><b>Attack Combat Value:</b></td> <td>" . $row['acv1'] . "</td></tr>"; echo "<tr><td><b>Defense Combat Value:</b></td> <td>" . $row['dcv1'] . "</td>"; echo "</tr>"; } echo "</table><br> <p class=choose> <input type=button value=Choose> </p>"; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 I have the following file that allows the user to choose a character to play or train from a list of characters that they have registered with their username. I then you say this? I would like to create a way to store the character they have chosen to display if they just I have the following file that allows the user to choose a character to play or train from a list of characters that they have registered with their username. I show the dam thing then are you asking you want to show other characters the user likes? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 I did show the file, at the end. The choose_character.php file, that uses JavaScript and PHP to display the character's stats so they can see which character they want to choose. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 What I want to do is let them choose the character they want to use. There is a choose button. They choose the character and they use it later on the site. I want to know how I would store this for use on the site? Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 Now create a new database with that info. table name characters. character_id user_id character_name date_added Do you understand bro. when the user wants to add a new character then update the database with the new entry's. now the game got more functions and features innit. when need to show what characters that user got, show it via the user_id from the new table. or even more clever use the LIKE statement and select what character the user wants to do from the new table arrrrrrrrrr Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 Nevermind, that's not what I meant. I already have a table with the characters it in. There is no need to make another one. What I need to do is store the chosen character I guess in a session, like a login username. Would I use a login type script, or can I just store the value in a session? What would work best is what I would like to know? Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 session will be used until the page is closed, then there gone, and a database is permanently there, so u tell us. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 LOL Yes, I know the difference. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 if you got a table for characters then why not use that then Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 I don't understand how you mean to use it. But look at my script. How would I store the session with the username the person chooses from the table? <?php $verify_category = "select id, identity, username from scouts where username = '".$_SESSION['userName']."'"; $verify_category_res = mysql_query($verify_category, $conn) or die(mysql_error()); if (mysql_num_rows($verify_category_res) < 1) { echo "<i>You have no Scouts created.</i> <a href=creationform.php>Create one</a>?<br>"; } else { echo "<b>Scouts:</b><br> <select name=users onchange=showUser(this.value)> <option>select character</option>"; while ($list_scouts = mysql_fetch_array($verify_category_res)) { $identity = ucwords($list_scouts['identity']); $scout_id = $list_scouts['id']; echo "<option value=$scout_id>$identity</option>"; } } echo "</select>"; Quote Link to comment Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 You've got four tables doing the job of 1...or 2 maybe select id, identity, username from scouts where username select id, identity, username from knights where username select id, identity, username from fdark_warrior where username select id, identity, username from mdark_warrior where username If it were me doing this project I would have one characters table with---- -id -identity -username -type -active Now you could also add another table for your character types just in case you ever decide to add more types..then you'd store the character type id in the above table schema .. under type So your character types table would be -- -id -type Now I haven't read through all your code, but hopefully I'll explain this well enough for you to figure it out. Or maybe you've already caught my idea when I introduced the 'active' column in the characters table. Using this simple binary data you can select all of a particular user's active characters...their types..and anything else you want to put in there. Just have the database updated when they select that Choose button. Create another AJAX function to do it. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 zanus, is telling you professionally, what i try to explain, but you told me, you got a database table for that lol..... Quote Link to comment Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 gloating (about nothing) isn't going to help anyone redarrow Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 I think I understand the active idea. The reason I have four different tables is because a few of the fields are different in each table. I chose to do it this way because on some pages, it displays something like: You don't have any Knights created. etc... Thanks for the active idea. How would I set a script disable the active character when the user logs off? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 Also, thanks for the help, zanus :-) Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 gloating (about nothing) isn't going to help anyone redarrow sorry mate Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 zanus, I have added this to my chosen.phppage: <?php $get_scout = "select * from scouts where id = $_GET[id]"; $get_scout_res = mysql_query($get_scout, $conn) or die(mysql_error()); while ($scout_info = mysql_fetch_array($get_scout_res)) { $scout_id = $scout_info['id']; $identity = ucwords($scout_info['identity']); $make_active = mysql_query("UPDATE scouts SET active='1' WHERE id = '$scout_id'"); } ?> And this to my button: <a href=chosen.php?id=$q><input type=button value=Choose></a> But it's not updating. What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 is the id showing from the button link and is $_GET['id'] like this? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 I don't understand what you mean? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 Nevermind, I added single quotes around the $_GET[id] and now it updates. But can you tell me how I'd update the active field when the user logs out? Or when they close the browser? Or both? Quote Link to comment Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 I think I understand the active idea. The reason I have four different tables is because a few of the fields are different in each table. I chose to do it this way because on some pages, it displays something like: You don't have any Knights created. etc... Thanks for the active idea. How would I set a script disable the active character when the user logs off? That's why it would be best to have another character type table... how different can the fields be for 4 types? post the schema's for them for the hell of it. You can always program in scripts that will check to see whether or not so many characters are active..like I said before. Logically you'd query the database like this SELECT * from characters WHERE user = 39 //For user number 39 you could even GROUP BY type and you probably have to join in the types table to get all the other stuff too. INNER JOIN types ON characters.type = types.id so that when you're looping through mysql_fetch_array...everything is already organized the way you need it to be. To disable all the active characters you'd just send an update query on logoff..something like this UPDATE characters SET active = 0 WHERE user = 39 just add that somewhere along with everything else that happens whenever the user clicks "Log out" it would be a little different doing it on browser closing because you'd be at the mercy of people having Javascript enabled..although you seem to be relying on JS alot anyway so that point doesn't really hold water. But to do it you'd have another AJAX script that runs of of onUnLoad() and you'd put it in the body tag. Read up http://www.livelearncode.com/archives/11 http://www.zachleat.com/web/2008/04/22/dont-let-the-door-hit-you-onunload-and-onbeforeunload/ Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 If they don't click log out, your have to use cookies and session's...... set the cookie for a future time example 20 mins for user to log out, then update database with session id from the cookie. don't worry if the user is still logged in after 20 mins , the cookie counts another 20 mins to update the database. the database only updates when user not logged in.... meaning user no there anymore they have logged out and cookie executes the update.... Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 27, 2009 Author Share Posted September 27, 2009 But these tables and fields are already being accessed on other pages, so I could create a new one, if needed, but not turn these into one. `scouts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `create_time` datetime NOT NULL, `username` varchar(150) NOT NULL, `identity` varchar(150) NOT NULL, `name` varchar(150) NOT NULL, `element_of_influence` varchar(150) NOT NULL, `age` int(2) NOT NULL, `birth_month` int(2) NOT NULL, `birth_date` int(2) NOT NULL, `birth_year` int(4) NOT NULL, `blood_type` varchar(3) NOT NULL, `hobbies` varchar(150) NOT NULL, `favorite_color` varchar(150) NOT NULL, `favorite_gemstone` varchar(150) NOT NULL, `favorite_food` varchar(150) NOT NULL, `least_favorite_food` varchar(150) NOT NULL, `favorite_school_subject` varchar(150) NOT NULL, `least_favorite_school_subject` varchar(150) NOT NULL, `strengths` varchar(150) NOT NULL, `weaknesses` varchar(150) NOT NULL, `goal` longtext NOT NULL, `mission` longtext NOT NULL, `biography` longtext NOT NULL, `height_feet` int(2) NOT NULL, `height_inches` int(2) NOT NULL, `available` tinyint(1) NOT NULL, `attack_level` int(11) NOT NULL, `active` tinyint(1) NOT NULL, PRIMARY KEY (`id`) `knights` ( `id` int(11) NOT NULL AUTO_INCREMENT, `create_time` datetime NOT NULL, `username` varchar(150) NOT NULL, `identity` varchar(150) NOT NULL, `name` varchar(150) NOT NULL, `emotion_of_influence` varchar(150) NOT NULL, `age` int(2) NOT NULL, `birth_month` int(2) NOT NULL, `birth_date` int(2) NOT NULL, `birth_year` int(4) NOT NULL, `blood_type` varchar(3) NOT NULL, `hobbies` varchar(150) NOT NULL, `favorite_color` varchar(150) NOT NULL, `favorite_gemstone` varchar(150) NOT NULL, `favorite_food` varchar(150) NOT NULL, `least_favorite_food` varchar(150) NOT NULL, `favorite_school_subject` varchar(150) NOT NULL, `least_favorite_school_subject` varchar(150) NOT NULL, `strengths` varchar(150) NOT NULL, `weaknesses` varchar(150) NOT NULL, `goal` longtext NOT NULL, `mission` longtext NOT NULL, `biography` longtext NOT NULL, `height_feet` int(2) NOT NULL, `height_inches` int(2) NOT NULL, `available` tinyint(1) NOT NULL, PRIMARY KEY (`id`) `fdark_warrior` ( `id` int(11) NOT NULL AUTO_INCREMENT, `create_time` datetime NOT NULL, `username` varchar(150) NOT NULL, `identity` varchar(150) NOT NULL, `name` varchar(150) NOT NULL, `element_of_influence` varchar(150) NOT NULL, `age` int(2) NOT NULL, `birth_month` int(2) NOT NULL, `birth_date` int(2) NOT NULL, `birth_year` int(4) NOT NULL, `blood_type` varchar(3) NOT NULL, `hobbies` varchar(150) NOT NULL, `favorite_color` varchar(150) NOT NULL, `favorite_gemstone` varchar(150) NOT NULL, `favorite_food` varchar(150) NOT NULL, `least_favorite_food` varchar(150) NOT NULL, `strengths` varchar(150) NOT NULL, `weaknesses` varchar(150) NOT NULL, `goal` longtext NOT NULL, `mission` longtext NOT NULL, `biography` longtext NOT NULL, `height_feet` int(2) NOT NULL, `height_inches` int(2) NOT NULL, `available` tinyint(1) NOT NULL, PRIMARY KEY (`id`) `mdark_warrior` ( `id` int(11) NOT NULL AUTO_INCREMENT, `create_time` datetime NOT NULL, `username` varchar(150) NOT NULL, `identity` varchar(150) NOT NULL, `name` varchar(150) NOT NULL, `element_of_influence` varchar(150) NOT NULL, `age` int(2) NOT NULL, `birth_month` int(2) NOT NULL, `birth_date` int(2) NOT NULL, `birth_year` int(4) NOT NULL, `blood_type` varchar(3) NOT NULL, `hobbies` varchar(150) NOT NULL, `favorite_color` varchar(150) NOT NULL, `favorite_gemstone` varchar(150) NOT NULL, `favorite_food` varchar(150) NOT NULL, `least_favorite_food` varchar(150) NOT NULL, `favorite_school_subject` varchar(150) NOT NULL, `least_favorite_school_subject` varchar(150) NOT NULL, `strengths` varchar(150) NOT NULL, `weaknesses` varchar(150) NOT NULL, `goal` longtext NOT NULL, `mission` longtext NOT NULL, `biography` longtext NOT NULL, `height_feet` int(2) NOT NULL, `height_inches` int(2) NOT NULL, `available` tinyint(1) NOT NULL, PRIMARY KEY (`id`) Quote Link to comment Share on other sites More sharing options...
Zane Posted September 27, 2009 Share Posted September 27, 2009 Yeah.. you'd have to re-write your whole project probably to fix it up the way I described. (Perhaps wait until version 2) But the active column is still doable..you'll just have to issue 4 update queries on log out rather than 1. update mdark_warrior SET active = 0 WHERE user = 56 update scouts SET active = 0 WHERE user = 56 update knights SET active = 0 WHERE user = 56 update fdark_warrior SET active = 0 WHERE user = 56 you can do it all in one query too so. no need calling mysql_query four times something like $sql = "update mdark_warrior SET active = 0 WHERE user = 56;"; $sql .= "update scouts SET active = 0 WHERE user = 56;"; $sql .= "update knights SET active = 0 WHERE user = 56;"; $sql .= "update fdark_warrior SET active = 0 WHERE user = 56;"; $result = mysql_query($sql); //Poof...characters disabled note the double semicolons .... 56;"; yes..they are necessary Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 whale that a lot off database entry's for a project like this... you have to look up normalization mate (( in your own time when got it bro. http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html you cold off used one table and entered the character name in stead off all that mysql data..... getting dynamic is grate ..... 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.