Jump to content

Simpale PHP and HTML coding


nni384

Recommended Posts

Hi guys, I just started programming and I need some help.

For my class I need to create a christmass tree generator but I really don't know how. 

 

Here is the instructions:

Input: user's choice of greeting tree type. You are required to

provide the following  three type options: Holiday, Sac State,

Surprise. You may implement them using Radio Buttons which are used

when you want the user to select one of a limited number of choices.

Refer to your on-line calculator for use of radio buttons in the

program examples.

 

Output: display the greeting tree pattern and a greeting message based

on the user's request. For example, for choice of "holiday greeting

tree", the generator display the following pattern with two major

colors: red (for 0) and green (for *).

 

00000000000

00000000000

00000*00000

0000***0000

000*****000

00*******00

0*********0

00000*00000

00000*00000

00000*00000

00000000000

Happy Holidays!

 

For choice of "Sac State", the two major colors are: gold (for 0) and

green (for *); and for choice of "surprise", the two major colors can

be any two of your favorite colors. Here is an example of greeting

tree generator. For choice of color, you may find this site helpful to

you. You can pick the color and use its corresponding hexadecimal

color code in your !

 

Extra Credit (30%): collect one more input from the user: size of the

greeting tree, an odd integer between 9 and 17 (or other range you can

display in full in one screen).  This improved one should be able to

generate the three different greeting trees in different sizes.

 

Programming Requirements

 

You may use two PHP files as you did in time-table generator: (a) an

include file contains all of the functions, called gt_functions.php;

and (b) a main program, called Greeting_tree.php. First, edit the two

files based on the outline given below and then load them to your web

space. Second, make a link to Greeting_tree.php from your home page,

index.html. Third, debugging your program -- always remember to test

each file incrementally -- make one change at a time and then check

from your home page to see if it is working. Repeat this debugging

process until the application is working.

 

Here is the coding I have so far:

Greeting_tree.php

<?php
/*
Your brief description of this program which may include input,
output, requirements, and your design highlights
*/

include_once("gt_functions.php");

# MAIN-PROGRAM
$title = "The Greeting Trees";
html_begin ($title, $title);

if ($submit)
{
if (!$type)
{
// your user input validation error message goes here to remind the
user to select a tree type
// you can reuse the input validation code in calc.php here
}

else
{
//set up the data in a 2-dimensional array to mark the background

$A =array(array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"),
                array("0","0","0","0","0","0","0","0","0","0","0"));

//this array A is 11x11, therefore the center of a row is 5
$N = 11;
$center = 5;

//mark tree leaves
for ($i=0; $i < 5; $i++)
{
  for($j= ($center - $i); $j<= ($center + $i); $j++)
{
   $A[$i+2][$j]= "*";
  }
}

//mark tree trunk
for ($i = 7; $i < 10; $i++)
{
$A[$i][$center] = "*";
}

//display elements of the array in colors of the tree type

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

for($j=0; $j< $N; $j++)
{
  DisplayChar ($type, $A[$i][$j]);
}

echo "<BR>";
}

DisplayGreeting($type);

}
}

//Form to collect user input: one of the 3  greeting tree types
?>
<FORM METHOD="post" ACTION="Greeting_tree.php">
<P><font face="Tahoma" size="2"><b>Please select a tree type: <br>
<font face="Tahoma" size="2">
<input TYPE="radio" NAME="type" >Holiday
<input TYPE="radio" NAME="type" >SacState
<input TYPE="radio" NAME="type" >Surprise
</b></font><br><br>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Display the tree"></p>
</FORM>
<? html_end () ?>

 

 

gt_functions.php

<?php
# A collection of functions that will be included in Greeting_tree.php

function html_begin ($title, $header)
{
print ("<HTML>\n");
print ("<HEAD>\n");
if ($title)
print ("<TITLE>$title</TITLE>\n");
print ("</HEAD>\n");
if ($header)
print ("<H2>$header</H2>\n");
}

function html_end ()
{
print ("</BODY></HTML>\n");
}

function DisplayChar ($type, $char)
//display leave color when $char = "*" and background color when $char = "0"
//according to the tree type

{

//find DisplayChar function body outline from  PHP notes in SacCT and
put the completed version here

$colors = array('red', 'gold');
if ($type === 'holiday greeting tree') $colors = array('red', 'green');
if ($type === 'Sac State') $colors = array('gold', 'green');
//if ($type === 'surprise')
if ($char != 0 && $char != '*') return $char;
if ($char == 0) return '<span style="color:'.$colors[0].'">0</span>';
if ($char == '*') return '<span style="color:'.$colors[1].'">*</span>';
}

function DisplayGreeting ($type)

//display greeting of the given tree type

{

//find DisplayGreeting function body from  PHP notes in SacCT and put it here
If($type=="Holiday")
{
$message="Happy Holidays!!!";
}
else if($type=="SacState")
{
$message="Go Sac Hornets!!!";
}
else if($type=="Surprise")
{
$message="Cake is a lie!!";
}
echo("<font face=\"Tahoma\"size=\"4\"color=\"#17e0e3\"><b>$message</b></font><br>");
}

?>

Link to comment
Share on other sites

So I worked on the functions a little more and this is what I got:

<?php
# A collection of functions that will be included in Greeting_tree.php

function html_begin ($title, $header)
{
print ("<HTML>\n");
print ("<HEAD>\n");
if ($title)
print ("<TITLE>$title</TITLE>\n");
print ("</HEAD>\n");
if ($header)
print ("<H2>$header</H2>\n");
}

function html_end ()
{
print ("</BODY></HTML>\n");
}

function GenerateTree($size) {
$tree = "";
if ($size < 9 || $size > 17) $size = 9;

for ($x = 0; $x < $size; $x++) {
   $spacer = str_repeat("0", ($size - $x));
   $stars  = str_repeat("*", ($x*2)+1);
   $tree  .= $spacer . $stars . $spacer . "\n";
}

$spacer = str_repeat("0", ceil($size/2)-1);
$tree  .= $spacer . "*" . $spacer . "\n";
$tree  .= $spacer . "*" . $spacer . "\n";
$tree  .= $spacer . "*" . $spacer . "\n";
return $tree;
}

function DisplayChar ($type, $char)
//display leave color when $char = "*" and background color when $char = "0"
//according to the tree type

{
$colors = array("red", "gold");
if ($type === "Holiday") $colors = array("red", "green");
if ($type === "SacState") $colors = array("gold", "green");
//if ($type === "surprise")
if ($char != 0 && $char != "*") return $char;
if ($char == 0) return '<span style="color:'.$colors[0].'">0</span>';
if ($char == '*') return '<span style="color:'.$colors[1].'">*</span>';
}

//find DisplayChar function body outline from  PHP notes in SacCT and
put the completed version here

if($type=="Holiday")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}

else if($type=="SacState")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}

else if($type=="Surprise")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}




//display greeting of the given tree type

function DisplayGreeting($type, $size = 9) {
$tree = GenerateTree($size);
$coloredTree = "";
foreach (str_split($tree) as $char) {
   $coloredTree .= DisplayChar($type, $char);
}
return $coloredTree;
}

//find DisplayGreeting function body from  PHP notes in SacCT and put it here
{
If($type=="Holiday")
{
$message="Happy Holidays!!!";
}
else if($type=="SacState")
{
$message="Go Sac Hornets!!!";
}
else if($type=="Surprise")
{
$message="Cake is a lie!!";
}
echo("<font face=\"Tahoma\"size=\"4\"color=\"#17e0e3\"><b>$message</b></font><br>");

}

?>

 

But now I am getting a fatal error: Fatal error: Call to undefined function: str_split() in

