Jump to content

JonnoTheDev

Staff Alumni
  • Posts

    3,584
  • Joined

  • Last visited

  • Days Won

    3

Posts posted by JonnoTheDev

  1. As Thorpe has said your design is bad. From your post

     

    Actually i made some classes. one is general, one for database functions, one for images functions, one for blog, one for forum.

     

    I a real world scenario, a database has nothing to do with an image, an image has nothing to do with a forum (forum is a bit general, this should be split into component parts), and a forum has nothing to do with a database or a blog (again a blog is a bit general).

     

    Therefore, as classes they should not be inheriting each others methods. As I have stated i a previous post, if an object requires the use of another, there should be a method defined that can accept the object it requires. For instance, in your blog website there may be a function where a user could upload an image into their blog. In your blog class(es) there should be a method that deals with this functionality. Now this function will  need to use a method within the image class to upload and manipulate the image. The blog class should NOT inherit the image class! A method in the blog class should accept the image object as a variable.

     

    The main point of OO programming is to separate all the components. This is called loose coupling. If everything is tied together it is tightly coupled meaning you wouldn't be able to take part of the structure and use it in another project without bringing a lot of unneccessary stuff along with it.

     

    Can you now see the problem with your thinking?

     

    don't want to create separate objects for each class...

     

    You are definately not at this stage yet as you need more knowledge of OO programming, however there are what are called design patterns that are basically solutions to common issues in OO design. There is a solution to the problem in creating objects all over the place in your application called the Registry Pattern. I haven't read over this article completely but i'm sure you can do your own googling to find more info.

     

    http://www.phppatterns.com/docs/design/the_registry

  2. This is the code for 1 way where array 1 has 1 value and array 2 has more than 1. See if you can amment to do both ways.

     

    <?php
    $a1 = array('Arctictrek\'s Indiana' => array(6),
    		'Storm Kloud\'s Chosen to Win' => array(5),
    		'Storm Kloud\'s Keep the Win' => array(11),
    		'Storm Kloud\'s Hharmony' => array(12),
    		'Fire \'N Ice In Conclusion' => array(29),
    		'Jacbar Alaskan Black Night' => array(13),
    		'Rameslyn Cherokee Squaw' => array(14),
    		'Highnoons Kaskinampo of Jacbar' => array(27),
    		'Malnorska\'s Gypsy Lady' => array(28),
    		'Highnoons Loucheuse' => array(30));
    
    $a2 = array('Arctictrek\'s Indiana' => array(6),
    		'Storm Kloud\'s Chosen to Win' => array(11),
    		'Storm Kloud\'s Keep the Win' => array(23),
    		'Storm Kloud\'s Hharmony' => array(24),
    		'Fire \'N Ice In Conclusion' => array(25,29),
    		'Jacbar Alaskan Black Night' => array(13),
    		'Rameslyn Cherokee Squaw' => array(14),
    		'Highnoons Kaskinampo of Jacbar' => array(27),
    		'Malnorska\'s Gypsy Lady' => array(28),
    		'Highnoons Loucheuse' => array(30));
    
    // make sure both arrays have the same num elements
    if(count($a1) == count($a2)) {
    for($x = 0; $x < count($a1); $x++) {
    	$key = key($a1);
    	print $key."<br />";
    	$start_val = $a1[$key][0];
    	print "Start Value: ".$start_val."<br />";
    	for($i = 0; $i < count($a2[$key]); $i++) {
    		print "Sum: ".$start_val."+".$a2[$key][$i]."<br />";
    		$equals = $start_val+$a2[$key][$i];
    		print "Equals: ".$equals."<br />";
    	}
    	print "<br />";
    	next($a1);
    }
    }
    ?>
    

     

    Will produce

     

    Arctictrek's Indiana
    Start Value: 6
    Sum: 6+6
    Equals: 12
    
    Storm Kloud's Chosen to Win
    Start Value: 5
    Sum: 5+11
    Equals: 16
    
    Storm Kloud's Keep the Win
    Start Value: 11
    Sum: 11+23
    Equals: 34
    
    Storm Kloud's Hharmony
    Start Value: 12
    Sum: 12+24
    Equals: 36
    
    Fire 'N Ice In Conclusion
    Start Value: 29
    Sum: 29+25
    Equals: 54
    Sum: 29+29
    Equals: 58
    
    Jacbar Alaskan Black Night
    Start Value: 13
    Sum: 13+13
    Equals: 26
    
    Rameslyn Cherokee Squaw
    Start Value: 14
    Sum: 14+14
    Equals: 28
    
    Highnoons Kaskinampo of Jacbar
    Start Value: 27
    Sum: 27+27
    Equals: 54
    
    Malnorska's Gypsy Lady
    Start Value: 28
    Sum: 28+28
    Equals: 56
    
    Highnoons Loucheuse
    Start Value: 30
    Sum: 30+30
    Equals: 60
    

  3. Now i want to join all these classes into one class. suppose if i join all classes into general class and made object of general class,

    Sweet child of mine.. that is not the idea of OO programming. Objects instantiated from classes should mimic real world objects. They should be separated into their own space. If an object of type A requires the use of an object of type B then a method needs to be defined in A that can accept B as a parameter i.e

     

    <?php
    class A {
    
    function do_stuff() { }
    
    function i_need_obj_b_to_do_stuff(B $obj) {
    $obj->do_some_more_stuff();
    }
    
    }
    
    class B {
    
    function do_some_more_stuff() { }
    
    }
    
    // usage
    $a = new A;
    $b = new B;
    $a->do_stuff();
    $a->i_need_obj_b_to_do_stuff($b);
    ?>
    

  4. Are you saying that you want to test if the checkbox is selected or not? If so you would use the following example:

     

    $("#blocked").click(function() {
    if($(this).is(':checked')) {
      alert('checked');
    }
    else {
      alert('unchecked');
    }
    });
    

  5. Real basic, however

     

    Systems

    From individual PCs to networks including software / specific operating systems.

     

    Solutions

    What is offered in terms of hardware, software by the company. An example may be accounting software or Windows Server for a network server.

     

    Services

    This may range from hardware, software installation / sales to telephone or on-site technical support.

  6. An undefined index or undefined variable error means that you are trying to use a function on, or trying to use a variable / array key that doesn't exist.

     

    Here is your first snippet of code:

     

    <?php
    $Page = isset($_GET["Page"]);
    if (!$_GET["Page"])
    {
    	$Page=1;
    }
    ?>
    

     

    The logic here is incorrect. $_GET['Page'] only exists if a value for Page is passed through the url. The first function that you use is isset(). If no Page parameter exists in the url then $_GET['Page'] is a reference to nothing and your isset() function is producing the error. You are using this funtion in the wrong way. It is not a function thats return value should be assigned to a variable. It is used to test the existence of a variable or array key. Here is a refactored version:

     

    <?php
    if(!isset($_GET['Page'])) 
    {
    $Page = 1;
    }
    else {
    $Page = $_GET['Page'];
    }
    ?>
    

     

    You could also write this like:

     

    <?php
    $Page = isset($_GET['Page']) ? $_GET['Page'] : 1; 
    ?>
    

     

  7. mod_rewrite is an apache module that is loaded within the httpd.conf file allowing you to use friendly urls. Rules are effectively regular expressions (use wikipedia if you are unfamiliar with the jargon) that match a friendly style url and convert back into it's original form so your script functions correctly.

    If the apache configuration is set to read .htaccess files (which it most likely is) then the rewrite rules can be defined there. They can also be within your httpd.conf

  8. Images should be accessible via the document root for the domain i.e http://www.xyz.com/images/test.jpg Not stored on some random part of your hard disk on a windows pc. You should be running a webserver like Apache or IIS do define the document root for your website. To get an image to display that it outside of the document root you would either have to copy it into the doc root folder i.e images/ or, as long as it has sufficient permissions use php to display it using the following:

     

    <?php
    if($image = @fopen('d:\\images\\sani.gif', 'rb')) {
    header('Content-Type: image/gif');
    fpassthru($image);
    }
    ?>
    

  9. Thanks for that, thats a pretty comprehensive answer. You seem to be more pro framework than against it. Are there any you recommend?

    No probs. Really i'm neither for or against using PHP frameworks. If it is a very large project then it would be a recommendation. If it is a small to medium sized project then I probably wouldn't. I use my own framework library that covers most of the groundwork for any website of this size.

     

    I have only really worked with Zend and had a play with CodeIgnitor so I cannot say out of all of them which one is best. It's really a personal preference. What I would say is that if your project is going to have developers coming and going then I would go with a well known framework. I have never heard of Elgg.

  10. Separate the path into parts and take the bits you need i.e

     

    <?php
    $path = '/var/www/html/mywebsite/images/foobar.jpg';
    $parts = explode('/', $path);
    $filename = $parts[count($parts)-1];
    ?>
    <img src="/images/<?php echo $filename; ?>" />
    

  11. the second way can apply in reading from database right?

    Yes, if you are constructing an array from a database query i.e

    <?php
    $result = mysql_query("SELECT name FROM users WHERE job='Web Developer'");
    $developers = array();
    while($row = mysql_fetch_assoc($result)) {
    if(!in_array($row['name'],$developers)) {
      $developers[] = $row['name'];
    }
    }
    ?>
    

     

    as for the first way, can make the arrangement in the correct way?

    eg. if i put red in the third place and blue in the fourth place, when you print out it will skip index 3.

    The array_unique() function removes duplicates from the array as I have stated, therefore the array will only contain red, green, blue in that order (indexes 0,1,2). If you want to sort an array you can use a variety of functions. Why not check over the php manual and read the examples:

     

    http://uk.php.net/sort

     

    There are loads of functions for sorting arrays in the left column.

     

    Also, data should really be sorted from your initial database query using ORDER BY, etc. Also I could only query unique records from my database so I do not have to have any filter logic in my application code i.e

     

    <?php
    $result = mysql_query("SELECT DISTINCT name FROM users WHERE job='Web Developer' ORDER BY name ASC");
    ?>
    

  12. 1 nobody codes large online applications from scratch anymore (his words)

    Probably true. A large application is more than likely going to have a team of developers working on it over time. Developers will come and go. Using an established framework means that a new team member will not have to trawl through thousands of lines of bespoke code to understand what it is doing. Don't forget, an application still has to be built using a framework. I would refer to this as from-scratch also, it's just that you already have all the nitty gritty bits like database connectors, session management, etc there for you.

     

    Nobody will ever write every line of code themselves in an application. Just think when you last made a website. Did you put a Google map, or adverts on it? Did you use a template engine? Did you add any JQuery? All these functions and libraries are or require frameworks. You include the JQuery framework in a scrip tag before you write any javascript. You include the Google maps API framework in a script tag before you add the javascript code to display a map. You include the Smarty template framework in your code before you can create template files for your website. People say they don't use frameworks when they do without realising it.

     

    2 its more stable

    No, a framework may add additional overhead if you are not using it all. If you only require a small amount of functionality that the framework can offer, there is going to be lots of redundant code included. Good frameworks are usually optimised so they only load the necessary objects when required, however like all code there can be bugs. If you use the framework's functionality in an unorthodox manor then it is also likely that the end result will be poor.

     

    3 it forces the different developers to code within certain parameters thereby having a more uniform code base

    True, however the client code using the framework should be inline with the coding standards. The team of developers must also use the same standards throughout. This should be a practice that is written down and given to all developers. Just because a framework might use the following standard:

     

    class My_Framework_Class {
    
    }
    
    $variable_name = "";
    
    function Fuction_name() { }
    

     

    Does not mean that in my application I could write the following code:

     

    class myExtendedClass extends My_Framework_Class {
    
    }
    
    $variableName = "";
    
    function someOtherFunction() { }
    

     

    4 easier to continually add features to the site because of the framework

    No, it does not make it any easier. Frameworks save time, that's the main point because you have access to functions that do a certain job, meaning you would not have to write all the nitty gritty stuff yourself. If I want to add a new feature to a website, lets say a blog, how is a framework going to make it easier? I'm still going to have to write all the code to make it work, just less of it. If I wanted it easy, I would install a copy of Wordpress and link it into my site.

    Using a framework will mean that your code is object based, so it does have advantages such as not affecting other parts of a program when integrating new features. Essentially, using a framework should indicate that your code will be well structured, easy to follow, meaning that new functionality can be added with a relatively small amount of hassle. Completely bespoke code, over time can become a spaghetti like mess if not well documented.

×
×
  • 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.