Jump to content

This is making my head hurt...


phatgreenbuds

Recommended Posts

I am going to bed for the night frustrated...I am trying to creating a decision-based branching tree.  Based on what you select the first round will determine what is presented in the second round.  And all this gets put into an array.  This is driving me nuts trying to make sense of it...Maybe I am just tired but here is what I have so far. If anyone out there has a better idea of how to do this I am all about hearing it.

<?php
if (isset($_POST['s2'])){
$arr = $_POST['s2'];
echo $_POST['s1'] . "-" . $arr[0];
} 
else {
  if (isset($_POST['s1'])){
if ($_POST['s1'] == "Red"){
	echo $_POST['s1'];
} else if ($_POST['s1'] == "Blue"){
	echo $_POST['s1'];
} else if ($_POST['s1'] == "Black"){
	echo $_POST['s1'];
} else if ($_POST['s1'] == "Yellow"){
	echo $_POST['s1'];
} else if ($_POST['s1'] == "Green"){ 
	echo $_POST['s1'];
	  ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="checkbox" name="s2[]" value="Red2" /> Red2 <br />
	  <input type="checkbox" name="s2[]" value="Blue2" /> Blue2 <br />
	  <input type="checkbox" name="s2[]" value="Black2" /> Black2 <br />
	  <input type="checkbox" name="s2[]" value="Yellow2" /> Yellow2 <br />        
	  <input type="checkbox" name="s2[]" value="Green2" /> Green2 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="s1" type="hidden" value="<?php echo $_POST['s1']; ?>" />
	  </form>
	  <?php
} 
else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s1" value="Red" /> Red <br />
	  <input type="radio" name="s1" value="Blue" /> Blue <br />
	  <input type="radio" name="s1" value="Black" /> Black <br />
	  <input type="radio" name="s1" value="Yellow" /> Yellow <br />        
	  <input type="radio" name="s1" value="Green" /> Green <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
	  </form>
	  <?php 
	}
} 
else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s1" value="Red" /> Red <br />
	  <input type="radio" name="s1" value="Blue" /> Blue <br />
	  <input type="radio" name="s1" value="Black" /> Black <br />
	  <input type="radio" name="s1" value="Yellow" /> Yellow <br />        
	  <input type="radio" name="s1" value="Green" /> Green <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
	  </form>
	  <?php 
	} 
}
  ?>

Link to comment
Share on other sites

IMO you would be best suited using a Chain of Responsibility as a decision may return multiple times in an application while the logic may remain the same. I also added some extra's like _getSession() and _getBreadCrumbs() for usage in your application.

 

<?php

abstract class DecisionTree {
    protected $_successor = null;
    protected $_session = null;
    
    public function __construct(DecisionTree $dt = null) {
        $this->_successor = $dt;
    }
    
    protected function _getSession() {
        if (null === $this->_session) {
            $this->_session = new ArrayObject(&$_SESSION,
                ArrayObject::ARRAY_AS_PROPS); // Works in PHP<5.3
        }
        return $this->_session;
    }
    
    protected function _getBreadCrumbs() {
        $session = $this->_getSession();
        if (!$session->offsetExists('breadCrumbs')) {
            $session->breadCrumbs = new ArrayObject(array(),
                ArrayObject::ARRAY_AS_PROPS);
        }
        return $session->breadCrumbs;
    }
    
    protected function _gameOver() { echo 'You lost.'; }
    
    protected function _handleRed() { echo 'You picked red.'; }
    protected function _handleBlue() { echo 'You picked blue.'; }
    protected function _handleOrange() { echo 'You picked orange.'; }
    protected function _handleYellow() { echo 'You picked yellow.'; }
    
    protected function _handleDecision($expected, $decision) {
        if ($expected !== $decision) {
            null === $this->_successor
                ? $this->_gameOver()
                : $this->_successor->handleDecision($decision);
            return false;
        }
        return true;
    }
    
    abstract public function handleDecision($decision);
}

///////////////////////////////////////////////////////////////////////////////
// CONCRETE CLASSES
///////////////////////////////////////////////////////////////////////////////

class Red extends DecisionTree {
    public function handleDecision($decision) {
        if ($this->_handleDecision('Red', $decision)) {
            $this->_handleRed();
        }
    }
}

