Jump to content

OOP Help


cmr

Recommended Posts

I have recently decided to make my current project OOP have read a few tutorials on it but im not expert but had a go at making my registration page object orientated. Can someone tell me where im going wrong and point me in the right direction please. much apreciated

 

<?php

include('connect.php');

 

class Registration

{

$first=$_POST['frm_first'];

$last=$_POST['frm_last'];

 

$first_lower=strtolower($first);

$last_lower=strtolower($last);

$user=$first . "." . $last;

 

$pass=md5($_POST['frm_pass']);

$dept=$_POST['frm_dept'];

$job=$_POST['frm_job'];

$email=$_POST['frm_email'];

$phone=$_POST['frm_phone'];

$mobile=$_POST['frm_mobile'];

$qual=$_POST['frm_qual'];

 

$errArray[] = 0;

$error = 'false';

 

function Registration()

{

if(empty($_POST))

{

$registration = new Registration();

$registration->displayForm();

}

else

{

$registration->errorCheck()

 

if($error != 'true')

{

$registration->registerUser();

}

if($error = 'true')

{

$registration->displayErrors();

$registration->displayForm();

}

}

}

 

function errorCheck()

{

if(empty($_POST['frm_first']))

{

array_push($errArray, "Please specify a First name");

$error = 'true';

}

if(empty($_POST['frm_last']))

{

array_push($errArray, "Please specify a Lastname");

$error = 'true';

}

if(empty($_POST['frm_pass']))

{

array_push($errArray, "Please specify a Password");

$error = 'true';

}

if(empty($_POST['frm_dept']))

{

array_push($errArray, "Please specify a Department");

$error = 'true';

}

if(empty($_POST['frm_job']))

{

array_push($errArray, "Please specify a Job Title");

$error = 'true';

}

if(empty($_POST['frm_email']))

{

array_push($errArray, "Please specify an Email");

$error = 'true';

}

}

 

public function displayForm()

{

?>

<p>Fill in the form below with your details</p>

<form class="frm" action="register.php" method="post">

First Name <input type="text" name="frm_first" maxlength="20" value="<?php echo $first ?>" /><br />

Last Name <input type="text" name="frm_last" maxlength="20" value="<?php echo $last ?>" /><br />

Password <input type="password" name="frm_pass" maxlength="20" value="" /><br />

Department

<select name="frm_dept">

<option value="<?php echo $dept ?>"><?php echo $dept ?></option>

<option value="F1">F1</option>

<option value="F2">F2</option>

<option value="F3">F3</option>

<option value="F4">F4</option>

<option value="F5">F5</option>

<option value="F6">F6</option>

<option value="F7">F7</option>

<option value="F8">F8</option>

<option value="F9">F9</option>

</select><br />

Job Title <input type="text" name="frm_job" maxlength="40" value="<?php echo $job ?>" /><br />

Email <input type="text" name="frm_email" maxlength="40" value="<?php echo $email ?>" /><br />

Phone <input type="text" name="frm_phone" maxlength="15" value="<?php echo $phone ?>" /><br />

Mobile <input type="text" name="frm_mobile" maxlength="15" value="<?php echo $mobile ?>" /><br />

Qualifications <input type="frm_qual" name="county" maxlength="20"  value="<?php echo $qual ?>" /><br />

<input type="submit" value="Submit" /><input type="reset" value="Reset" />

</form>

<a href='index.php'>Back to Main</a><br />

<?php

}

 

public function registerUser()

{

mysql_connect($sql_server,$sql_username,$sql_password);

mysql_select_db($sql_database) or die( "Unable to select database");

 

$query = "INSERT INTO staff VALUES ('$user','$pass','$first','$last','$qual','$email','$phone','$mobile','$dept','$job','NULL','n','n');";

 

$result = mysql_query($query);

 

mysql_close();

 

if (!$result)

{

array_push($errArray, "User Account Already Exists");

$error = 'true';

}

else

{

echo "Registered $user<br />";

echo "<a href='index.php'>Back to Main</a><br />";

}

}

 

public function displayErrors()

{

echo "There were errors in your form:<br />";

$count = 0;

reset($errArray);

foreach ($errArray as $value)

{

if ($count != 0)

{

echo "<li>" . $value . "</li>";

}

$count++;

}

}

}

?>

Link to comment
Share on other sites

Properties (variables) with a class need to be accessed via $this-> eg;

 

<?php

  class foo {
    public $f = 'hello';
    function sayhi() {
      echo $this->f;
    }
  }

  $foo = new foo();
  $foo->sayhi();

?>

 

I think you need to find some more tutorials. There are quite a few problems with your code.

 

ps;Just because your using classes does not meen its oop.

