1. This forum is in read-only mode.

PHP and HTML Guides

Discussion in 'Computers & Modding' started by Kage56, Nov 22, 2009.

  1. Kage56

    Kage56 Well-Known Member

    Does anyone know a site that has good guides to get a better understanding of PHP and HTML?
    I'm a bit confused and i'd like to get a better understand of it, from the basics if needed.
    I learned a bit but i sorta forgot most of them :\

    The first project i'd like to do is a mini site that displays information from the mySQL database.
    using array is confusing for me :X
     
  2. kamuikurou

    kamuikurou Well-Known Member

    marcy, a regular poster here gave me this link: http://www.php.net/ maybe you can start there. :)

    And I'm still learning it too, but I haven't learned how to use the database... :(
     
  3. Kage56

    Kage56 Well-Known Member

    yea, this was when i was trying to create a CMS for my personal MS pserver.
    i was interested because it looks like it'd be a fun thing to make
     
  4. kamuikurou

    kamuikurou Well-Known Member

    Ya, PHP is one of the best programming language and pretty fun and easy to make. :D
    And since it's integrated with the internet, I see it has a good future since we will do everything from our web more or less. :)
     
  5. Loonylion

    Loonylion Administrator Staff Member

    PHP is not in the same league as the likes of C etc. PHP is an interpreted scripting language.

    for HTML try www.w3schools.com
     
  6. Kage56

    Kage56 Well-Known Member

    um, wha? o_o

    thanks for the link, the navigation is a bit more friendly on this one :D


    oh, btw, can you explain what XHTML, PHP 2.0, 4.0. 5.2, etc. are? i've never knew anything beyond just PHP and HTML.
    if they're upgraded versions... how do you upgrade codes o_o?
     
  7. Loonylion

    Loonylion Administrator Staff Member

    XHTML is a more developed version of HTML, the coding rules are more strict mainly.

    PHP has different versions, 2.0 is absolutely ancient and you'll never see it anywhere anymore. 4.0 is out of date but you may still encounter it, and the current is 5.3.1. Different versions may have added functionality, different coding structures language constructs etc. For example, PHP 4 could not be object orientated, PHP 5 can. Generally you always want to use the latest stable version.
     
  8. Kage56

    Kage56 Well-Known Member

    what are objects?
     
  9. nomercy

    nomercy Well-Known Member

    Say a Car has Wheels and an Engine. Then Car, Wheel and Engine are Objects. They can contain certain properties or have certain functions. In this example, a Wheel has a diameter and you can start() an Engine. Objects are a way to constrain things and hide functionality from other Objects.
     
  10. Kage56

    Kage56 Well-Known Member

    do you think you could provide an example, i'm a bit lost o_O
     
  11. Loonylion

    Loonylion Administrator Staff Member

    There are three main concepts here: Objects, classes and namespaces.

    A class is a group of common properties discribing something, eg a car. All cars have wheels, an engine, a steering wheel etc.

    Code:
    Class car
    {
             private $engine;
             private $wheels;
             private $seats;
             private $steeringwheel;
             private $paintcolour;
    }
    
    These properties are common to all cars.

    Some cars are made by a different company, but they still have the same base properties defined in our car class, so we can inherit those properties into a new class:

    Code:
    Class Mazda extends car
    {
           private $radialengine;
    }
    
    Here our Mazda class has all the properties of car, but it also has the $radialengine property.

    Because cars are all different, they have different paint colours, different options etc, we are going to end up with hundreds of classes. So, instead, we create objects, which are like copies of a class, that have their own values for each of the different properties:

    Code:
    $mx5 = new Mazda;
    $mx5->engine = 2.0;
    $mx5->wheels = 4;
    $mx5->seats = 2;
    $mx5->steeringwheel = yes;
    $mx5->paintcolour = red;
    $mx5->radialengine = yes;
    
    We could also create an RX7 object, that would also be a mazda, but its properties would be completely independent of the mx5 object.

    Classes can also define methods, which are like functions to do specific actions. For instance, a car class would have a start() method to start the engine.

    Code:
    Class car
    {
             private $engine;
             private $wheels;
             private $seats;
             private $steeringwheel;
             private $paintcolour;
    
    Public function start()
    {
             start engine code here
    }
    
    }
    
    methods can be public (accessible to all other code) or private (accessible only to other code in the same class).

    A Namespace is simply a group of classes. e.g

    Code:
    namespace vehicle
    {
           class car
           {
           ....
           }
           class train
           {
            ....
           }
    }
    
     
  12. Kage56

    Kage56 Well-Known Member

    ok, i slightly understand how it works.
    i think i'm going to have to dive a bit deeper because so far you lost me after the first one w/ the inheriting thing.
     
  13. Seph

    Seph Administrator Staff Member

    OOP is not an easy concept to understand, what Loony described is really just skimming the surface. But don't worry if you don't get it all at once, programming is something that takes several months to learn and several years to master.
     
  14. Loonylion

    Loonylion Administrator Staff Member

    It took me about 2 years of teaching from both Seph and NoMercy (and other people) to understand OOP. I was disadvantaged by having programmed the old way for 12 years, however.
     
  15. Kage56

    Kage56 Well-Known Member

    you mind helping me out with this?
    i made a simple code for a calculator but the problem would be i don't know how to get it to work using "radios" for the +, -, /, *
    is it possible to shorten this, too? there's also a "divide 0 error", is there a way to get it the ignore the error and just bring up some text saying "can't start $a1 with 0, or a blank"?

    Code:
    <?php
    
    $a1=$_GET["answer1"];
    $a2=$_GET["answer2"];
    $add=add($a1,$a2);
    $sub=sub($a1,$a2);
    $div=div($a1,$a2);
    $mul=mul($a1,$a2);
    
    		  function add($a1,$a2)
    		  {
    			  $total=$a1+$a2;
    			  return $total;
    		  }
    
    
    			function sub($a1,$a2)
    			{
    				$total=$a1-$a2;
    				return $total;
    			  }
    			  function div($a1,$a2)
    			  {
    				  $total=$a1/$a2;
    				  return $total;
    			  }
    			  function mul($a1,$a2)
    			  {
    				  $total=$a1*$a2;
    				  return $total;
    			  }	  
    
    ?>
    
    
    <?php 
    if ($a1 == "")
    echo "<p>FILL IT IN!</p>";
    else
    echo "<p>" . $a1 . " + " . $a2 . " = " . $add . "</p>"; ?>
    <?php 
    if ($a1 == "")
    echo "<p>FILL IT IN!</p>";
    else
    echo "<p>" . $a1 . " - " . $a2 . " = " . $sub . "</P>"; ?>
    <?php 
    if ($a1 == "")
    echo "<p>FILL IT IN!</p>";
    else
    echo "<P>" . $a1 . " / " . $a2 . " = " . $div . "</p>"; ?>
    <?php if ($a1 == "")
    echo "FILL IT IN!";
    else
    echo "<p>" . $a1 . " * " . $a2 . " = " . $mul . "</p>"; ?>
    <?php 
    if ($a1 > $div)
    echo "<p>" . $a1 . " > " . $div . "</p>";
    else
    echo "<p>" . $a1 . " < " . $div . "</p>";
    if ($a1 > $add)
    echo "<p>" . $a1 . " > " . $add . "</p>";
    else
    echo "<p>" . $a1 . " < " . $add . "</p>";
    if ($a1 > $sub)
    echo "<p>" . $a1 . " > " . $sub . "</p>";
    else
    echo "<p>" . $a1 . " < " . $sub . "</p>";
    if ($a1 > $mul)
    echo "<p>" . $a1 . " > " . $mul . "</p>";
    else
    echo "<p>" . $a1 . " < " . $mul . "</p>";
    
    ?>

    edit: the greater than and less than were just for the hell of it, you should ignore that o_o
     
  16. Loonylion

    Loonylion Administrator Staff Member

    in your HTML file, you want the following:

    Code:
    <html>
    <head>
    <title>calculator</title>
    </head>
    <body>
    <form action="calc.php" method="POST">
    number 1:<input type="text" name="number1">
    <input type="radio" name="operation" value="add"> +
    <input type="radio" name="operation" value="sub"> -
    <input type="radio" name="operation" value="mul"> *
    <input type="radio" name="operation" value="div"> /
    number 2:<input type="text" name="number2">
    <input type="submit">
    </form>
    </body>
    </html>
    
    Then in calc.php:

    Code:
    <?php
    $num1 = (int)$_POST['number1'];
    $num2 = (int)$_POST['number2'];
    if ($num1 OR $num2 == null)
    {
    	die('cannot calculate');
    }
    else
    {
    	switch($_POST['operation'])
    	{
    		case 'add':
    			$total = $num1 + $num2;
    			$operator = '+';
    		break;
    		case 'sub':
    			$total = $num1 - $num2;
    			$operator = '-';
    		break;
    		case 'mul':
    			$total = $num1 * $num2;
    			$operator = '*';
    		break;
    		case 'div':
    			if ($num2 == 0)
    			{
    				die('cannot divide by zero');
    			}
    			$total = $num1 / $num2;
    			$operator = '/';
    		break;
    	}
    	echo $num1.' '.$operator.' '.$num2.' = '.$total;
    }
    ?>
     
  17. Kage56

    Kage56 Well-Known Member

    well, i figured out how 'switch' works and , thanks


    strangely enough, testing out the script gave me 'cannot calculate'
    i used dreamweaver for a code visual in case i forget to close things, or if i forget.
    'OR' wasn't highlighted meaning it was equivalent to average text, and uncapitalizing didn't change the previous result

    is this due to my PHP?
     
  18. Loonylion

    Loonylion Administrator Staff Member

    try replacing
    Code:
    if ($num1 OR $num2 == null)
    with
    Code:
    if ($num1 == null OR $num2 == null)
     
  19. Kage56

    Kage56 Well-Known Member

    that did it.
    i'm going to mess with other stuff