Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Stumped OOP php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Stumped OOP php

Postby C0D3Y » Tue Nov 27, 2012 9:54 am

Okay so I am creating a simple blog from scratch, with mysql "phpmyadmin" godaddy supplies it, php in dreamweaver, and css and html in dreamweaver also. Just for practice, no real reason. I have a new_user.php file that I can't fix to use my objects. I know how to create a class and its properties, an object essentially.., and instantiate it. But I don't know how to do this in the real world and actually use it for two objects, BlogUser and BlogEntry, I don't even have the blog on the website yet but i do have it to where I cant create new staff users to use the cms i built to edit the website. It's such a simple problem but I don't see any help on it anywhere. I have a bunch more files that connect to this but I cant attach them and it would be impossible to read if i just pasted them all here.
new_user.php
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php
include_once("includes/form_functions.php");

// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();

// perform validations on the form data
$required_fields = array('username', 'password');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));

$fields_with_lengths = array('username' => 30, 'password' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

$username = trim(mysql_prep($_POST['username']));
$password = trim(mysql_prep($_POST['password']));
$hashed_password = sha1($password);

if ( empty($errors) ) {
$query = "INSERT INTO users (
username, hashed_password
) VALUES (
'{$username}', '{$hashed_password}'
)";
$result = mysql_query($query, $connection);
if ($result) {
$message = "The user was successfully created.";
} else {
$message = "The user could not be created.";
$message .= "<br />" . mysql_error();
}
} else {
if (count($errors) == 1) {
$message = "There was 1 error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
} else { // Form has not been submitted.
$username = "";
$password = "";
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<a href="staff.php">Return to Menu</a><br />
<br />
</td>
<td id="page">
<h2>Create New User</h2>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
<form action="new_user.php" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Create user" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>

and this is what I think my include.inc file will look like
class BlogUser {
protected $User_name;
protected $User_email;
protected $User_password;
protected $User_signup_date;
}
class BlogEntry {
public $Blog_title;
public $Blog_category;
protected $Blog_body;
protected $Blog_draft_date;
protected $Blog_modify_date;
}
But I don't know where I'm supposed to declare their behaviors or properties. in the same file I assume?
any help on the matter would be greatly appreciated. oop with php is a nightmare if you are a noob who solely learned php and sql without the oo portion.
C0D3Y
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Nov 27, 2012 9:39 am

Re: Stumped OOP php

Postby seandisanti » Tue Nov 27, 2012 11:41 am

Sounds like you've kind of bitten off a little more than you can currently chew on this one, but don't fret you can get up to speed pretty quickly. There are several ways to approach your particular situation, I recommend going to http://jream.com/learning/videos/php-oop for a quick crash course. Free video lessons there for php oop.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 773
Joined: Mon Oct 01, 2012 12:32 pm

Re: Stumped OOP php

Postby simplypixie » Wed Nov 28, 2012 11:55 pm

I know your struggle as I had the same problems when starting out years ago - no 'real world' examples on how to implement what you are learning.

To keep things simple, this is what you need, very simply.

Code: Select all
// You need to extend your connection class (if it is a class) so that you can connect to the db
class BlogUser extends Connection {
protected $User_name;
protected $User_email;
protected $User_password;
protected $User_signup_date;

function __construct() {
  // This initiates your class and you can put variables or call functions in here that always need running when your class is instantiated (if any, or you can leave blank)
// For instance connect to the database (I would need to see your connection class to help with this)
}

function add_user() {
  $query = "INSERT INTO users (username, hashed_password) VALUES ('".$this->User_name."', '".$this->User_password."')";
  $result = mysql_query($query, $connection);
  if ($result) {
    $this->message = "The user was successfully created.";
  } else {
    $this->message = "The user could not be created.";
    $this->message .= "<br />" . mysql_error();
  }
  return $this->message;
}


In your page:
Code: Select all
if ( empty($errors) ) {
$password = trim(mysql_prep($_POST['password']));

$user = new BlogUser; 
$user->User_name = trim(mysql_prep($_POST['username']));
$user->User_password = sha1($password);

$user->add_user();

echo $user->message;
}


I hope this gives you a better idea and you can apply it to the other code/classes.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron