Jump to content

sharal

Members
  • Posts

    31
  • Joined

  • Last visited

    Never

Everything posted by sharal

  1. Here's a good place to start, and yes - implementing the pattern yourself does give you a better understanding of how it works. You could for instance start here: http://net.tutsplus.com/tutorials/php/create-your-first-tiny-mvc-boilerplate-with-php/
  2. Well it doesn't change the behaviour of the parent class, it just extends it. I don't see where you apply the LSP then. Anyway, you should not ponder things as LSP even though it is vital for making code act as we expect it to, not just yet atleast. Or that depends on how much you can comprehend. The baseclass duck does not implement fly, because the general duck, the abstraction of duck is not able to fly.
  3. Lets say your page url was : "http://www.mysite.com/script.php?id=1" where 1 is the id of the contact information in the database you want to retrieve. first off you want to retrieve the "id" parameter which we have set to '1' in the url (script.php?id=1) we do that by using $_GET['id']; $_GET['urlParameterNameHere'] retrieves an url parameter from the url address, in this case id. $id = $_GET['id']; // will be equal to '1' if the url ends in ?id=1 it would be '2' if it ended in ?id=2 etc. $query="SELECT * FROM contacts WHERE id='$id'"; $result=mysql_query($query);
  4. You can try something like this, there might be some minor parse errors, it's just a rough sketch. Site with the image on: top page php script session_start(); if(isset($_POST['submitted'])) { $redirectPage = 'http://yourside.com/pageAccessedByClickingImage.php'; $_SESSION['access'] = 1; echo '<meta http-equiv="refresh" content="0;url='.$redirectPage.'" />'; } the image link: <form id="myform" action="" method="post"> <img src="someImage" onclick="document.getElementById("myform").submit()" /> <input type="hidden" name="submitted" value="1" /> </form> The page only accessible by clicking the image, top page php code: session_start(); if(!isset($_SESSION['access']) || $_SESSION['access'] != 1) { exit('You cannot access this page directly'); } The page only accessible by clicking the image, bottom page php code: $_SESSION['access'] = NULL; unset($_SESSION['access'];
  5. sharal

    fatih

    this should do the trick: <script type="text/javascript"> var added = 0; function bul () { if(added == 0) { var element = document.createElement("input"); element.type ="text"; element.name ="sehir"; element.id ="sehir"; element.size = "51"; document.getElementsByName("talep_form")[0].appendChild(element); added = 1; } } </script>
  6. Strange, I pasted your code directly into my editor and ran it - with no problems whatsoever.
  7. sharal

    fatih

    Hi bdmfatih. onchange doesn't work with php as it is a client side html attribute, it does however work with javascript which might also solve your issue. <script type="text/javascript"> function bul () { var element = document.createElement("input"); element.type ="text"; element.name ="sehir"; element.id ="sehir"; element.size = "51"; document.getElementsByName("talep_form")[0].appendChild(element); } </script>
  8. it has something to do with your static method probably, I dont usually work with those so afraid I can't help you. My best guess is that it's not possible to access non static class variables(as those are different from each instance of the object) from within the static method, you can however, always access static variables from inside the static method. Class someclass { private static $test = 'hello'; public static function something () { echo self::$test; } } $test = new someClass; $test::something();
  9. Btw you might find this interesting: http://www.developer.com/lang/article.php/3628381/The-Object-Oriented-Thought-Process-Index-Page.htm it's a short article of a book named the "object oriented thought process", it explains interfaces, abstract classes and all the basics quite well. http://www.developer.com/design/article.php/3635836/Objects-and-Interfaces.htm
  10. Do you actually have a class wrapping around that? As the error indicates you are using the $this operator outside of the objects scope. Can you try to post the whole class with the class declaration too?
  11. Or simply: $myvalue = uniqid(); echo $myvalue; http://dk.php.net/manual/en/function.uniqid.php
  12. (int) typecasts the variable as an integer, hence all non-integer values will be gone. is_numeric($number) returns true, if $number is a numeric, but does nothing more. You could also use: $var = filter_var($myInteger, FILTER_VALIDATE_INT); // validates http://www.php.net/manual/en/filter.filters.validate.php also $var = filter_var($myInteger,FILTER_SANITIZE_NUMBER_INT); http://www.php.net/manual/en/filter.filters.sanitize.php
  13. This should probably be changed: if (!isset($_POST["$submit"])) Into: if (isset($_POST["submit"])) If you do the !isset($_POST['submit']), then your validation won't run if $_POST['submit'] exists, which it does the moment you submit your form as your submit input is named "submit"
  14. You should consider namespaces in vast projects containing a lot of code, and possibly where you are multiple employees working on the project. It will save you a lot of potential problems if you use pseudo namespaces as pointed out by Thorpe. As for the constructor, it's best sticking to the magic method __construct(), for instance, when you rename your class, you won't have to rename your constructor.
  15. Yes it does work as a sort of contract too, abstract classes can leave methods that children must implement as well, then they have to be declared abstract (look below). And yes, an abstract class can provide both methods with implementations and methods that must be implemented by inheriting child classes. eg: <?php abstract class Duck { private $name; private $size; // lets assume that the implementation of how ducks eat food is different, from duck to duck public abstract function eat (); /* abstract methods within an abstract class does not contain a method body, just like those of an interface, and must be implemented by inheriting children */ } class MallardDuck extends Duck { private $gender; public function fly () { } public function eat () { /* as theres an abstract method in the Duck class that we are extending, we must implement it in child classes (just like interfaces) */ } } ?> A little tip on when to use abstract methods: When you know that something will be different in the inheriting classes, but yet it's a feature that is common. For instance: if the MallardDuck and the DecoyDuck both could eat food, but they ate it in different ways (the implementation would differ), then you should have an abstract method in the Duck class named eat(). Doing that, forces MallardDuck, DecoyDuck and all other ducks to implement their own way of eating. The abstract method works as a "contract" again, and it literally defines that all ducks must be able to eat, but they must implement their own way of doing that. Additionally, we can also define in the abstract parent class Duck, that all ducks can Quack, and implement it in the abstract parent class - because all ducks quack the same way, furthermore we can also define properties that are common for all ducks.
  16. You should probably use a multidimensional array for this, so that you could store the quantity, product name and eventually a price too probably, in the same array. For instance if your products comes from a database. This won't exactly solve your issue, this is merely another way of doing it and you will have to look other places for further instruction in building a shopping cart this way. <?php $items = array(); function addToCart($productId) { if(array_key_exists($productId, $items)) { // the product id is already in the array, increment the quantity $items[$productId]['quantity']++; } else { $items[$productId]['quantity'] = 1; } } ?>
  17. <?php function create_user($params) { db_connect_posts(); $query = sprintf("INSERT INTO users SET users.screen_name = '%s', users.user_email = '%s', users.user_pwd = '%s', users.image = '%s', created_at = NOW()" , mysql_real_escape_string($params['screen_name']), mysql_real_escape_string($params['user_email']), mysql_real_escape_string(md5($params['user_pwd'])), /* the md5 function wrapping around the password string hashes the password with the md5 algorithm. the string "hello world" will always produce the same hash value, hence you can compare the inserted hashed password when you log your users in by, hashing the password from the login formula before comparing with the password, that is already in the database */ mysql_real_escape_string($params['image']) ); $result = mysql_query($query); if(!$result) { return false; } else { return true; } } function login($username, $password) { db_connect_posts(); $query = sprintf("SELECT * FROM users WHERE user_email = '%s' AND user_pwd = '%s'" , mysql_real_escape_string($username), // hashing the password again before comparing. mysql_real_escape_string(md5($password)) ); $result = mysql_query($query); $number_of_posts = mysql_num_rows($result); if($number_of_posts == 0) { return false; } $row = mysql_fetch_array($result); $_SESSION['user'] = $row; return true; } ?>
  18. try something like this: You will have to modify your code a little. By default when you submit a form with checkboxes, if its checked for instance $_POST['field14'] will be equal to 'On' if its not checked however $_POST['field14'] will be NULL, nothing that is. btw your second input field after the else block, the name of that is just '14' rather than 'field14', that could be it too. <?php if ($field14 == 'On') { echo "<p><label>ADR in Tank Class 1</label><input type='checkbox' name='field14' checked='checked'/></p><br /><br />"; } else { echo "<p><label>ADR in Tank Class 1</label><input type='checkbox' name='field14' /></p>"; } ?> as for inserting into the database with the values 1 and 0 you could do something like this: <?php $val; if($_POST['field14'] == 'On') { $val = 1; } else if(is_null($_POST['field14'])) { $val = 0; } // qry mysql_query("UPDATE mytable SET somefield = '$val'"); ?>
  19. exactly, if a method is declared abstract within the abstract class then the inheriting classes will be forced to implement it, otherwise it works exactly like a normal method being inherited. im sorry if you have read something like this below, but i'll try to explain it anyway, both the interfaces and abstract classes. It's hard to learn when to use abstract classes and interfaces to start off with, but if you are patient your efforts will surely pay off. This is one of the things that you can only learn by coding. Anyway, I hope this is of any help - feel free to question me if you need anymore answers or are in doubt about something, it's quite a large topic if you go into details, but i've tried to sum it up the best I could You should use abstract classes as a design feature when something is actually abstract, an abstract object could be something that is not tangible, a common example is a mammal. A mammal does not define a specific tangible object but is rather a generalisation / abstraction that defines some attributes and actions (methods) that are general for a lot of animals that are mammals (thus they inherit the mammals attributes and actions). Now a mammal is not really a good example is programming, as you will rarely imitate such an object. But basically as there cannot exist a mammal as a concrecte tangible thing - there is no such thing as a mammal, well yes a cat, but not a mammal in that sense. Therefore abstract classes cannot be instantiated, just like there is no such thing as a mammal walking around (literally a mammal, as if it was an unique animal itself). <?php $abstractClass = new abstractClass; // this will produce an error as abstract classes cannot be instantiated ?> A better example for programming could be a shopProduct. A shopProduct is a rather abstract term as it could basically define any product in your shop, as it does not present anything concrete but rather something that is general, it should be declared abstract. Then you could have tangible objects inheriting from the shopProduct like a bookShopProduct for example, if your shop sold books. As for interfaces: Interfaces defines a way to implement something, but does not provide the implementation. Instead, as you pointed out, it provides a "contract" that all objects implementing it must abide by. This ensures that you get a common class interface in all classes implementing that interface. So, when you have a class implementing that interface, you know that it will atleast implement those methods that are defined in the interface. An example from the Standard Php Library is the ArrayAccess interface which defines some methods that an object implementing the ArrayAccess interface must implement, in order to make the object act as an array (hence the word "ArrayAccess"). You can see at the php manual here http://dk2.php.net/manual/en/class.arrayaccess.php, that to implement ArrayAccess into our object, we have to implement the methods (these four methods are the contract, that we must fulfill /implement for our object to act as an array): offsetExists() offsetGet() offsetSet() offsetUnset() in order for our object to be accesible by arrays.
  20. ended up with this work around, it outputs the string as it's supposed to - well nearly. It causes javascript to stop executing because of some error, but nevertheless the auto complete on click works. <?php $str1 = '<div id="highlight'.$i. '"'; $str2 = 'onclick="swap'; $str3 = "('highlight$i')"; $str4 = '>' . $row['suggest'] . '</div>'; $base = $str1 . $str2 . $str3 . $str4; ?>
  21. Looks like javascript function values must look like this: swap('highlight1'); - so the single quotes are missing, just need php to return it with single quotes now then.
  22. It can be discussed whether this is actually, an issue with my php. But as i'm in doubt, i'll post it here. Who knows, most of you do probably have some experience with javascript as well. You do not need to worry about the xml http request object or anything, because that works. What i'm trying to do, is returning a div with an id that is generated via the php script. That also works perfectly well, however javascript doesn't seem to be reading the innerHTML that is dynamically returned. Enough talking, here's what i think you need of the php code to understand the problem: <?php $base = ''; $i = 0; while($row = mysql_fetch_assoc($query)) { $i++; $base .= '<div id="highlight'.$i.'" onclick="swap(highlight'.$i.')">' . $row['suggest'] . '</div>'; /* $base is succesfully returned to the auto_suggest tab. that works * Notice the click function, thats what's making trouble. Basically it's parsing the id with it in the swap function * So that it will be easy to extract with javascript.. or? */ } echo $base; return $base; ?> and here's the javascript: // javascript. function swap (fid) { document.getElementById("search").value = document.getElementById(fid).innerHTML; } so, i would now expect the swap(fid) to be - for instance swap(highlight1) and so on. Which it should be, but for some reason, fid appears to be undefined. To make it perfectly clear, what i want to do is: The server returns a response in a list below the search field, (now heres the problem) and when i click that div where the response is contained in, the response from that div is supposed to go into my search field (search). i've attached an image of how it looks. [attachment deleted by admin]
  23. Fixed it myself apparantly the path had to be $path = "xml/" rather than $path = "/xml/
  24. So, i'm having a problem with the php rename function. Basically my script 'delete_user.php' attempts to copy the users xml file(i stored all users information, in xml files as a backup of my database). They're all located in a folder named "xml", which is located in the root directory of my website, as is 'delete_user.php'. What i want to do is, that the file name is changed when the person's user account is deleted. Here's the code i've tried to do it with, i get an 'invalid directory / file error' when i try to run the script. <?php $path = "/xml/"; // you cannot see it here, but $login contains a username. (The username is the name of the file) // eg. an user named "sharal" would have an associated xml file named "sharal.xml" // this is not exactly what i want it to do, but it simply renames the file with it's original name // and adds "old" behind it $file = $login; $old = "old"; $new_path = $login . $old; rename($path.$file.".xml", $path.$new_path.".xml"); ?>
  25. yes, this is how it works in the reg part <?php $_SESSION['username'] = "$user"; // variable from html form. $_SESSION['pw'] = "$shapw"; $_SESSION['ses'] = "$ses"; $_SESSION['loggedin'] = 1; ?>
×
×
  • 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.