Link to comment
Share on other sites

Well its my first try at OOP in PHP. It may not be strictly OOP at the moment but it make it a lot more readable than it was before.

 

Apart from messing up the variables im still not sure where im going wrong. the registration function is supposed to automatically load and load functions as necessary. Thats what im attempting to do anyway sorry if its basic stuff im getting wrong but i have to start somewhere.

Link to comment
Share on other sites

<?php
include('connect.php');

class Registration
{
   $first=$_POST['frm_first'];
   $last=$_POST['frm_last'];

   $first_lower=strtolower($first);
   $last_lower=strtolower($last);
   $user=$first . "." . $last;

   $pass=md5($_POST['frm_pass']);
   $dept=$_POST['frm_dept'];
   $job=$_POST['frm_job'];
   $email=$_POST['frm_email'];
   $phone=$_POST['frm_phone'];
   $mobile=$_POST['frm_mobile'];
   $qual=$_POST['frm_qual'];

   $errArray[] = 0;
   $error = 'false';

   function Registration()
   {
      if(empty($_POST))
      {
         $registration = new Registration();
         $registration->displayForm();
      }
      else
      {
         $registration->errorCheck()

         if($error != 'true')
         {
            $registration->registerUser();
         }
         if($error = 'true')
         {
            $registration->displayErrors();
            $registration->displayForm();
         }
      }
   }

   function errorCheck()
   {
      if(empty($_POST['frm_first']))
      {
         array_push($errArray, "Please specify a First name");
         $error = 'true';
      }
      if(empty($_POST['frm_last']))
      {
         array_push($errArray, "Please specify a Lastname");
         $error = 'true';
      }
      if(empty($_POST['frm_pass']))
      {
         array_push($errArray, "Please specify a Password");
         $error = 'true';
      }
      if(empty($_POST['frm_dept']))
      {
         array_push($errArray, "Please specify a Department");
         $error = 'true';
      }
      if(empty($_POST['frm_job']))
      {
         array_push($errArray, "Please specify a Job Title");
         $error = 'true';
      }
      if(empty($_POST['frm_email']))
      {
         array_push($errArray, "Please specify an Email");
         $error = 'true';
      }
   }

   public function displayForm()
   {
   ?>
   <p>Fill in the form below with your details</p>
   <form class="frm" action="register.php" method="post">
      First Name <input type="text" name="frm_first" maxlength="20" value="<?php echo $first ?>" />

      Last Name <input type="text" name="frm_last" maxlength="20" value="<?php echo $last ?>" />

      Password <input type="password" name="frm_pass" maxlength="20" value="" />

      Department
      <select name="frm_dept">
         <option value="<?php echo $dept ?>"><?php echo $dept ?></option>
         <option value="F1">F1</option>
         <option value="F2">F2</option>
         <option value="F3">F3</option>
         <option value="F4">F4</option>
         <option value="F5">F5</option>
         <option value="F6">F6</option>
         <option value="F7">F7</option>
         <option value="F8">F8</option>
         <option value="F9">F9</option>
      </select>

      Job Title <input type="text" name="frm_job" maxlength="40" value="<?php echo $job ?>" />

      Email <input type="text" name="frm_email" maxlength="40" value="<?php echo $email ?>" />

      Phone <input type="text" name="frm_phone" maxlength="15" value="<?php echo $phone ?>" />

      Mobile <input type="text" name="frm_mobile" maxlength="15" value="<?php echo $mobile ?>" />

      Qualifications <input type="frm_qual" name="county" maxlength="20"  value="<?php echo $qual ?>" />

      <input type="submit" value="Submit" /><input type="reset" value="Reset" />
   </form>
   <a href='index.php'>Back to Main[/url]

   <?php
   }

   public function registerUser()
   {
      mysql_connect($sql_server,$sql_username,$sql_password);
      mysql_select_db($sql_database) or die( "Unable to select database");

      $query = "INSERT INTO staff VALUES ('$user','$pass','$first','$last','$qual','$email','$phone','$mobile','$dept','$job','NULL','n','n');";

      $result = mysql_query($query);

      mysql_close();

      if (!$result)
      {
         array_push($errArray, "User Account Already Exists");
         $error = 'true';
      }
      else
      {
         echo "Registered $user
";
         echo "<a href='index.php'>Back to Main[/url]
";
      }
   }

   public function displayErrors()
   {
      echo "There were errors in your form:
";
      $count = 0;
      reset($errArray);
      foreach ($errArray as $value)
      {
         if ($count != 0)
         {
            echo "<li>" . $value . "</li>";
         }
         $count++;
      }
   }
}
?>
The above has many flaws, as mentioned.
One thing is you should try to seperate your presentation from your logic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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