Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by KevinM1

  1. Some pseudo-code: class PlayerCharacter { private $race; private $class; private $raceAbilities = array(); private $classAbilities = array(); public function __construct($race, $class) { $this->race = $race; $this->class = $class; $this->populate(); } private function populate() { /* read race and class abilities from the db or XML file(s) for all abilities the PC is able to have access to (e.g., those abilities labeled equal to or below their level) add to the arrays. while(iterating through datastore) { $raceAbilities[$row['name']] = new $row['type']($row['name'], $row['level'], ... ); //same for classAbilities } */ } public function raceAbility($name) { // if $raceAbilities[$name] exists, execute it. Else, execute default ability or do nothing } public function classAbility($name) { // if $classAbilities[$name] exists, execute it. Else, execute default ability or do nothing } } abstract class RaceAbility { protected $name; protected $level; // etc. public function __construct($name, $level, /* etc. */) { $this->name = $name; $this->level = $level; // etc. } abstract public function execute(); } class OrcRage extends RaceAbility { public function execute() { // do orc ragey things } }
  2. It sounds like you need a bridge or adapter (Bridge Pattern - Adapter Pattern). You can have a Race class and a Class class. Their concrete children can be members of a concrete PlayerCharacter class. The biggest hurdle is attempting to access the abilities the various race/class combinations can potentially have. What I did in my game was store class info in XML files so they could be completely modular. I then made each ability/attack they could perform an instance of an Attack class. A list of these attacks were then stored in my PC class. Attacks were chosen based on the attack name - if found in the list, and if the PC had enough MP, the attack would be invoked. Otherwise, the generic base attack (which all classes had) would be invoked. My game didn't have various races for the player to choose from, but the same basic idea could work there, too. Anyway, just some ideas.
  3. The . is the concatenation operator. In your example, the query string is demarcated by double quotes. The single quotes are there, as was said, to prevent errors. SQL queries tend to like values passed in with quotes around them. The query could also be written as: $result = $conn-> query ("UPDATE user SET passwd = sha1 ('$new_password') WHERE username = '$username'"; Due to the nature of double quotes in PHP (strings denoted with them automatically interpolate variables). Explicitly exiting and entering the string in conjunction with the use of the concatenation operator just makes it clear that variables are being injected into the string. Fake edit: Like Mchl said.
  4. I think they're referring to the else if, which was written as just an if in the OP. If that's the case, your logic is messed up. If the first case (the regex) is true, then you access the db and do a bunch of stuff. If it's not true (else if), then there's no $row['solution'] because you haven't accessed the db. The db is accessed only if the first case is true.
  5. No. Global variables are not the way to go. They never are. Functions and methods have argument lists for a reason. Use them.
  6. Okay, here's a new test file and script. The test file will illustrate how you should view the process. It's pretty much identical to my other test files: <!DOCTYPE html> <html> <head> <style> .clicked { background-color: red; border: 1px solid black;} </style> </head> <body> <form name="form1" action="#" method="post"> <input type="text" /> <br /> <textarea></textarea> <br /> <input name="submit" type="submit" value="Submit" /> </form> </body> <script type="text/javascript"> var inputs = document.getElementsByTagName('input'); var textareas = document.getElementsByTagName('textarea'); var textInputs = new Array(); var previousElement; for(var i = 0; i < inputs.length; ++i) { if (inputs[i].type == "text") { textInputs.push(inputs[i]); } } for(var j = 0; j < textInputs.length; ++j) { textInputs[j].onclick = function() { this.className = (this.className == "clicked") ? "" : "clicked"; if (previousElement != null) { previousElement.className = ""; previousElement = this; } else { previousElement = this; } } } for(var k = 0; k < textareas.length; ++k) { textareas[k].onclick = function() { this.className = (this.className == "clicked") ? "" : "clicked"; if (previousElement != null) { previousElement.className = ""; previousElement = this; } else { previousElement = this; } } } </script> </html> As you can see, I added a simple CSS class called 'clicked'. This class contains the styling that will be applied to any clicked and focused text input or textarea. In order to turn off the styling on other, non-focused elements when a new one is clicked, you must keep track of which element was last active. So, I added a variable to the JavaScript named previousElement that tracks that element. Let's say you've already clicked on a textarea. That element is stored in the previousElement variable. When you click on some other form element that can have a different background, the clicked class is added to that new element and removed from previousElement. The new element is then stored in previousElement, so additional clicks can toggle the effect. Now, this toggling won't work if you click on some non-input element. If that's what you want, you should probably use a framework like jQuery, as that will handle some of the more tedious parts of the script in the background, and make designing the functionality you want much easier. The more complex the functionality, the more nitty gritty details you must understand. Since you seem to be pretty new to JavaScript, that sort of thing may only confuse you further as the nuts-and-bolts code can hide what its actually doing as a whole.
  7. Well, you never specifically said you wanted the other inputs to change back to white if some other input was clicked. Again, it's an easy fix. Create a CSS class (or classes) that will contain the clicked background color you want. Then, inside the script, we can add/remove this class from the proper elements. I'll write it up after I have some caffeine. Also, you may be able to include the JavaScript file that way, but you'd need to put the include inside of some HTML, like so: <script type="text/javascript"> <?php include("colorCode.js"); ?> </script> I'm not sure if it would work, however.
  8. It works in my test file: <!DOCTYPE html> <html> <head></head> <body> <form name="form1" action="#" method="post"> <input type="text" /> <br /> <textarea></textarea> <br /> <input name="submit" type="submit" value="Submit" /> </form> </body> <script type="text/javascript"> var inputs = document.getElementsByTagName('input'); var textareas = document.getElementsByTagName('textarea'); var textInputs = new Array(); for(var i = 0; i < inputs.length; ++i) { if (inputs[i].type == "text") { textInputs.push(inputs[i]); } } for(var j = 0; j < textInputs.length; ++j) { textInputs[j].onclick = function() { this.style.backgroundColor = (this.style.backgroundColor == "red") ? "white" : "red"; } } for(var k = 0; k < textareas.length; ++k) { textareas[k].onclick = function() { this.style.backgroundColor = (this.style.backgroundColor == "blue") ? "white" : "blue"; } } </script> </html>
  9. Ah, you want a toggle. Easy modification: <script type="text/javascript"> var inputs = document.getElementsByTagName('input'); var textareas = document.getElementsByTagName('textarea'); var textInputs = new Array(); for(var i = 0; i < inputs.length; ++i) { if (inputs[i].type == "text") { textInputs.push(inputs[i]); } } for(var j = 0; j < textInputs.length; ++j) { textInputs[j].onclick = function() { this.style.backgroundColor = (this.style.backgroundColor == "red") ? "white" : "red"; } } for(var k = 0; k < textareas.length; ++k) { textareas[k].onclick = function() { this.style.backgroundColor = (this.style.backgroundColor == "blue") ? "white" : "blue"; } } </script> Replace the script you currently have with what I wrote above. Here's what the change does: It checks to see what the current color of the background is. If it's a 'special' color (red or blue), it sets it to white. Otherwise, it sets it to the special color. Also note that you can use any background color you want. I chose red and blue merely as example colors. Feel free to use whatever fits with your site. The code will accept color codes (like #CC00FF), too. Just be sure they're encased in quotes.
  10. What you need is a function, something along the lines of: function headline($storyId) { $query = "SELECT headline FROM tablename WHERE story_id = $storyId"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_assoc($result); echo $row['headline']; } else { echo "Could not process request."; } } headline(5); Should give you an idea on what to do.
  11. Do not resurrect dead threads. We're ill equipped to handle zombies at the moment.
  12. It doesn't work, in part, because you put the script in the wrong place. I intentionally placed the JavaScript at the end of the HTML. This ensures that it won't create any runtime errors. Putting it at the very beginning of the markup, in the way that I wrote it, will practically ensure it won't run as the script will run before the page elements are created. Thus, it will look to add onclick events to non-existing elements. Put the JavaScript at the end, after the closing </body> tag, but before the closing </html> tag, and tell me what you get.
  13. The superglobal arrays require you to write them correctly: $_GET['index'] $_POST['index'] $_REQUEST['index'] Where index is the name of the value you're trying to obtain. In your case: $_GET['t'] If that doesn't fix the problem, share more code.
  14. So, you want something like: <!DOCTYPE html> <html> <head></head> <body> <form name="form1" action="#" method="post"> <input type="text" /> <br /> <textarea></textarea> <br /> <input name="submit" type="submit" value="Submit" /> </form> </body> <script type="text/javascript"> var inputs = document.getElementsByTagName('input'); var textareas = document.getElementsByTagName('textarea'); var textInputs = new Array(); for(var i = 0; i < inputs.length; ++i) { if (inputs[i].type == "text") { textInputs.push(inputs[i]); } } for(var j = 0; j < textInputs.length; ++j) { textInputs[j].onclick = function() { this.style.backgroundColor = "red"; } } for(var k = 0; k < textareas.length; ++k) { textareas[k].onclick = function() { this.style.backgroundColor = "blue"; } } </script> </html> ?? Or are you looking to change the background of other elements?
  15. hmmm the average user time would be half an hour... I don't think this should be an issue, if he gets a new IP he will have the new one in his next session. I'm not saving users, only while they use one part of the website. Are sessions secure? Are they stored in the server or can the user make modifications in his cookies/private browser data that could pose a security risk to my site? Thanks again! Session data is stored on the server. The only bit that's saved on the client is a cookie with the session id. You can change/refresh that id to prevent session fixation (session_regenerate_id).
  16. Instead of using session_register(), try: $_SESSION['schoolid'] = $schoolid;
  17. What order is this code being executed in? Is the first bit of code in a different file than the second? Where does the first code block's $row->name come from?
  18. Well, you have a variable named $name that you attempt to use in your query. I'm assuming the value stored in it is supposed to be what you stored in $_SESSION['name']. If this is the case, and you want to use $name, you need to essentially unpack the session variable like so: $name = $_SESSION['name']; Also, be sure to have session_start(); as the first line of every page you want to be able to use sessions on. It won't work otherwise. If this doesn't fix it, post more code so we can better diagnose the problem.
  19. Definition != declaration. Your variable has no value, and you're attempting to echo said non-existing value, thus PHP gives you that error. Ensure your variables actually contain values before attempting to output them.
  20. Yeh, trust a facebook developer to create something great and kill it by trying to be cool when he hasn't got the first clue of what cool is. I think it's ironic that these people develop a 'social network' that connects millions of people all over the world, yet lack completely in any form of social skills... Who said programming wasn't cool? I'd rather crunch regular expressions than go out in an 'invited party' any day! I dunno about that. A regular expression has a 0% chance of touching my penis.
  21. Do you take the name value out of the session variable?
  22. Try using the following: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>variables</title> </head> <body> <?php // Script 2.3 - variables.php // An address: $street = "100 Main Street"; $city = "State College"; $state = "PA"; $zip = 16801; // Print the address. print "<p>The address is:<br />$street<br />$city $state $zip</p>"; ?> </body> </html> You forgot to end your PHP code island (note how the color of this code changes from black and white (html) to colored (PHP)).
  23. What do the different concrete validators do? How would you normally invoke their validation methods?
  24. Looks interesting from a technical standpoint. The name sucks, though.
  25. It sounds like you've designed yourself into a corner. Some questions: What is the likelihood that a user would break a session? If they DID break the session, would that constitute a critical/unrecoverable error? Why can't you retrieve/replace this value on demand within the other files?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.