Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1013 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

OOP with forms

Thread title: OOP with forms
Closed Thread    
    Thread tools Search this thread Display Modes  
08-26-2007, 11:13 AM
#1
Haris is offline Haris
Status: Request a custom title
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 2,741
iTrader: 9 / 100%
 

Haris is on a distinguished road

  Old  OOP with forms

HTML Code:
<form action="tree.php" method="post" accept-charset="utf-8">
    <select name="type">
        <option selected="selected" disabled="disabled">Select Type</option>
        <option value="Orange">Orange</option>
        <option value="Apple">Apple</option>
        <option value="Mango">Mango</option>
    </select>
    <p><input type="submit" value="Continue &rarr;" name="treetype"></p>
</form>

Tree.php
PHP Code:
<form action="" method="post" accept-charset="utf-8">
    <select name="command">
        <option selected="selected" disabled="disabled">Commands</option>
        <option value="oneYearPasses">Grow one year</option>
        <option value="count_fruits">Count the fruits</option>
    </select>
    <p><input type="submit" value="Continue &rarr;" name="commands"></p>
</form>

<?php
    
include('class_tree.php'); ## includes the class

$Tree = new Tree;
$TreeType $_POST['type'];

if(isset(
$_POST['treetype'])){
echo 
$Tree->setType($TreeType);
}

if(
$_POST['command'] == 'oneYearPasses'){
    echo 
$Tree->oneYearPasses();
}    
elseif(
$_POST['command'] == 'count_fruits'){
    echo 
$Tree->count_fruits();
}

?>
class_tree.php
PHP Code:
<?php

class Tree{

    private 
$Type;
    private 
$Height 1;
    private 
$Fruit 0;
    
    
# Sets the type of Tree
    
public function setType($TheType){
        
$this->Type $TheType;
        
$return_val '<b>'.$TheType.' tree has been cultivated.</b><br/>';
        return 
$return_val;
    }
    
    
# When oneYearPasses is called, the function adds 1 meter to the height.
    # When the height reaches 10, it grows 10 fruits.
    # When the tree grows to 10 meter, every year it grows 5 fruits.
    # When the tree reaches height of 15 meters, it dies.
    
public function oneYearPasses(){
        
$TheHeight $this->Height++;
        
$return_val 'The '.$this->Type.' Tree grows to '.$TheHeight.' meter <br/>';
        
        if(
$TheHeight == 10){
            
$return_val 'The '.$this->Type.' Tree grows to '.$TheHeight.' meter <br/>';
            
$return_val .= '<b>Your Tree is now mature and grows 10 fruits</b><br/>';
            
$this->Fruit 10;
        }
        
        if(
$TheHeight 10){
            
$return_val 'The '.$this->Type.' Tree grows to '.$TheHeight.' meter <br/>';
            
$return_val .= '<b>The Tree grows more 5 fruits</b><br/>';
            
$this->Fruit += 5;
        }
        
        if(
$TheHeight == 15){
            
$return_val '<b>Sorry but your tree died</b><br/>';
            
$this->Fruit 0;
        }
        
        return 
$return_val;
    }
    
    
# Counts the number of $this->Fruit present in $this->Tree
    
public function count_fruits(){
        
$return_val 'The '.$this->Type.' Tree has grown '.$this->Fruit.' Fruits';
        return 
$return_val;
    }
    
}

?>
The oneYearPasses function doesn't take the setType into the account and just echo out 'Tree grows to 1 meter'. How would I fix it?
PHP Code:
if($_POST['command'] == 'oneYearPasses'){
    echo 
$Tree->oneYearPasses();
}    
elseif(
$_POST['command'] == 'count_fruits'){
    echo 
$Tree->count_fruits();

And for those who prefer clean code, please check my code and let me know what can I do to improve my code.

08-26-2007, 11:30 AM
#2
Garrett is offline Garrett
Status: Waving
Join date: Aug 2005
Location:
Expertise:
Software:
 
Posts: 2,694
iTrader: 11 / 100%
 

Garrett is on a distinguished road

Send a message via MSN to Garrett

  Old

Based on the <form> output on the first code box, are you using TextMate, or that "E" editor on Widnows?

hehe

08-26-2007, 11:57 AM
#3
Haris is offline Haris
Status: Request a custom title
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 2,741
iTrader: 9 / 100%
 

Haris is on a distinguished road

  Old

Originally Posted by Garrett View Post
Based on the <form> output on the first code box, are you using TextMate, or that "E" editor on Widnows?

hehe
TextMate wannabe e*

08-26-2007, 01:47 PM
#4
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

That is because it doesn't automatically pass type. Put this code in the form
PHP Code:
<php= <input type="hidden" name="type" value="$_POST['treetype']"?> 

08-26-2007, 02:06 PM
#5
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

I can't see anywhere where the current Tree's data is persisted across page loads. Because of that you'll always reset to the initial tree data. Use the session, or throw in hidden form data, to keep track of the current status of the Tree.


As for the 'clean code police', here's one little tidbit:
PHP Code:
/*
 |----------
 | Haris' Version
 |----------
*/
if(isset($_POST['treetype'])){
echo 
$Tree->setType($TreeType);
}

if(
$_POST['command'] == 'oneYearPasses'){
    echo 
$Tree->oneYearPasses();
}    
elseif(
$_POST['command'] == 'count_fruits'){
    echo 
$Tree->count_fruits();
}

/*
 |----------
 | Cleaner?.. You decide.
 | It's more about the switch than anything else.
 |----------
*/
if (isset($_POST['type'])) 
{
    echo 
$Tree->setType($TreeType)
}

if (isset(
$_POST['command']))
{
    switch(
$_POST['command'])
    {
        case 
'oneYearPasses' :
            echo 
$Tree->oneYearPasses();
            break;
        case 
'count_fruits' :
            echo 
$Tree->count_fruits();
            break;
    }

08-26-2007, 03:46 PM
#6
Haris is offline Haris
Status: Request a custom title
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 2,741
iTrader: 9 / 100%
 

Haris is on a distinguished road

  Old

Alright, thanks. I got it working.

How do you use HTML in PHP when not working with MVC?

Do you prefer?

PHP Code:
echo "<input type=\"text\">"
or do you prefer

PHP Code:
<?php

echo "php code here";
?>
#HTML code here
<?php
echo "php code here";
?>

08-26-2007, 06:19 PM
#7
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Whichever is most convenient and easy to read.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed