Jump to content

include question (globals)


pngtest

Recommended Posts

do you need to have registered globals on when you use the include.

 

i have these pages that i got from a book to teach me to program php i had to change some stuff as it teach's with globals on. but i did not think you would for the include.

Link to comment
Share on other sites

thanks for that but it does not seem to help. i have the two files where one get variables from the other and prints out basic html tags here

<?php
//import the definition of SuperHTML
include "SuperHTMLDef.php";

//instantiate and use a superHTML page

$s = new SuperHTML("My Super Duper Page!");
$s->setTitle("SuperHTML Demo");
$s->buildTop();

$s->h3("Using the tag method");
$s->tag("i", "This line should be italicized");

$s->h3("adding arbitrary text");
$s->addText("I used the addText method here");

$s->h3("build table from 2d array");
$myArray = array(
  array("English", "Spanish", "Japanese"),
  array("One", "Uno", "Ichi"),
  array("Two", "Dos", "Nii"),
  array("Three", "Tres", "San")
);
$s->buildTable($myArray);

$s->h3("build table row-by-row");
$s->startTable(3);
$s->tRow(array("English", "Greek"), "th");
$s->tRow(array("a", "alpha"));
$s->tRow(array("b", "beta"));
$s->endTable();

$s->h3("build an unordered list");
$s->buildList(array("alpha", "beta", "gamma", "delta"));

$s->h3("build an ordered list");
$s->buildList(array("alpha", "beta", "gamma", "delta"), "ol type = 'a'");

$s->h3("form elements");

$s->addText("<form> \n");
$s->textbox("user name", "Joe");


$s->h3("create select object from associative array");

$numArray = array(
  "1"=>"ichii",
  "2"=>"nii",
  "3"=>"san",
  "4"=>"shi"
);

$s->buildSelect("options", $numArray);

$s->h3("make form elements inside a table!");

$myArray = array(
  array($s->gTag("b","name"), $s->gTextbox("name")),
  array($s->gAddText("address"), $s->gTextbox("address")),
  array($s->gAddText("phone"), $s->gTextbox("phone")),
  array($s->gAddText("favorite number"), $s->gSelect("numbers", $numArray))
);

$s->buildTable($myArray);
$s->submit();

$s->addText("</form> \n");

print $s->getPage();

?>

 

you would go to this in the browser and it should print tables and other things.

 

and this is the include.

 

<?

// SuperHTML Class Def
// Andy Harris
// PHP / MySQL Programming for the Absolute Beginner
// 2nd Ed.
class SuperHTML{

  //properties
  var $title;
  var $thePage;

  function __construct($tTitle = "Super HTML"){
    //constructor
    $this->setTitle($tTitle);
  } // end constructor

  function getTitle(){
    return $this->title;
  } // end getTitle

  function setTitle($tTitle){
    $this->title = $tTitle;
  } // end setTitle

  function getPage(){
    return $this->thePage;
  } // end getPage

  //most basic tags
  function addText($content){
    //given any text (including HTML markup)
    //adds the text to the page
    $this->thePage .= $content;
    $this->thePage .= "\n";
  } // end addText

  function gAddText($content){
    //given any text (including HTML markup)
    //returns the text
    $temp= $content;
    $temp .= "\n";
    return $temp;
  } // end addText

  function buildTop(){
    $temp = <<<HERE
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>$this->title</title>
</head>
<body>
<center>
<h1>$this->title</h1>
</center>
HERE;
    $this->addText($temp);
  } // end buildTop;

  function buildBottom(){
    //builds the bottom of a generic web page
    $temp = <<<HERE
</body>
</html>
HERE;
    $this->addText($temp);
  } // end buildBottom;

  //general tag function
  function tag($tagName, $contents){
    //given any tag, surrounds contents with tag
    //improve so tag can have attributes
    $this->addText($this->gTag($tagName, $contents));
  } // end tag

  function gTag($tagName, $contents){
    //given any tag, surrounds contents with tag
    //improve so tag can have attributes
    //returns tag but does not add it to page
    $temp = "<$tagName>\n";
    $temp .= "  " . $contents . "\n";
    $temp .= "</$tagName>\n";
    return $temp;
  } // end tag

  //header functions
  function h1($stuff){
    $this->tag("h1", $stuff);
  } // end h1

  function h2($stuff){
    $this->tag("h2", $stuff);
  } // end h2

  function h3($stuff){
    $this->tag("h3", $stuff);
  } // end h3

  function h4($stuff){
    $this->tag("h4", $stuff);
  } // end h4

  function h5($stuff){
    $this->tag("h5", $stuff);
  } // end h5

  function h6($stuff){
    $this->tag("h6", $stuff);
  } // end h6

  function gBuildList($theArray, $type = "ul"){
    //given an array of values, builds a list based on that array
    $temp= "<$type> \n";
    foreach ($theArray as $value){
      $temp .= " <li>$value</li> \n";
    } // end foreach
    $temp .= "</$type> \n";
    return $temp;
  } // end gBuildList

  function buildList($theArray, $type = "ul"){
    $temp = $this->gBuildList($theArray, $type);
    $this->addText($temp);
  } // end buildList

  function gBuildTable($theArray){
    //given a 2D array, builds an HTML table based on that array
    $table = "<table border = 1> \n";
    foreach ($theArray as $row){
      $table .= "<tr> \n";
      foreach ($row as $cell){
        $table .= "  <td>$cell</td> \n";
      } // end foreach
      $table .= "</tr> \n";
    } // end foreach
    $table .= "</table> \n";

    return $table;
  } // end gBuildTable

  function buildTable($theArray){
    $temp = $this->gBuildTable($theArray);
    $this->addText($temp);
  } // end buildTable


  function startTable($border = "1"){
    $this->thePage .= "<table border = \"$border\">\n";
  } // end startTable

  function tRow ($rowData, $rowType = "td"){
    //expects an array in rowdata, prints a row of th values
    $this->thePage .= "<tr> \n";
    foreach ($rowData as $cell){
      $this->thePage .= "  <$rowType>$cell</$rowType> \n";
    } // end foreach
    $this->thePage .= "</tr> \n";
  } // end thRow

  function endTable(){
    $this->thePage .= "</table> \n";
  } // end endTable

  //form elements
  function gTextbox($name, $value = ""){
    // returns but does not print
    // an input type = text element
    // used if you want to place form elements in a table
    $temp .= <<<HERE
<input type = "text"
       name = "$name"
       value = "$value" />

HERE;
    return $temp;
  } // end textBox

  function textbox($name, $value = ""){
    $this->addText($this->gTextbox($name, $value));
  } // end textBox

  function gSubmit($value = "Submit Query"){
    // returns but does not print
    // an input type = submit element
    // used if you want to place form elements in a table
    $temp .= <<<HERE
<input type = "submit"
       value = "$value" />

HERE;
    return $temp;
  } // end submit

  function submit($value = "Submit Query"){
    $this->addText($this->gSubmit($value));
  } // end submit

  function gSelect($name, $listVals){
    //given an associative array,
    //prints an HTML select object
    //Each element has the appropriate
    //value and displays the associated name
    $temp = "";
    $temp .= "<select name = \"$name\" >\n";
    foreach ($listVals as $val => $desc){
      $temp .= "  <option value = \"$val\">$desc</option> \n";
    } // end foreach
    $temp .= "</select> \n";
    return $temp;

  } // end gSelect

  function select($name, $listVals){
    $this->addText($this->gSelect($name, $listVals));
  } // end buildSelect

  function formResults(){
    //returns the names and values of all form elements
    //in an HTML table
    $this->startTable();
    foreach ($_REQUEST as $name => $value){
      $this->tRow(array($name,$value));
    } // end foreach
    $this->endTable();
  } // end formResults

} // end class def

?>




Link to comment
Share on other sites

You have an incorrectly named function (or you called the wrong one in your page)...

 

function select($name, $listVals){
    $this->addText($this->gSelect($name, $listVals));
  } // end buildSelect

 

Should be:

 

function buildSelect($name, $listVals){
    $this->addText($this->gSelect($name, $listVals));
  } // end buildSelect

Link to comment
Share on other sites

when i go to the demo page you get a whole lot of undefined Variable error's  and i don't know what would cause that.

 

That would be caused by using a whole lot of undefined variables. handling variables is often not tought properly in older books. Basically you need to make sure any variables exist prior to trying to use them. eg;

 

<?php

  if (isset($_POST['foo'])) {
    // $_POST['foo'] is safe to use.

  }

?>

Link to comment
Share on other sites

here it is

 //form elements
  function gTextbox($name, $value = ""){
    // returns but does not print
    // an input type = text element
    // used if you want to place form elements in a table
    $temp .= <<<HERE
<input type = "text"
       name = "$name"
       value = "$value" />

HERE;
    return $temp;
  } // end textBox

Link to comment
Share on other sites

Don't use ".=", instead use "="

 

The ".=" assumes the target of the assignment already exists. It doesn't here.

 

<?php
//form elements
  function gTextbox($name, $value = ""){
    // returns but does not print
    // an input type = text element
    // used if you want to place form elements in a table
    $temp = <<<HERE
<input type = "text"
       name = "$name"
       value = "$value" />

HERE;
    return $temp;
  } // end textBox
?>

 

Ken

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.