class Blue extends DecisionTree {
    public function handleDecision($decision) {
        if ($this->_handleDecision('Blue', $decision)) {
            $this->_handleBlue();
        }
    }
}

class Orange extends DecisionTree {
    public function handleDecision($decision) {
        if ($this->_handleDecision('Orange', $decision)) {
            $this->_handleOrange();
        }
    }
}

class Yellow extends DecisionTree {
    public function handleDecision($decision) {
        if ($this->_handleDecision('Yellow', $decision)) {
            $this->_handleYellow();
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// LOGIC
///////////////////////////////////////////////////////////////////////////////

$l = new Red(new Blue(new Yellow(new Orange())));

print 'Act One: Orange<br />';
$l->handleDecision('Orange');
print '<br />';

print 'Act Two: Blue</br />';
$l->handleDecision('Blue');
print '<br />';

print 'Act Three: Lost!<br />';
$l->handleDecision('Purple');
print '<br />';

 

Outputs:

Act Orange
You picked orange.
Act Blue
You picked blue.
Act Losing
You lost.

 

It always issues a Deprecated warning if you are using PHP 5.3.0 due to the &$_SESSION

 

Link to comment
Share on other sites

wow ignace, that is soooo far over my head. It will take a while to understand how it works. :)

 

The reason I went with arrays is that the ultimate goal is to make a searchable page that will dig through the array holding key words assoctiated with a picture...and if any of the selections are found it will return that picture.

 

So yeah i will be using implode and all the good stuff...just writing out the decision making tree can be pretty easy to get yourself lost in.

Link to comment
Share on other sites

Ok well I got it sorta working but as you can see below as this branches out it could quickly turn into a huge mess...and I plan to have several branches so I do mean a mess. Looks like I have to try something else perhaps the post above from ignace.

 

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<?php
if (isset($_POST['s1'])){
$arr[0] = $_POST['s1'];
$i = $_POST['s1'];
switch ($i) {
    case "Red1":
        echo $arr[0];
        break;
    case "Blue1":
        echo $arr[0];
        break;
    case "Green1":
        echo $arr[0];
        break;
	}	

} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s1" value="Red1" /> Red1 <br />
	  <input type="radio" name="s1" value="Blue1" /> Blue1 <br />
	  <input type="radio" name="s1" value="Green1" /> Green1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
	  </form>
<?php } ?>
</td>
  </tr>
  <tr>
    <td>
<?php
if ($arr[0] == "Red1") {
if (isset($_POST['s2'])){
		$arr[1] = $_POST['s2'];
		$j = $_POST['s2'];
		switch ($j) {
		case "Red2Red1":
			echo $arr[1];
			break;
		case "Blue2Red1":
			echo $arr[1];
			break;
		case "Green2Red1":
			echo $arr[1];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s2" value="Red2Red1" /> Red2Red1 <br />
	  <input type="radio" name="s2" value="Blue2Red1" /> Blue2Red1 <br />
	  <input type="radio" name="s2" value="Green2Red1" /> Green2Red1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
	  </form>
<?php } 
} else if ($arr[0] == "Blue1") {
if (isset($_POST['s2'])){
		$arr[1] = $_POST['s2'];
		$j = $_POST['s2'];
		switch ($j) {
		case "Red2Blue1":
			echo $arr[1];
			break;
		case "Blue2Blue1":
			echo $arr[1];
			break;
		case "Green2Blue1":
			echo $arr[1];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s2" value="Red2Blue1" /> Red2Blue1 <br />
	  <input type="radio" name="s2" value="Blue2Blue1" /> Blue2Blue1 <br />
	  <input type="radio" name="s2" value="Green2Blue1" /> Green2Blue1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
	  </form>
<?php } 
} else if ($arr[0] == "Green1") {
if (isset($_POST['s2'])){
		$arr[1] = $_POST['s2'];
		$j = $_POST['s2'];
		switch ($j) {
		case "Red2Green1":
			echo $arr[1];
			break;
		case "Blue2Green1":
			echo $arr[1];
			break;
		case "Green2Green1":
			echo $arr[1];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s2" value="Red2Green1" /> Red2Green1 <br />
	  <input type="radio" name="s2" value="Blue2Green1" /> Blue2Green1 <br />
	  <input type="radio" name="s2" value="Green2Green1" /> Green2Green1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
	  </form>
<?php } 
}
?>
    </td>
  </tr>
  <tr>
  	<td>
<?php
if ($arr[1] == "Red2Red1") {
if (isset($_POST['s3'])){
		$arr[2] = $_POST['s3'];
		$k = $_POST['s3'];
		switch ($k) {
		case "Red3Red2Red1":
			echo $arr[2];
			break;
		case "Blue3Red2Red1":
			echo $arr[2];
			break;
		case "Green3Red2Red1":
			echo $arr[2];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s3" value="Red3Red2Red1" /> Red3Red2Red1 <br />
	  <input type="radio" name="s3" value="Blue3Red2Red1" /> Blue3Red2Red1 <br />
	  <input type="radio" name="s3" value="Green3Red2Red1" /> Green3Red2Red1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
          <input name="s2" type="hidden" value="<?php echo $arr[1]; ?>" />
	  </form>
<?php } 
} else if ($arr[1] == "Blue2Red1") {
if (isset($_POST['s3'])){
		$arr[2] = $_POST['s3'];
		$k = $_POST['s3'];
		switch ($k) {
		case "Red3Blue2Red1":
			echo $arr[2];
			break;
		case "Blue3Blue2Red1":
			echo $arr[2];
			break;
		case "Green3Blue2Red1":
			echo $arr[2];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s3" value="Red3Blue2Red1" /> Red3Blue2Red1 <br />
	  <input type="radio" name="s3" value="Blue3Blue2Red1" /> Blue3Blue2Red1 <br />
	  <input type="radio" name="s3" value="Green3Blue2Red1" /> Green3Blue2Red1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
          <input name="s2" type="hidden" value="<?php echo $arr[1]; ?>" />
	  </form>
<?php } 
} else if ($arr[1] == "Green2Red1") {
if (isset($_POST['s3'])){
		$arr[2] = $_POST['s3'];
		$k = $_POST['s3'];
		switch ($k) {
		case "Red3Green2Red1":
			echo $arr[2];
			break;
		case "Blue3Green2Red1":
			echo $arr[2];
			break;
		case "Green3Green2Red1":
			echo $arr[2];
			break;
			}
} else { ?>
	  <form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="profEdit">
	  <input type="radio" name="s3" value="Red3Green2Red1" /> Red3Green2Red1 <br />
	  <input type="radio" name="s3" value="Blue3Green2Red1" /> Blue3Green2Red1 <br />
	  <input type="radio" name="s3" value="Green3Green2Red1" /> Green3Green2Red1 <br /><br />
	  <input name="submit" type="submit" value="submit" />
	  <input name="ident" type="hidden" value="3" />
          <input name="s1" type="hidden" value="<?php echo $arr[0]; ?>" />
          <input name="s2" type="hidden" value="<?php echo $arr[1]; ?>" />
	  </form>
<?php } 
} 
?>
    </td>
  </tr>
  <tr>
  	<td> 
    <?php print_r ($arr); ?>
    </td>
  </tr>
</table>

Link to comment
Share on other sites

Looks like I have to try something else perhaps the post above from ignace.

 

It isn't actually so hard to implement. You have one class for each decision (Red, Blue, Yellow). This works only if the logic behind each decision remains the same if the logic differs then my solution can become your problem and I would advise not to use it.

 

Because the logic for each decision handler varied in only a few parts of it's code have I created a general method in the base class

 

protected function _handleDecision($expected, $decision) {
    if ($expected !== $decision) {
        null === $this->_successor
            ? $this->_gameOver()
            : $this->_successor->handleDecision($decision);
        return false;
    }
    return true;
}

 

By adding a new variable $expected I could now pass my color (eg 'Red') and by returning a boolean value (wether or not my class could handle it) If it returned true the current class would be the one responsible and thus it could execute it's logic.

 

if ($this->_handleDecision('Red', $decision)) {
    $this->_handleRed();
}

 

Ofcourse was this not my original code but through code refactoring were I able to create a nice clean solution.

 

My code is not without flaw though but I'll blame that to the late hour

 

protected function _handleRed() { echo 'You picked red.'; }
protected function _handleBlue() { echo 'You picked blue.'; }
protected function _handleOrange() { echo 'You picked orange.'; }
protected function _handleYellow() { echo 'You picked yellow.'; }

 

This code should have been in his respective class instead of the base class.

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.