OOP PHP


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

So i have been trying to expand my knowledge with PHP, and recently have taken on a project for a friend to build a website. In the process I ran into an issue, and I am not sure what is happening. Here is the relevant code:

Re-Programmed:

<link rel="stylesheet" type="text/css" href="../themes/addTheme.css">
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once('dbinfo.php');
include_once('authenticate.php');
class racer
{
    private $id, $firstname, $lastname,
    $banner, $bio;
    public function list_racers()
  {
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE);
    ?>
      <div id="form">
        <h3>Select Racer: </h3>
        <form method="post" action="">
        <?php
          $racer_list = "SELECT * FROM ArtecRacers";
          $get_racers = $mysqli->query($racer_list);
          while ($list = $get_racers->fetch_array(MYSQLI_NUM))
          {
            echo '<input id="part" type="radio" name="editRacer" value="' . $list[0] . '"/>';
            echo '<label for"part">' . $list[1] . ' ' . $list[2] . '</label><br />';
          }
        ?>
          <input type="submit" name="selectRacer" id="submit" value="Select Racer" />
        </form>
      </div>
    <?php
    }
    public function open_racer($id)
    {
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE)
            or die(error(3));
        
        $select_racer = "SELECT * FROM ArtecRacers WHERE RacerID=?";
        $load_info = $mysqli->prepare($select_racer);
        $load_info->bind_param('s', $id);
        $load_info->execute();
        $load_info->bind_result($this->id, $this->firstname, $this->lastname,
            $this->banner, $this->bio);
        $load_info->fetch();
        
        if($load_info)
            return true;
        else
            return false;
    }
    
    public function load_form()
    {
    ?>
        <div id="form">
      <h3>Edit Racer:</h3>
      <form method="post" action="">
        <label for="firstname">First Name: </label>
        <input type="text" id="firstname" name="firstname"
          placeholder="Racer's First Name"
          value="<?php echo $this->firstname;?>" />
        <br />
        <label for="lastname">Last Name: </label>
        <input type="text" id="lastname" name="lastname"
          placeholder="Racer's Last Name"
          value="<?php echo $this->lastname;?>" />
        <br />
        <label for="banner">Banner Location: </label>
        <input type="text" id="banner" name="banner"
          placeholder="Racer's Banner Image Location:"
          value="<?php echo $this->banner;?>" />
        <br />
        <label for="bio">Racer's Bio Info: </label>
        <textarea rows="5" cols="50" id="bio" name="bio"
          placeholder="Racer Statistics / Biography"
          value=""><?php echo $this->bio;?></textarea>
        <input type="submit" id="submit" name="newinfo" value="submit" />
      </form>
        </div>
    <?php
    }
    
    public function change_info()
    {
        var_dump($this);
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE)
            or die(error(3));
        
        $new_info = "UPDATE ArtecRacers SET First_Name=?, Last_Name=?,
            Banner_Location=?, BIO=? WHERE RacerID=?";
        $update_racer = $mysqli->prepare($new_info);
        $update_racer->bind_param('sssss', $this->firstname,
            $this->lastname, $this->banner, $this->bio, $this->id);
        $update_racer->execute();
        
        if($update_racer)
            return TRUE;
        else
            return FALSE;
        
    }
    private function error($code)
  {
    switch($code)
    {
    case 1:
      echo '<p id="error"><b>Error:</b> Please fill in all fields!</p>';
      break;
    case 2:
      echo '<p id="error"><b>Error:</b> User does not exists!</p>';
      break;
    case 3:
      echo '<p id="error"><b>Error:</b> Could not connect to MySQLi: ' . mysqli_error() . '</p>';
      break;
    }
    }
}
// Make $start a class racer. //
$start = new racer();
// Begin the program. //
if(!isset($_POST['editRacer']))
    $start->list_racers();
else
{
    if($start->open_racer($_POST['editRacer']))
    {
        $start->load_form();
        var_dump($start);
        
        if(isset($_POST['newinfo']))
        {
            if($start->change_info())
                echo "<p>Racer successfully updated!</p>";
            else
                echo "<p>Could not update racer!</p>";
        }
    }
}
?>

After re-programming the layout, I am finding that I cannot call the function at all.

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

So i have been trying to expand my knowledge with PHP, and recently have taken on a project for a friend to build a website. In the process I ran into an issue, and I am not sure what is happening. Here is the relevant code:

Re-Programmed:

<link rel="stylesheet" type="text/css" href="../themes/addTheme.css">
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once('dbinfo.php');
include_once('authenticate.php');
class racer
{
    private $id, $firstname, $lastname,
    $banner, $bio;
    public function list_racers()
  {
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE);
    ?>
      <div id="form">
        <h3>Select Racer: </h3>
        <form method="post" action="">
        <?php
          $racer_list = "SELECT * FROM ArtecRacers";
          $get_racers = $mysqli->query($racer_list);
          while ($list = $get_racers->fetch_array(MYSQLI_NUM))
          {
            echo '<input id="part" type="radio" name="editRacer" value="' . $list[0] . '"/>';
            echo '<label for"part">' . $list[1] . ' ' . $list[2] . '</label><br />';
          }
        ?>
          <input type="submit" name="selectRacer" id="submit" value="Select Racer" />
        </form>
      </div>
    <?php
    }
    public function open_racer($id)
    {
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE)
            or die(error(3));
        
        $select_racer = "SELECT * FROM ArtecRacers WHERE RacerID=?";
        $load_info = $mysqli->prepare($select_racer);
        $load_info->bind_param('s', $id);
        $load_info->execute();
        $load_info->bind_result($this->id, $this->firstname, $this->lastname,
            $this->banner, $this->bio);
        $load_info->fetch();
        
        if($load_info)
            return true;
        else
            return false;
    }
    
    public function load_form()
    {
    ?>
        <div id="form">
      <h3>Edit Racer:</h3>
      <form method="post" action="">
        <label for="firstname">First Name: </label>
        <input type="text" id="firstname" name="firstname"
          placeholder="Racer's First Name"
          value="<?php echo $this->firstname;?>" />
        <br />
        <label for="lastname">Last Name: </label>
        <input type="text" id="lastname" name="lastname"
          placeholder="Racer's Last Name"
          value="<?php echo $this->lastname;?>" />
        <br />
        <label for="banner">Banner Location: </label>
        <input type="text" id="banner" name="banner"
          placeholder="Racer's Banner Image Location:"
          value="<?php echo $this->banner;?>" />
        <br />
        <label for="bio">Racer's Bio Info: </label>
        <textarea rows="5" cols="50" id="bio" name="bio"
          placeholder="Racer Statistics / Biography"
          value=""><?php echo $this->bio;?></textarea>
        <input type="submit" id="submit" name="newinfo" value="submit" />
      </form>
        </div>
    <?php
    }
    
    public function change_info()
    {
        var_dump($this);
        $mysqli = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASS, MYSQLI_DATABASE)
            or die(error(3));
        
        $new_info = "UPDATE ArtecRacers SET First_Name=?, Last_Name=?,
            Banner_Location=?, BIO=? WHERE RacerID=?";
        $update_racer = $mysqli->prepare($new_info);
        $update_racer->bind_param('sssss', $this->firstname,
            $this->lastname, $this->banner, $this->bio, $this->id);
        $update_racer->execute();
        
        if($update_racer)
            return TRUE;
        else
            return FALSE;
        
    }
    private function error($code)
  {
    switch($code)
    {
    case 1:
      echo '<p id="error"><b>Error:</b> Please fill in all fields!</p>';
      break;
    case 2:
      echo '<p id="error"><b>Error:</b> User does not exists!</p>';
      break;
    case 3:
      echo '<p id="error"><b>Error:</b> Could not connect to MySQLi: ' . mysqli_error() . '</p>';
      break;
    }
    }
}
// Make $start a class racer. //
$start = new racer();
// Begin the program. //
if(!isset($_POST['editRacer']))
    $start->list_racers();
else
{
    if($start->open_racer($_POST['editRacer']))
    {
        $start->load_form();
        var_dump($start);
        
        if(isset($_POST['newinfo']))
        {
            if($start->change_info())
                echo "<p>Racer successfully updated!</p>";
            else
                echo "<p>Could not update racer!</p>";
        }
    }
}
?>

After re-programming the layout, I am finding that I cannot call the function at all.

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 145

TheSilentDrifter wrote:
I noticed that when apply_changes() is executed in the following class, it does not retain the variables that have been set. Can anyone help me with this?

When you say that it doesn't retain the variables that have been set, I'm guessing the firstname variable is empty in the line it echos?

If that's the case, was it also empty in send_form()? Have you tried putting some var_dump's in various places in your class to see what the value of $_POST['editRacer'] is, and the value of the various instance variables at different points.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 145

TheSilentDrifter wrote:
I noticed that when apply_changes() is executed in the following class, it does not retain the variables that have been set. Can anyone help me with this?

When you say that it doesn't retain the variables that have been set, I'm guessing the firstname variable is empty in the line it echos?

If that's the case, was it also empty in send_form()? Have you tried putting some var_dump's in various places in your class to see what the value of $_POST['editRacer'] is, and the value of the various instance variables at different points.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

I the form fills out like a charm, and that is why I am not sure what is going on. Var Dumps all over the code show up with the appropriate values, except in in that function...

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

I the form fills out like a charm, and that is why I am not sure what is going on. Var Dumps all over the code show up with the appropriate values, except in in that function...

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 145

TheSilentDrifter wrote:
I the form fills out like a charm, and that is why I am not sure what is going on.sad

I just tested the code (with a mocked database part) and it worked fine for me. I would suggest putting var_dump($this->firstname) in get_racer() before and after the send_form() function gets called, and try var dumping it in apply_changes(). Are you sure that the firstname field was set in the form?

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 145

TheSilentDrifter wrote:
I the form fills out like a charm, and that is why I am not sure what is going on.sad

I just tested the code (with a mocked database part) and it worked fine for me. I would suggest putting var_dump($this->firstname) in get_racer() before and after the send_form() function gets called, and try var dumping it in apply_changes(). Are you sure that the firstname field was set in the form?

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

Here is what I am looking at, as well as a var_dump of the Class.
i169.photobucket.com/albums/u209/eowdaemon/form.png

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg


TheSilentDrifter
Member
Registered: 29.12.11 18:02
Timezone: UTC -7
Posts: 19

Here is what I am looking at, as well as a var_dump of the Class.
i169.photobucket.com/albums/u209/eowdaemon/form.png

i169.photobucket.com/albums/u209/eowdaemon/TheSilentDrifter-1.jpg