/gaia/class/cs0104/cs010415/html/gt_functions.php on line 85

 

This is my first time using the str_split and I think I missused it,  anyone know how I can fix it?

Link to comment
Share on other sites

The str_split function is only available in PHP 5 so I'd guess that you're using PHP 4.x (hence the "undefined function" error).

 

If you check the manual page, one of the users has left a comment with a PHP 4 implementation that might work for you.

 

Thanks, so I ended up with this code:

<?php
# A collection of functions that will be included in Greeting_tree.php

function html_begin ($title, $header)
{
print ("<HTML>\n");
print ("<HEAD>\n");
if ($title)
print ("<TITLE>$title</TITLE>\n");
print ("</HEAD>\n");
if ($header)
print ("<H2>$header</H2>\n");
}

function html_end ()
{
print ("</BODY></HTML>\n");
}

{
function str_split($string, $length = 1)
{
    $return = array();
    $count = 0;
        
    for($i = 0; $i < strlen($string); $i++)
    {
        if($i % $length == 0)
        {
            $count++;
            continue;
        }
            
        $return[$count] .= $string{$i};
    }
        
    return $return;
}
}


function GenerateTree($size) {
  $tree = "";
  if ($size < 9 || $size > 17) $size = 9;

  for ($x = 0; $x < $size; $x++) {
    $spacer = str_repeat("0", ($size - $x));
    $stars  = str_repeat("*", ($x*2)+1);
    $tree  .= $spacer . $stars . $spacer . "\n";
  }

  $spacer = str_repeat("0", ceil($size/2)-1);
  $tree  .= $spacer . "*" . $spacer . "\n";
  $tree  .= $spacer . "*" . $spacer . "\n";
  $tree  .= $spacer . "*" . $spacer . "\n";
  return $tree;
}

function DisplayChar ($type, $char) 
//display leave color when $char = "*" and background color when $char = "0"
//according to the tree type

{ 
  $colors = array("red", "gold");
  if ($type === "Holiday") $colors = array("red", "green");
  if ($type === "SacState") $colors = array("gold", "green");
  //if ($type === "surprise") 
  if ($char != 0 && $char != "*") return $char;
  if ($char == 0) return '<span style="color:'.$colors[0].'">0</span>';
  if ($char == '*') return '<span style="color:'.$colors[1].'">*</span>';
}

//find DisplayChar function body outline from  PHP notes in SacCT and put the completed version here

if($type=="Holiday")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}

else if($type=="SacState")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}

else if($type=="Surprise")
{
if($char=="0")
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#E41B17\"><b>$char</b></font>");}
else
{echo("<font face=\"Tahoma\"size=\"5\"color=\"#347C17\"><b>$char</b></font>");};
}




//display greeting of the given tree type

function DisplayGreeting($type, $size = 9) {
  $tree = GenerateTree($size);
  $coloredTree = "";
  foreach (str_split($tree) as $char) {
    $coloredTree .= DisplayChar($type, $char);
  }
  return $coloredTree;
}

//find DisplayGreeting function body from  PHP notes in SacCT and put it here
{
If($type=="Holiday")
{
$message="Happy Holidays!!!";
}
else if($type=="SacState")
{
$message="Go Sac Hornets!!!";
}
else if($type=="Surprise")
{
$message="Cake is a lie!!";
}
echo("<font face=\"Tahoma\"size=\"4\"color=\"#17e0e3\"><b>$message</b></font><br>");

}

?>

 

I still cant get the tree or the message to show up.  This is my website for it: http://athena.ecs.csus.edu/~cs010415/Greeting_tree.php

 

This is how it should be: http://athena.ecs.csus.edu/~cs010134/Greeting_tree.php

 

Any suggestions?

Link to comment
Share on other sites

Why is your class using PHP4? PHP5 was released in 2004.

 

You should talk to your teacher about this, there's no reason to learn the language on a 10 year old, unsupported parser.

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.