Jump to content

Still Need Help (been working-->Greeting Tree)


isuckat_php

Recommended Posts

I made some changes to the code. Hopefully it helps in my much need assistance.

Help is greatly appreciated. I am working on a code that needs to have the capabilities as this link:

http://athena.ecs.csus.edu/~cs010142/Greeting_tree.php

 

Exclude the background color and the blinking. I think there is away to set up arrays to make it do this but having difficulty. This is my code so far:

 

<HTML>

<HEAD>

<TITLE>The Greeting Trees </TITLE>

</HEAD>

<BODY bgcolor=" #3BB9FF">

 

<?

 

/*

Your brief description of this program which may include input, output, requirements, and your design highlights

*/

 

if ($submit)

{

$size=$_POST['size'];

$tree=$_POST['tree'];

$type=$_POST['type'];

 

if (($size == "")  || (!$tree))

{

  echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot to supply some information. </b></font><br>");

    }

    else if ((!ereg("[0-9]",$size)))

{

    echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict

    your input to only numbers.</b></font><br>");

}

else if (($size < "12")|| ($size > "24"))

{

echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Out of range</b></font><br>");

}

}

else

{

$A =array($size);

if (($size % 2 == 0))

 

$center =(($size/2)) && (($size/2 + 1));

 

else

 

$center = int($size/2);

 

for ($i=0; $i < 5; $i++)

{

  for($j= ($center - $i); $j<= ($center + $i); $j++)

  {

    $A[$i+2][$j]= "*";

  }

}

 

 

for ($i=0; $i< $N; $i++)

{

 

  for($j=0; $j< $N; $j++)

{

  DisplayChar ($type, $A[$i][$j]);

  }

 

echo "<BR>";

}

//Form to collect user input: one of the 3  greeting tree types

?>

<FORM METHOD="post" ACTION="Greeting_tree.php">

<p>Please enter a tree size between 12 and 24:</p>

<input type="text" name="size" />

<p>Select tree type:</p>

<input type="radio" name="tree" value="Holiday"  /> Holiday

<input type="radio" name="tree" value="SacState" /> SacState

<input type="radio" name="tree" value="Suprise" /> Suprise

<br/>

<input type="submit" value="Display the tree" >

</FORM>

</BODY>

</HTML>

 

ok

I checked the code and here are my thoughts:

 

1- to check if the form is posted you have to check for a value that is really posted so the first line should look like:

if($_POST['submit'])

 

2- Even if I use the previous line to check for form submission I will not get any results since the submit button does not have a name attribute in your form so you should change it to.

<input type="submit" name="submit" value="Display the tree" >

 

3- You have to check the if/else statments in your code they seem to be messed up.

 

4- you can use extract function to get the values of $_POST. (See the code below)

 

5- some notes that would not affect the code currently but for future refrences (once you update to php 5.3.0 or 6.0.0):

- do not use short tags like <?, instead use <?php at the beginning of of the file.

- using the end tag is ?> is optional.

- erge-related functions  are being deprecated as of php 5.3.0

 

That is what I got so far and felt important that you know to improve your code even if it is a small-scale code.

 

and here is the modfied code (i only check the posting format) so you can improve on it:

 

<HTML>
<HEAD>
<TITLE>The Greeting Trees </TITLE>
</HEAD>
<BODY bgcolor="   #3BB9FF">

<?php

/*
Your brief description of this program which may include input, output, requirements, and your design highlights
*/
if ($_POST[submit]) {

extract($_POST);
    
    if (($size == "") || (!$tree)) {
        echo ("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot to supply some information. </b></font><br>");
    } else if ((!ereg("[0-9]", $size))) {
        echo ("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict
       your input to only numbers.</b></font><br>");
    } else if (($size < "12") || ($size > "24")) {
        echo ("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Out of range</b></font><br>");
    }
    
    else {
        $A = array(
            $size
        );
        if (($size % 2 == 0))
            $center = (($size / 2)) && (($size / 2 + 1));
        
        else
            $center = int($size / 2);
        
        for ($i = 0; $i < 5; $i++) {
            for ($j = ($center - $i); $j <= ($center + $i); $j++) {
                $A[$i + 2][$j] = "*";
            }
        }
        
        
        for ($i = 0; $i < $N; $i++) {
            for ($j = 0; $j < $N; $j++) {
                DisplayChar($type, $A[$i][$j]);
            }
            
            echo "<BR>";
        }
    }
} else {
    //Form to collect user input: one of the 3  greeting tree types
?>
<FORM METHOD="post" ACTION="test.php">
<p>Please enter a tree size between 12 and 24:</p>
<input type="text" name="size" />
<p>Select tree type:</p>
<input type="radio" name="tree" value="Holiday"   /> Holiday
<input type="radio" name="tree" value="SacState" /> SacState
<input type="radio" name="tree" value="Suprise" /> Suprise
<br/>
<input type="submit" name='submit' value="Display the tree" />
</FORM>
<?php
}
?>
</BODY>
</HTML>

Archived

This topic is now archived and is closed to further replies.

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