Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by KevinM1

  1. Switch hosts? In all seriousness, that is a toughy. The only thing I've thought of, off the top of my head, is to use JavaScript to hash the inputed values before they're transmitted to the server. Like you say, that's hardly a safe/secure way of doing things, though. Does SSH require SSL?
  2. A few things: 1. From a design standpoint, you'll be better served by refactoring some aspects of your character. In particular, their armor and weapon(s) would benefit by being objects themselves. That way, you can simply have methods in your Char class that call methods in your Armor and Weapon classes: <?php class Char { private $armor; private $weapon; . . . public function getArmorHP() { return $this->armor->getHP(); } public function getWeaponAttack() { return $this->weapon->getAttack(); } } class Armor { private $hpMod; . . . public function getHP() { return $this->hpMod; } } class Weapon { private $attackMod; . . . public function getAttack() { return $this->attackMod; } } ?> 2. You can't define a function within another function (as far as I know), but you can invoke another function. Is your use_potion() method defined within your Char class? If so, then you simply need to use: <?php $this->hp_up($row['hp_bonus']); ?> To use it. If use_potion() is a function defined outside of the class, then you'll have to have a Char object to work on: <?php function use_potion($char, $wepId) { . . . $char->hp_up($row['hp_bonus']); . . . } $trogdor_the_burninator = new Char(); use_potion($trogdor_the_burninator, $someWepId); ?>
  3. Mind showing your class code, at the very least the parts that show how the object data (SKU, model number, etc) is actually stored? I hate reading through var_dump output...makes everything look like an array. It would be beneficial to see it for all involved classes, so not just the individual products, but the cart and shipping manager to.
  4. Sounds like you need to employ a while loop: <?php $query = "SELECT * FROM alphaground WHERE id = '$id'"; $result = mysql_query($query); $count = 1; $id = 1; echo "<table width='210' border='0' align='center' cellpadding='0' cellspacing='0'>"; while($check = mysql_fetch_assoc($result)) { if($check['owner'] == 0) { $keycheck = "<img src='images/keyneautural.png'>"; } else { $keycheck = "<img src='images/keyalliance.png'>"; } if($count > 11) //for every 12 items, create a new row { echo "</tr><tr><td>$id $keycheck</td>"; $count = 1; } else { echo "<td>$id $keycheck</td>"; } $count++; $id++; } ?> This code isn't tested by any means, but it should put you on the right track.
  5. It sounds as though you both have Magic Quotes turned on. Try the following function: <?php function myEscape($value) { return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($value)) : mysql_real_escape_string($value); } $name = myEscape($_POST['name']); ?>
  6. <?php function keyCheck($key) { if($key == "empty") { $string = "<img src='images/keyneautural.php'>"; //are you sure you're not trying to spell 'neutral'? } else { $string = ""; } return $string; } $myImg = keyCheck("empty"); ?>
  7. If this forum is for public use, you should purchase a domain name.
  8. To stop dancing around the point, your array example fails for one of two possible reasons, depending on what you're trying to do: 1. If your array is supposed to be part of the class, it fails because the array is not declared as a property of the class. 2. If your array is not supposed to be part of the class, it fails because you're not passing it to the object as a method argument. Example of using the array as a property of the class: <?php class Test { private $testArray = array('Hello', ' ', 'World'); //best to keep it private, as you shouldn't give the outside world direct access to class properties public function __construct() { $this->display(); } public function display() { foreach($this->testArray as $value) { echo $value; } } } $myObj = new Test(); ?> Example of passing the array as an argument: <?php $testArray = array('Hello', ' ', 'World'); class Test { public function __construct($array) { $this->display($array); } public function display($array) { foreach($array as $value) { echo $value; } } } $myObj = new Test($testArray); ?> Note: the examples I've written use PHP 5 syntax.
  9. You can inherit the parent constructor. In fact, PHP does it inherently behind-the-scenes. Try: <?php class Parent { private $other; public function __construct() { $this->other = new Other(); } public function doSomething() { $this->other->do(); } } class Child extends Parent { public function newFunction() { parent::doSomething(); } } ?> Similarly, if you want to directly invoke the parent constructor, you could always go: <?php public function __construct() { parent::__construct(); //additional code if needed } ?>
  10. I started off with the PHP and MySQL Quickstart Guide by Larry Ullman. Great book if you want to become proficient in a hurry. I'm still going through Zandstra's OOP book. I'm not a big fan of his Application Controller. Something just doesn't sit right with me in using an XML file to map view flow, but I digress. I actually recommend not using w3schools to learn. They teach only the very bare minimum, and certainly don't teach best practices (embedded JavaScript in markup, oy vey). Good texts are the way to go, IMO.
  11. You could remove addPhoto and deletePhoto from the User class and put them in the PhotoManager. You'd still have to pass the correct User instance into the method, and, in this case, you'd still need helper methods within the User class itself to add/remove photos from the $photos array. IMO, both ways seem equally logical on the surface (not looking at the rest of your system), so feel free to use the one you think fits best. Regarding getAllPhotos, I left it rather sparce on purpose. You can have it create Photo objects and return an array of those. In fact, you should probably have another method that does that. I left it like this because I saw it as more of a utility method. You may only want the raw DB info and not Photo objects. A better way to do this would be to rename this method something like loadAllPhotos, and have another getAllPhotos method invoke it and use the raw data to create Photo objects.
  12. A PhotoManager class would definitely clean things up (I'm kinda annoyed I didn't think of it myself ;-) ). You could put the grunt work of dealing with the DB within it. I'm thinking along the lines of: <?php class PhotoManager { private $instance; private final function __construct(){} public static function getInstance() { if(!self::$instance) { $this->instance = new self(); } return $this->instance; } public function getAllPhotos(User $user) { $userId = $user->getId(); $query = "SELECT * FROM userphotos WHERE userId = '$userId'"; $result = mysql_query($query); return $result; } public function deleteAllPhotos(User $user) { $userId = $user->getId(); $query = "DELETE FROM userphotos WHERE userId = '$userId'"; $result = mysql_query($query); } public function deleteGallery() { $query = "DELETE FROM userphotos"; $result = mysql_query($query); } . . . } class User { . . . private $photos = array(); public function getAllPhotos() { $manager = PhotoManager::getInstance(); $photos = $manager->getAllPhotos($this); //note, $photos != $this->photos return $photos; } public function displayAll() { $photos = $this->getAllPhotos(); if($photos) { while($row = mysql_fetch_assoc($photos)) { echo "<img src='{$row['url']}' alt='{$row['caption']}' /><br />"; } } else { echo "No photos found"; } } public function deleteAllPhotos() { $manager = PhotoManager::getInstance(); $manager->deleteAllPhotos($this); } } ?>
  13. I think you should use the mysql_insert_id() function (http://www.php.net/manual/en/function.mysql-insert-id.php). It returns the ID if the last INSERT query. So, you'd upload the raw info first (url, caption), then create a new Photo object using that info and the result generated from mysql_insert_id().
  14. Well, the first thing to keep in mind is that nothing is set in stone. If you need another parameter, or method, then feel free to add it. One of the things I used to get stuck with was looking at examples (such as the ones other people here would give me) as though they were hard rules to follow. They're not. They're just an illustration on one way of approaching a problem. The biggest thing to get is the concepts behind the code examples. Okay, now to the code. A method to get all photos for a particular user is very simple. Just return the photos array: <?php class User { . . . public function getPhotos() { return $this->photos; } } $Kevin = new User(15); $Kevin->addPhoto("vacation.jpg", "My summer vacation"); $Kevin->addPhoto("dog.jpg", "My dog"); $kevPhotos = $Kevin->getPhotos(); foreach($kevPhotos as $photo) { echo "<img src='{$photo->getUrl()}' alt={$photo->getCaption()}' /><br />"; } ?> Deleting a photo is a bit more work, but still not too complicated. The first step is in finding the photo we want to delete. The second is in deleting it from the database. The final step is removing it from the user. <?php class User { . . . public function deletePhoto($photoId) { $tmp = array(); //temporary array to hold non-deleted photos for($i = 0; i < count($this->photos); i++) //loop through all the photos for this user { if($this->photos[$i]->getId() == $photoId) //if it matches the one we want to delete, store it in the $delPhoto variable { $delPhoto = $this->photos[$i]; } else //else, add it to the temporary array { $tmp[] = $this->photos[$i]; } } $this->photos = $tmp; //set the user's array to the temporary array...if we have a photo to delete, it WON'T be included because we already stored it in $delPhoto if($delPhoto) //if there's a photo to delete { $delPhoto->deletePhoto(); //delete it } } } class Photo { private $photoId; . . . public function __construct($photoId, $url, $caption) { $this->photoId = $photoId; $this->url = $url; $this->caption = $caption; } public function getId() { return $this->photoId; } public function getUrl() { return $this->url; } public function getCaption() { return $this->caption; } public function deletePhoto() { $query = "DELETE FROM userphoto WHERE IDPhoto = '{$this->photoId}'"; $result = mysql_query($query); } } ?> If you need the userId to delete a photo, then just pass the deletePhoto() method the user object, just like we did with addPhoto.
  15. What does it matter that you use '&&' in JavaScript? Virtually all C-style code uses '&&' as logical AND. I fail to see any gain this proposed switch would give. Indeed, I think it would look awkaward, and perhaps even confusing.
  16. The key to your confusion lies with the SQL query. Since you're selecting all values (SELECT *....), you're extracting all values. If you limited your query (SELECT petName....), then you'd only extract those values you explicitly specify. The variables appear to 'magically' work because of two things: mysqli_fetch_assoc() and extract(). The first tells it to use the DB column names in order to reference the results, and the second pulls the results and assigns them to variables constructed out of those column names (http://us3.php.net/extract).
  17. Mind posting your form? It's hard to see exactly what you want without it. Also, put your code in CODE tags. Press the # button to activate them.
  18. I think some of your code is missing. Try putting it inside of CODE tags (click the # button).
  19. Looks like you're having some array trouble. Remember: the superglobal values (i.e. $_POST, $_GET, etc) are the arrays. Every time you use square-bracket notation with them ($_POST['field2']), you're specifying an individual element of that array, located at the value specified within those brackets. In your case, your value is repeating because you're telling it to hit field2 repeatedly. Now, here's what you should do: <?php $length = count($_POST) - 1; //don't want to include the submit button $message .= "You ordered product(s):<br /><br />"; for($i = 0; i < $length; i++) { $message .= "{$_POST[$i]} <br />"; } ?>
  20. Yup, it's fine. Despite the need for encapsulation, objects also still need to communicate with each other. This is done by passing objects as arguments to another object's methods. The way I did it above - with the object passing itself as an argument to a function - is often a sign of delegation. You'll see it many more times as you get more comfortable with OOP.
  21. Remember, objects can contain other objects.: <?php class User { private $userId; private $userName; private $email; private $country; private $photos = array(); public function __construct($userId, $userName = null) { $this->userId = $userId; $this->userId = $userName; } public function addPhoto($url, $caption) { $photo = new Photo($url, $caption); //create a new photo $photo->upload($this); //upload it $this->photos[] = $photo; //add it to the user's array of photos } public function setId($id) { $this->userId = $id; } public function getId() { return $this->userId; } } class Photo { private $url; private $caption; public function __construct($url, $caption) { $this->url = $url; $this->caption = $caption; } public function upload(User $user) { $userId = $user->getId(); $query = "INSERT INTO photos (url, caption) VALUES ({$this->url}, {$this->caption}) WHERE user_id = '$userId'"; $result = mysql_query($query); if($result) { //success! } else { //failure } } } $Bobby = new User(23); $Bobby->addPhoto("vacation.jpg", "My summer vacation"); ?>
  22. Semantically, an ID is a unique identifier, so, logically, there shouldn't be two elements with the same ID anywhere on your page. So, no, you wouldn't be able to use document.getElementById() on them. What you can do is access the elements by their names: <script type="text/javascript"> window.onload = function() { var input1 = document.forms["myForm"].elements["input1"]; var input2 = document.forms["myForm"].elements["input2"]; input1.value = "something"; input2.value = "something else"; } </script> </head> <body> <form name="myForm" action="#" method="post"> <input name="input1" type="text" /><br /> <input name="input2" type="text" /> </form> </body> </html>
  23. Well, the culprit is this line of code: <?php echo 'document.write("Hello World!");'; ?< You can't echo a JavaScript statement in PHP and expect it to magically work. You have to do one of two things: 1. Use PHP to write <script> tags around the echoed code, forcing it to be written as JavaScript, so: <?php echo '<script type="text/javascript">document.write("hello world!");</script>'; ?> 2. Use PHP to echo a value to be obtained through AJAX, and then use JavaScript to actually print it correctly I haven't had the chance to look over all of your code, but your script declaration is pretty screwed up, too. 1. The language is JavaScript, not LiveScript. I'm not sure if (X)HTML automatically defaults to JavaScript if a bad value is passed to one of its attribute tags, but a quick Google search tells me that 'LiveScript' was the original proposed name for the language, nothing more. 2. Since I'm talking about the <script> tag, I should point out that the 'language' attribute is depricated. You should use the 'type' attribute instead. So, with those points out of the way, the correct way to declare script code is: <script type="text/javascript"></script> 3. Right now, your script is outside of the (X)HTML document structure. <script> tags are generally placed inside the <head> tag of an (X)HTML document, like so: <html> <head> <title>Example</title> <script type="text/javascript"> //script code goes here </script> </head> Your alert not showing up may be affected by this. At the very least, you should wait until the DOM loads: <script type="text/javascript"> window.onload = function() //wait until the entire (X)HTML document is loaded, so we're sure we can reference elements correctly { //rest of the code goes here } </script> 4. I'm not sure if you're actually passing the form to the function. Why? Because you tell it to send input.form. I'm not certain if JavaScript allows you to move back up the ladder like that. After all, a form contains inputs, but an input doesn't contain a form. An easier way to do it would be to remove the function call from the input element itself and place it back inside the script tags: <script type="text/javascript"> window.onload = function() { //your other functions go here var myForm = document.forms["myForm"]; //form reference in case you need that commented-out code that refers to an e-mail input var jobNumber = myForm.elements["jobnumber"]; //reference to the jobnumber element jobNumber.onblur = function() //onblur event handler { alert("Active"); if(this.value != "") { sndReq(this.value); } } } </script> And yes, I do mean remove the script from your input element: <input type="text" name="jobnumber"> Since your main project is written in PHP, I suggest putting all of the reused markup code in a separate file and include()/require() it. It's a much better way to do things than echoing out the beginning of an HTML document on every page.
  24. Awesome! I'm glad I was able to help. Let me know if you need anything else.
  25. You know what? The script is working. I just noticed that when I click on a checkbox, the border surrounding the <td> elements disappears. It looks like the problem is some sort of CSS collision. I think it's because you originally have: table#matrix td{ padding:10px; background-color:#ccc; } For all the cells, and the new active class isn't overwriting the background color, for whatever reason. Try giving the <td> elements a class of their own that I can toggle.
×
×
  • 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.