Jump to content
Larry Ullman's Book Forums

Antonio Conte

Members
  • Posts

    1084
  • Joined

  • Last visited

  • Days Won

    126

Everything posted by Antonio Conte

  1. I don't really know what to suggest to you. Larry might have something to add. I would recommend you to learn how to use objects, learn a little about algorithms, and to just play with PHP. I tried a youtube-search, and looks like a good introduction to algorithms. It is not written in PHP, but it applies to PHP as well. Do not focus to much on math here. What's important is to have a basic understanding of how fast algorithms are when finding/sorting etc. I haven't looked trough more than one video, but it looks promising. This will give you an understanding on why you should use associative arrays in PHP (They are called hash maps in some other languages) and how to write fast code in general. This will give you a better understanding of how data is saved and manipulated inside a PHP array. (Read about PHP arrays in the manual) What's special with PHP arrays are that they can be used as Stacks, queues, hash maps, linked lists and general collections in. In many other languages, these are different data structures with different strengths and weaknesses. Understanding arrays are in other words VERY important in PHP.
  2. They are pretty similar. Textpattern is like a scaled down version of Wordpress. You don't have themes or pages, but else they are pretty much the same. I would recommend wordpress really, but we are currently locked to TXP due to the football system. I'm working on building those kind of systems myself, and then I think a switch to Wordpress/something similar will happen. Nice theme btw.
  3. 1. Download it again and increase the column capacity. That data is truncated by the database now. When you have a column which holds the correct 8 chars, trying running it again. You can do this if you don't wanna download postals/etc again, but I recommend trying that first. It's just much easier and also what you are really looking for. You MAY, instead of substring/similar functionality, try with a like instead for testing. What like = 'string%' does is to match with string + any other data that might fit. like = 'Larry U%'; would match Larry Ullman for example. SELECT * FROM table WHERE postal like = 'AB10%'; 'AB10' would then match 'AB10 5AU'. But as I said. Download the postal coded again and save them to a column with 8 chars capacity. 2. Try to do it straight in phpmyadmin first. When you have 2-3 correct distances calculated, you can be pretty sure it's working. That is what I would've done. Do not bother with PHP before it works correctly.
  4. The variables are only regarded as false because of how if() works. The variables are really 'null' (empty) and thus the code if ( $var ) will be false. This would also be the case without instantiating the variables, but you would also get the php error "undefined variable" in your scripts. You will more often see Larry checking if an error array is empty (code run without error) than checking a specific variable. It does not make sense to use the value of a variable OR set the variable to true/false. $error_array = array(); $firstname = $_POST['firstname']; // check VALUE of $firstname If ( strlen($firstname) < 2 ) { // firstname IS under 2 chars long. Define an error $error_array[] = "Firstname must be at least 2 characters long"; } // error array will be empty if no errors exist If ( empty($error_error) ) { echo $firstname . ' is over 2 chars long! Yay'; } else { Echo $errors_array[0]; } I'm sorry, but I write on mobile. Hope this was clear enough.
  5. Recursion is a very specific programming solution. In most cases regular loops will do the job perfectly, but in those special cases the power of recursion cannot be beaten. If you read up on recursive programming, you will see there's even rules for how to write recursive algorithms. If you are interested in the more advanced asPects of programming, you should read up on big O-analysis, data structures and object-oriented programming. How does functions like mysql_fetch_array() works? Why is array_key_exsist a faster function than in_array in most cases? Getting a better understandment for how basic functionally works if often a good idea. Could you write a custom function that is faster than in_array()? Could you implement the iterator used in mysql_fetch_array() in your own code for easy array iterations? These are possible next steps.
  6. 1: if the columns holds, similar data, I would use the same type on both columns, yes. Without any guarantees, the join would probably work anyway. The only thing to do is to test this. 2. That is hard to say. If I remember correctly, Larry used a saved procedure in the database to calculate distances. Are you using this procedure? Have you seen if the query works inside phpmyadmin/similar? You need to isolate the problem by testing both your queries and php step by step. Don't expect code to work flawlessly on this level of programming.
  7. Your array is most likely empty. That is why you are getting the foreach error. As rob says, this is most likely because you fail to retrieve the rows from DB. Show us some code.
  8. Outer joins are inclusive. It allows null's, like if a staff dosen't speak another/any languages, while Inner Joins are exclusive and will only show those who have a language in the staff_language table. I don't know your structure, but this is should be done with three tables. One for staff, one for languages, and one for staff_languages. SELECT s.first_name, s.last_name FROM staff AS staff[/font] OUTER JOIN staff_lang AS staff_lang ON ( staff.id = staff_land.staff_id ) INNER JOIN languages AS lang ON ( lang.lang_id = staff_lang.lang_id ) Table structures: staff ( id, firstname, lastname, other data) staff_lang ( lang_id*, staff_id* ) languages ( lang_id, name, other data ) As you can see, staff_lang should be only foreign keys to the staff and languages tables. A combined lang_id/staff_id in staff_lang will allow a staff to speak several languages. I would then do an outer join for the staff_lang on the staff table to get all staff and their languages spoken. A Inner join from staff_lang to languages ensure all information about the language is found.
  9. I think you're right. Because "The Object class" is not the base class for all objects, you don't have a lot of the Java tools to implement or extend. Things like compareTo(), equals() and hashCode() are very important in Java, but a lot of this functionality is spread around in PHP, as you would more likely use an associative array than hashCodes in the object itself. I thought about another important difference yesterday, namely the super global arrays like POST and GET. In PHP, you can skip a lot of reference passing just because of this. When you have a native way of passing data, no need to build objects and pass those data along the line. It just makes sense. I have not. Thank you, Larry. This is very familiar to me with structures such as SplStack, SplQueue, SplDoublyLinkedList, ArrayObject and so on. This is really what I've been looking for. The whole point of an array wrapper is to create easy to use functionality. A stack, a queque and maybe also the LinkedList is what I need. I don't LinkedLists are really that useful in PHP because of how the arrays works.Scratch that. If adding is done alphabetized, numerical or whatever your data's are, you could do cool things like binary search when getting elements. That would be really cool to implement. I hope PHP will get closer to a strongly typed language as time goes, but still keep the simplicity that's one of PHP's strong suits. I would definitely like argument type hinting and return type hinting for the basic data structures like integers, doubles and strings. I find I need to check too much data right now because this is not part of the language. Interesting topic, Larry. I will check out those classes and maybe publish some code if it seems about right.
  10. Just wanted to update on this. I decided to go for developing my own Collection based on a normal array. I built an abstract superclass which should be extended by small sub classes. The Collection implements an interface Listable, with the sole method get_key( ) which is used to set the key in the array for our objects. The point is that Collection implements Listable WHILE having Listable object hinting for the methods. (This allows several collections to be the array elements of ONE collection.) The collection will offer functionality such as add( $object ), remove( $anytype ), get( $key ), getAll( ), contains( $anytype ) and size( );. The abstract collection looks like this: <?php interface Listable { /** * GET_ID() * Collections need a clear and unique ID to work */ public function get_key ( ); /** * __toString() * Must implement the magic method toString */ public function __toString ( ); } <?php abstract class Collection implements Listable { private $array = null; public function __construct ( ) { $this->array = array(); } public function add ( Listable $object ) { // Remove duplicates if ( $this->contains($object) ) { return "Object already exists"; } // Add to array $this->array[$object->get_key()] = $object; } public function add_event( Event $object) { $key = count($this->array)+1; // Set key as object id $object->set_key($key); // add to array $this->array[$key] = $object; } public function remove ( $anytype ) { $key = $this->retrieveKey($anytype); unset($this->array[$key]); } /** * GET * Get an object from the array * @param Object The array key to return * @return Object The object if found, else null */ public function get ( $key ) { // Get The object return (isset($this->array[$key])) ? $this->array[$key] : null; } public function getAll ( ) { return $this->array; } public function size ( ) { return count($this->array); } /** * CONTAINS * Checks wheter the object is in the array * * @param anytype $anytype A key or an object * @return boolean true if not in array, else false */ public function contains ( $anytype ) { $key = $this->retrieveKey($anytype); return ($key != null) ? true : false; } /** * RETRIEVE * Retrieve object key * * @param anytype $anytype A key or an object * @return boolean key if in array, else null */ private function retrieveKey ( $anytype ) { // Check whether anytype is a Listable object if ( $anytype instanceof Listable ) { return (isset($this->array[$anytype->get_key()])) ? $anytype->get_key() : null; } // Use key directly return (isset($this->array[$anytype])) ? $anytype : null; } } A typical subclass of Collection would look like this: <?php class Round extends Collection { private $id; private $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function add ( Match $club ) { parent::add($club); } public function get_key ( ) { return $this->id; } public function __toString() { return $this->name; } } ?> Or this: <?php include(&#39;event.php&#39;); include(&#39;goal.php&#39;); include(&#39;card.php&#39;); class Match extends Collection implements Listable { private $id; private $home_team; private $away_team; private $kickoff; private $home_goals; private $away_goals; private $number_of_events = 0; public function __construct($id, $home_team, $away_team, $kickoff, $goals_home = null, $goals_away = null) { $this->id = $id; $this->home_team = $home_team; $this->away_team = $away_team; $this->kickoff = $kickoff; $this->home_goals = $goals_home; $this->away_goals = $goals_away; } public function add ( Event $event ) { parent::add_event($event); } public function get_key ( ) { return $this->id; } public function __toString() { return $this->home_team .&#39; vs. &#39;. $this->away_team .&#39; at &#39;. $this->kickoff .&#39; <br /> &#39;. $this->getEventsAsString(); } private function getEventsAsString( ) { $out = ""; $events = parent::getAll(); foreach ($events as $event ) { $out .= &#39;- &#39;. $event . &#39;<br />&#39;; } return $out; } } ?> I&#39;m guessing I will find errors/weird behavior initially, but for now, it works REALLY great. By extending a simple Collections class, I can simply add, remove and retrieve objects in a very simple matter. The next implementation will be to create an iterator object inside the Collection for simple output of the data. Application code would look like this: <?php // Create clubs $juventus = new Club(1, "Juventus"); $barcelona = new Club(2, "Barcelona"); $real = new Club(3, "Real Madrid"); // Create players $vuci = new Player(10, "Mirko Vucinic", 17, "Vucinic"); $del_piero = new Player(10, "Alessandro Del Piero", 10, "Del Piero"); $messi = new Player(10, "Leonel Messi", 10, "Messi"); $ronaldo = new Player(10, "Cristiano Ronaldo", 10, "Ronaldo"); // Create matches $match1 = new Match(1, $juventus, $barcelona, &#39;2011-05-20 20:45&#39;, 2, 0); $match2 = new Match(2, $real, $barcelona, &#39;2011-05-25 20:45&#39;, 0, 6); // Create events $match1->add(new Goal( $vuci, 40, $del_piero )); $match1->add(new Goal( $del_piero, 60, $vuci )); $match1->add(new Card( $vuci, 65, Card::$YELLOW_CARD)); $match1->add(new Card( $vuci, 75, Card::$RED_CARD)); echo $match1 . &#39;<br />&#39;; // Create events $match2->add(new Goal( $messi, 20 )); $match2->add(new Goal( $messi, 30 )); $match2->add(new Goal( $messi, 39, $ronaldo )); $match2->add(new Card( $ronaldo, 39, Card::$YELLOW_CARD)); $match2->add(new Goal( $messi, 60 )); $match2->add(new Card( $ronaldo, 61, Card::$RED_CARD)); echo $match2; ?> This produces the following output: Juventus vs. Barcelona at 2011-05-20 20:45 - Goal: Vucinic ( Assist: Del Piero ) 40&#39; min - Goal: Del Piero ( Assist: Vucinic ) 60&#39; min - Yellow card Vucinic 65 - Red card Vucinic 75 Real Madrid vs. Barcelona at 2011-05-25 20:45 - Goal: Messi 20&#39; min - Goal: Messi 30&#39; min - Goal: Messi ( Assist: Ronaldo ) 39&#39; min - Yellow card Ronaldo 39 - Goal: Messi 60&#39; min - Red card Ronaldo 61 And print_r: MATCH 1: Match Object ( [id:Match:private] => 1 [home_team:Match:private] => Club Object ( [id:Club:private] => 1 [name:Club:private] => Juventus ) [away_team:Match:private] => Club Object ( [id:Club:private] => 2 [name:Club:private] => Barcelona ) [kickoff:Match:private] => 2011-05-20 20:45 [home_goals:Match:private] => 2 [away_goals:Match:private] => 0 [number_of_events:Match:private] => 0 [array:Collection:private] => Array ( [1] => Goal Object ( [id:Goal:private] => 1 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Mirko Vucinic [shirtname:Player:private] => [shirtnumber:Player:private] => Vucinic ) [time:Goal:private] => 40 [assist:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Alessandro Del Piero [shirtname:Player:private] => [shirtnumber:Player:private] => Del Piero ) ) [2] => Goal Object ( [id:Goal:private] => 2 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Alessandro Del Piero [shirtname:Player:private] => [shirtnumber:Player:private] => Del Piero ) [time:Goal:private] => 60 [assist:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Mirko Vucinic [shirtname:Player:private] => [shirtnumber:Player:private] => Vucinic ) ) [3] => Card Object ( [id:Card:private] => 3 [player:Card:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Mirko Vucinic [shirtname:Player:private] => [shirtnumber:Player:private] => Vucinic ) [time:Card:private] => 65 [type:Card:private] => 1 ) [4] => Card Object ( [id:Card:private] => 4 [player:Card:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Mirko Vucinic [shirtname:Player:private] => [shirtnumber:Player:private] => Vucinic ) [time:Card:private] => 75 [type:Card:private] => 2 ) ) ) 1 MATCH 2: Match Object ( [id:Match:private] => 2 [home_team:Match:private] => Club Object ( [id:Club:private] => 3 [name:Club:private] => Real Madrid ) [away_team:Match:private] => Club Object ( [id:Club:private] => 2 [name:Club:private] => Barcelona ) [kickoff:Match:private] => 2011-05-25 20:45 [home_goals:Match:private] => 0 [away_goals:Match:private] => 6 [number_of_events:Match:private] => 0 [array:Collection:private] => Array ( [1] => Goal Object ( [id:Goal:private] => 1 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Leonel Messi [shirtname:Player:private] => [shirtnumber:Player:private] => Messi ) [time:Goal:private] => 20 [assist:Goal:private] => ) [2] => Goal Object ( [id:Goal:private] => 2 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Leonel Messi [shirtname:Player:private] => [shirtnumber:Player:private] => Messi ) [time:Goal:private] => 30 [assist:Goal:private] => ) [3] => Goal Object ( [id:Goal:private] => 3 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Leonel Messi [shirtname:Player:private] => [shirtnumber:Player:private] => Messi ) [time:Goal:private] => 39 [assist:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Cristiano Ronaldo [shirtname:Player:private] => [shirtnumber:Player:private] => Ronaldo ) ) [4] => Card Object ( [id:Card:private] => 4 [player:Card:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Cristiano Ronaldo [shirtname:Player:private] => [shirtnumber:Player:private] => Ronaldo ) [time:Card:private] => 39 [type:Card:private] => 1 ) [5] => Goal Object ( [id:Goal:private] => 5 [goal:Goal:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Leonel Messi [shirtname:Player:private] => [shirtnumber:Player:private] => Messi ) [time:Goal:private] => 60 [assist:Goal:private] => ) [6] => Card Object ( [id:Card:private] => 6 [player:Card:private] => Player Object ( [id:Player:private] => 10 [name:Player:private] => Cristiano Ronaldo [shirtname:Player:private] => [shirtnumber:Player:private] => Ronaldo ) [time:Card:private] => 61 [type:Card:private] => 2 ) ) ) Sorry for the long post. I understand that it would be difficult to comment upon this without having more code, but that seems a little impractical. I hope I've succeeded in demonstrating the simplicity in the class design. The question is: Why don't we see more code like this in PHP? I've never seen a Java-like Collection in PHP. Why do you think that is? Do you understand how the code works, or is this not the way most PHP applications are coded? Hoping for some answers.
  11. You need some kind of check like isset(). <textarea name="comments" rows="5" cols="45" ><?php if (isset($_POST['comments'])) { echo $_POST['comments']; } ?></textarea> That will remove the error.
  12. I recommend his book, but I have my own coding style. That's right. I like this explanation. "You don't know how a toaster works. It turns your bread into toast, but you don't care how." For the second part, I would say that really depends, but if you read on, he'll elaborate on when to do what later on. Jon: Yeah, as said I don't know JS. If you think that's a good way, I take your word for it. I trust your skills.
  13. It looks like you can create objects and properties in JS. (I don't know JS) Instead of creating global values, declare var1 to var4 as object members, and build an array with these values. When the loop is done, return the array. I will try to illustrate this in PHP. The class that does calculations: class someFunction { private $var1, $var2, $var3, $var4; private $calc = array(); public function __construct( $var1, $var2, $var3, $var4 ) { $this->var1 = $var1; // Same of 2, 3, 4 } public function calculate( $iterations ) { for ( $i = 0; $i < $iterations; $i++) { // Some calculation here $this->var1 += 0.5; $this->var4 *= 72; // Add calculation to array $this->calc[] = array( $this->var1, $this->var2, $this->var3, $this->var3); } // Return array when done calculating return $this->calc; } public function getValues ( return $this->calc; } } Use your new class: // Assign som values to variable $var1 = 12; #var4 = 2; ..... // Create calculation object $calc = new someFunction($var1, $var2, $var3, $var3); // Save calculations to Array $aLinks = $calc->calculate( 100 ); // Then you need these calculations someplace else.... $aLinks = $calc->getValues( ); (other code) // And some other place.... $newLinks= $calc->getValues( ); The object will hold this information for you instead of global variables. If you do not need an array, simply remove the array and return the single members by functions like getVar1(), getVar2. This way, calculations are done once and then saved inside the object instead of saved globally. It just makes more sense to me. If you need these values in other code, you pass along the object or the values. I don't know if you can do something like this in JS, but that would be the best way. Global variables are no good coding style in most cases, but your case MIGHT be legit. Remember that I don't know JS, but I speak from a general programming perspective. The most important thing is that the code WORKS. When it works, improve it.
  14. Move towards objects. That's really the way to go. If you think you have to many variables, you are probably right. The problem though is NOT speed or memory, because that's cheap with variables. It's that your code gets hard to maintain and understand. You should know that an object is really not taking up more memory than your common string or integer. Objects are not these huge, heavy things. It's the algorithms for sorting, filtering and validation (of array structures) you'd want to be critical about. As I mentioned to someone else. I created 300 000 Card objects (yellow cards in football) which itself holds a Player and a DateTime object. How much time did that take? 0.3 seconds for the instantiation. How long did this take to print (echo) to screen? About 1.8 seconds, (which is to long) but who would need to create 900 000 objects (1 card, 1 player, 1 datetime) in one PHP-script? The reason why I tell you this is to understand that objects are cheap too. Learn object-oritented design, and your code will get a lot better FAST. I would really recommend this book: http://www.amazon.com/PHP-Object-Oriented-Solutions-David-Powers/dp/1430210117
  15. Variable instantiation and object creations are pretty cheap. It's loop iterations you want to think about reducing. (If it's possible to remove an inner loop, do it) I don't know how JS garbade collection works, but Java removes any unused references and values. The reason this sometimes fails in Java is because you still have references to unused objects someplace you would not think about removing them. It's more of programming errors than about the garbage collector itself. I've never had any such troubles in the time I've developed java.
  16. It's just bad coding style. To keep code short is generally a very good idea. Simple solutions to difficult problems is always good, but readability is even more important. If you like short code, create several functions that keeps your code simple. The real problem here, however, is type. You should not return value OR a boolean. They are not the same type. Return value or null, and true or false. This will in the end give you code that is much easier to read and predict. Create your own functions for validating vital input. Good function names are very important to ensure that your code is easy to understand and use. (The do_some_calculations_on_integer() function is not a good name btw ) As explained in my code, you should first check that $_POST is even set. (form is sendt). $_POST is an array, so we can use isset() to se if the post ARRAY is set. This is also the way Larry says you should do it in his books. // Check that script is even sending post before anything if ( isset($_POST) ) { $integer = $_POST['val']; if ( valid_integer($integer) ) { echo 'Input is an integer. '; // Calculate the value according to a method. $new_value = do_some_calculations_on_integer( $int ); // We check that function did not return null if ( $new_value != null ) { echo $new_value; // Value is ok! Put in database or whatever } } else { echo 'Input is not an integer.'; } } function valid_integer( $int ) { return ( ctype_digit( (string) $int ) && is_int( (int) $int ) ) ? true : false; } function do_some_calculations_on_integer( $int ) { // This is the validation of input, if needed. I have just said that the $int argument should be between 1 and 9. // You will of course replace this what what you need. (Maybe you can skip the whole validation? It depends.) if ( 0 < $int < 10 ) { // Some random calculations. This is to illustrate that you can manipulate value inside a function instead of in code. $exponent = pow($int, 3); $devision = $exponent / 2; $pi_multiplcate = $exponent * pi(); return (int) $pi_multiplcate + 42; } else { // We only allow values between 1-9 allow for some reason. Value is not what we wanted. // We indicate this by null or some other value that CAN'T be right. (-1 is an option if we only want positive numbers) return null; } }
  17. Hey, everyone How common is it develop some sort of collection in PHP? I do this a lot in Java. I create methods such as add(), remove(), get(), containts(), isEmpty(), size() and iterator(). This allow me to handle a group of objects as a single object. It will also allow me to treat the collection as an array, stack or queue if I want to. I really love this flexibility. The client side code will often then look like this. // Create contact list $contact = new Contacts(); // Create a few person objects $larry = new Person("Larry Ullman", "telephone number", "Other info"); $thomas = new Person("Thomas Larsson", "telephone number", "Other info"); $jon = new Person("Jon somename", "telephone number", "Other info"); // Add a few contacts $contact->add($larry)); $contact->add($thomas); $contact->add($jon); // I don't need Jon anymore $contact->remove($jon); // Get an iterator to loop through contacts $contacts = $contact->Iterator(); // Print all contacts while ($contacts->hasNext()) { echo $contacts->next(); } It's just that I haven't seen any kind of code like this in PHP before. Is this because of PHP's array capabilities, or what is it? Because you can treat PHP arrays like maps, lists and normal arrays, the NEED for collections is not really that big. The collection is pretty much a wrapper for the array to make your life easier. I really need these kinds of collection in a large personal project, where I often has collection inside collection again. This is why I wanna know. While creating this thread, I decided to search at stack overflow. It has been addressed here, but I want your view too. http://stackoverflow.com/questions/1580223/java-like-collections-in-php
  18. I would say you should really try to code functional classes either way. I tend to ignore procedural code because I also code a lot of Java, which cannot be coded procedural like PHP. You will often code several functions to handle your code anyway. By creating classes, you will allow a clear cut use of your method. Because you can spesify private and protected methods in a class, you can program with a divide and concur mindset and code to a top-down approach instead of bottom-up. This is often the difference between those who have written a lot of code object-oriented and those who code procedural. As you become a better coder, you'll start to isolate the common problem areas in your code. PHP developers don't have to worry about problem such a arrayOutOfBoundsExceptions and such, but often you'll find similar problems you would easily need helper methods for. With all good programming, you'll tend to see a much more clear language in code, namly spoken language. This is an example from Java validating array positions in a multidimensional array, but it applies to PHP to. I've rewritten it to PHP here: // Class something public function addCell($row, $col) { // Validate array position if ( ! $this->validKey($row, $col) ) return; // If position is empty, add Cell object if( $this->emptyPos($row, $col) ) { $this->add($row, $col); } // If position exist and drawing mode is by click, remove object else if ( $isDrawnByClick && ! $this->emptyPos($row, $col) ) { // Cell exists in array. Remove it $this->remove($col, $row) } } private function add($row, $col) { // If array position is empty, add object if ( $this->emptyPos($row, $col) ) { $this->collection[$row][$col] = new Cell($row, $col); } } private function emptyPos($row, $col) { // Check if key is valid. Return true on empty position. False if set. if ( $this->validKey($row, $col) ) { return ( $this->collection[$row][$col] == null) ? true : false; } } private function validKey($row, $col) { return ( (0 <= $row && 0 <= $col) && ($this->numOfRows < $row && $this->numOfCols < $col) ) ? true : false; } This code is simply checking whether a clicked coordinate in a grid system is inside the grid. It's fairly simple programming. Because I've added helper methods such as emptyPos() (who also uses validKey()) to check array positions, I do not need to check input. All I have to do to alter this programs allowed array positions, is to change the objects member numOfRows and numOfCols. If I coded this in PHP, I would only have to add a SINGLE method to determine whether the input is a valid integer. (Java is stronly typed.) This illustrates the power of object orientation. Sure you could just as simply create these functions inside procedural code. The problem arrives when you need this functionality in another script. Sure, you could include it into your scripts. Another problem arise when you need small alterations for different uses. You'll then have to create several different functions to handle these alterations. Which of these should another programmer use? Without possibility to add viability level to a method, all kinds of functions are then available to the new coder. It just starts to get chaotic. Someone using your code, including yourself, will be very happy to be able to do something like this: $collection = new Collection(30, 50); $collection->addClick(10, 15); // Valid click. Cell object added to array $collection->addClick(311, 51); // NOT valid click. Everything still works as expected. Object not added Starts writing $collection->...(Looks in IDE. Programmer sees the removeClick() method) $collection->removeClick(10, 15); // First Cell object removed I'm not saying procedural code is wrong. In most cases though, you'll see most programming tasks has been done before. No need to re-invent the wheel each time. As you build classes, more and more code can be re-used. You'll become a better programmer, more in tune with the professional world, and will write more clear-cut code. The transition to other languages is also a lot smaller. Start coding object-oriented. You'll really see the advantages in the long run.
  19. This is the right way to do it. But your revised tables are not INF1. They are boyce-codd or INF3. The number indicates what kind of normalization level you are on. INF1 is almost useless for tables as they are then redundant and lack integrity. INF2 means there is an unwanted dependency like postal code / postal address in the same table. The table is INF3/Boyce-codd if you remove the dependency. This is what we are looking for. Hope this makes sense. Read up on normalization definition. You seem to have figured out how it works though.
  20. Try removing the differences between them, then you won't be locked to either of them. Both eclipse and netbeans works on both os. The main difference then, is only keyboard. (different strokes for [{$ etc) FileZilla (FTP Program) also works in both os. Same for xampp (local apache server with php and MySQL). I like to code php on a Mac simply becose of the dollar variable keys. It took some time, but now I transition between them without a thought. Non of them are any better than the other really. This is php, a web coding language. Os dont really matter.
  21. Don't force to many rules when Not necessary. If someone wants to use something stupid as a name, allow them. It will not break your application. Take the telephone example. If you want country codes, define another input field for it. As numbers are different in countries, make sure it a plausible number. If the number is truly important, make them validate it by using a code sent by SMS. If not, allow some slack. You cannot check everything.
  22. I agree with Larry here. With his suggested db design, there's no need to worry about data size. Indexed tables are fast. Size do not matter as the algorithms know where to look for the requested data. There's no more iterations than asked for.
  23. I think it has to do with types. "false" as a string is not the same as the Boolean value. Try a negative number as suggested
  24. Just Add an <option> before your for-loops and set them to be default. The value could be something like -1 as it's easy to check against.
×
×
  • Create New...