Jump to content

Recommended Posts

I can't seem to figure these parts out...I have the highlight and table up and running, but these are the parts I'm having difficult with. Help would be awesome!

 

 

"Generate Table with POST using these values"

    Values used in table generation: Clicking this button will submit a form with three table values (row, col, highlight) using the POST method . These POST values will be used in the generation of the table.

    Default Values: The three select boxes are preset to the default values when the page is first entered or the "Generate Table using default values" link is clicked.

    Select Box has Memory: The three select boxes in the form "remember" the values selected from each POST.

 

"Generate Table using GET. "

 

    Always has URL parms: Clicking this link will generate the table using the values stated in the URL parms within this link. This link always has URL parms and is never without them.

    Default Values: The URL parms of this link are set to the default values when the page is first entered or the "Generate Table using default values" link is clicked.

    Acquires POSTed values: The URL parms of this link are set to the values chosen in the three select boxes each time the form is POSTed.

 

Some functions required to use:

 

    The builtin Apache global $_SERVER['REQUEST_METHOD'] was used to determine if the incoming request was a POST or a GET.

    The builtin PHP function count() will return the number of rows in an array.

 

What I have so far:

 

<!--"StAuth10065: I Adam Graham, 000136921 certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else."-->
<?
include "greenarray.php";

if ($_SERVER['REQUEST_METHOD'] == 'GET') 



?>


<html>
<head>
<style>
body {background-color:salmon} 
table {	border: 1px solid black;	margin-right: auto;	margin-left: auto;}
table tr.odd, span.odd {background-color:#99CC99}
table tr.even, span.even {background-color:#CCFFCC}
table td {padding:5; }
table td.five {background-color:#990000; color:#FFFFFF }
div#form {	margin-right: auto;	margin-left: auto;	width: 300px;
text-align:center;	background-color: #CCCCCC;}
div#container {border: 5px ridge #990000; margin: auto auto; width: 600px;
			background-color:white;text-align: center}
h1 {color: #CCCCCC;}
a {color:#006}
a:hover {text-decoration:none}
</style>
</head>
<body>
<?= '<pre>'.print_r($_POST,true).'</pre>' ?>
<div id="container">
<h1>Modified Table Generator</h1>
<p ><a href="<?= $_SERVER['PHP_SELF'] ?>">Generate Table using default values.</a></p>
<p ><a href="<?= $_SERVER['PHP_SELF'] ?>?rows=<?= $_POST['rows'] ?>cols=10&highlight=5">
Generate Table with GET: table.php?rows=<?= $_POST['rows'] ?>&cols=10&highlight=5 

</a></p>

<div style=";">
<div id="form">
<form name="form1" method="post" action="table.php ">
  rows
    <select name="rows">
    <option value="5"   >5</option>
    <option value="6" >6</option>
    <option value="7" <? if ($_POST['rows'] == 7)  echo 'selected' ?>> 7 </option>

<option value="8" >8</option>
<option value="9" <?= ($_POST['rows'] == 9)?'selected':'notselected' ?>> 9</option>
<option value="10" selected>10</option>
  </select>
    cols
  <select name="cols">
    <option value="5"   >5</option>

    <option value="6" >6</option>
    <option value="7" >7</option>
<option value="8" >8</option>
<option value="9" >9</option>
<option value="10" selected>10</option>
  </select>

  highlight
  <select name="highlight">
    <option value="5"  selected >5</option>
    <option value="6" >6</option>
    <option value="7" >7</option>
<option value="8" >8</option>
<option value="9" >9</option>

<option value="10" >10</option>
  </select>
  
  <br><input type="submit" name="Submit" value="Generate Table with Post using these values">
</form>
</div>
</div>
    

        <?php

        $randomnumber = rand(0,100);

        if ($randomnumber % 2 == 0)
        {
        $rowclass = "class=even";
        $alternatingrowclass = "class=odd";
        echo "<center>First number is an <span $rowclass> even</span> number</center>";
    }
    else
    {
    $rowclass = "class=odd";
    $alternatingrowclass = "class=even";
    echo "<center>First number is an <span $rowclass> odd</span> number</center>";
    }

    echo "<table>";
        for ($row = 1; $row <= 10; $row ++)
        {
        if ($row %2 == 1)
        echo "<tr $rowclass>\n";
            else
            echo "<tr $alternatingrowclass>\n";

            for ($col = 1; $col <= 10; $col ++)
            {
            $columnclass = ($col == 5 || $col == 10) ? "class=five" : "";
            echo "<td $columnclass>$randomnumber</td>\n";
            $randomnumber++;
            }
            }       
            echo "</tr>\n";
        echo "</table>\n";

    ?>
</p>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/248706-modified-table-fenerator/
Share on other sites

I'm guessing this is what you were after.

<?php
@include "greenarray.php"; //suppressed error, because I didn't have the file;
//variable setting #defaults
$rows = 7;
$cols = 10;
$highlight = 5;
//reset variables if they are in the GET array.
$rows = (!empty($_GET['rows'])) ? $_GET['rows'] : $rows;
$cols = (!empty($_GET['cols'])) ? $_GET['cols'] : $cols;
$highlight = (!empty($_GET['highlight'])) ? $_GET['highlight'] : $highlight;
//reset variables if they are i the POST array.
$rows = (!empty($_POST['rows'])) ? $_POST['rows'] : $rows;
$cols = (!empty($_POST['cols'])) ? $_POST['cols'] : $cols;
$highlight = (!empty($_POST['highlight'])) ? $_POST['highlight'] : $highlight;

?>


<html>
<head>
<style>
body {background-color:salmon} 
table {	border: 1px solid black;	margin-right: auto;	margin-left: auto;}
table tr.odd, span.odd {background-color:#99CC99}
table tr.even, span.even {background-color:#CCFFCC}
table td {padding:5; }
table td.five {background-color:#990000; color:#FFFFFF }
div#form {	margin-right: auto;	margin-left: auto;	width: 300px;
text-align:center;	background-color: #CCCCCC;}
div#container {border: 5px ridge #990000; margin: auto auto; width: 600px;
			background-color:white;text-align: center}
h1 {color: #CCCCCC;}
a {color:#006}
a:hover {text-decoration:none}
</style>
</head>
<body>

<div id="container">
<h1>Modified Table Generator</h1>
<p ><a href="table.php">Generate Table using default values.</a></p>
<p ><a href="?rows=<?php echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //builds a GET query string?>">
Generate Table with GET: table.php?rows=<?php echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //prints a GET query string?>

</a></p>

<div style=";">
<div id="form">
<form name="form1" method="post" action="">
  rows
    <select name="rows">
    <option value="5" <?php echo ($rows == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($rows == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($rows == 7) ? 'selected' : NULL; ?>> 7 </option>

<option value="8" <?php echo ($rows ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($rows == 9) ? 'selected' : NULL; ?>> 9</option>
<option value="10" <?php echo ($rows == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
    cols
  <select name="cols">
    <option value="5" <?php echo ($cols == 5) ? 'selected' : NULL; ?>>5</option>

    <option value="6" <?php echo ($cols == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($cols == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($cols ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($cols == 9) ? 'selected' : NULL; ?>>9</option>
<option value="10" <?php echo ($cols == 10) ? 'selected' : NULL; ?>>10</option>
  </select>

  highlight
  <select name="highlight">
    <option value="5" <?php echo ($highlight == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($highlight == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($highlight == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($highlight ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($highlight == 9) ? 'selected' : NULL; ?>>9</option>

<option value="10" <?php echo ($highlight == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
  
  <br><input type="submit" name="Submit" value="Generate Table with Post using these values">
</form>
</div>
</div>
    

        <?php

        $randomnumber = rand(0,100); //get random number;

        if ($randomnumber % 2 == 0)
        {
        $rowclass = "odd"; //if number is even, set the row class to odd.
        echo "<center>First number is an <span class=\"even\"> even</span> number</center>";
    }
    else
    {
    $rowclass = "even";    //if number is odd, set the row class to even.
    echo "<center>First number is an <span class=\"odd\"> odd</span> number</center>";
    }

    echo "<table>"; //start table.
        for ($row = 0; $row < $rows; $row++) //if the row number is less than the rows wanted, continue the loop.
        {
		$rowclass = ($rowclass == 'even') ? 'odd' : 'even'; //if the row class is even, set it to odd, if it is odd, set it to even.
            echo "<tr class=\"{$rowclass}\">\n";
           
            for ($col = 0; $col < $cols; $col++) //if the col number is less than the cols wanted, continue the loop.
            {
            $columnclass = ($col + 1 == $highlight) ? "class=five" : ""; //if the col number + 1 (since we are always a number behind) is equal to the highlight wanted, set the class.
            echo "<td $columnclass>$randomnumber</td>\n";
            $randomnumber++; //increment number.
            }
		echo "</tr>\n"; //end of row.
        }       
        echo "</table>\n"; //end of table.
    ?>
</p>
</body>
</html>

 

I'm guessing this is what you were after.

<?php
@include "greenarray.php"; //suppressed error, because I didn't have the file;
//variable setting #defaults
$rows = 7;
$cols = 10;
$highlight = 5;
//reset variables if they are in the GET array.
$rows = (!empty($_GET['rows'])) ? $_GET['rows'] : $rows;
$cols = (!empty($_GET['cols'])) ? $_GET['cols'] : $cols;
$highlight = (!empty($_GET['highlight'])) ? $_GET['highlight'] : $highlight;
//reset variables if they are i the POST array.
$rows = (!empty($_POST['rows'])) ? $_POST['rows'] : $rows;
$cols = (!empty($_POST['cols'])) ? $_POST['cols'] : $cols;
$highlight = (!empty($_POST['highlight'])) ? $_POST['highlight'] : $highlight;

?>


<html>
<head>
<style>
body {background-color:salmon} 
table {	border: 1px solid black;	margin-right: auto;	margin-left: auto;}
table tr.odd, span.odd {background-color:#99CC99}
table tr.even, span.even {background-color:#CCFFCC}
table td {padding:5; }
table td.five {background-color:#990000; color:#FFFFFF }
div#form {	margin-right: auto;	margin-left: auto;	width: 300px;
text-align:center;	background-color: #CCCCCC;}
div#container {border: 5px ridge #990000; margin: auto auto; width: 600px;
			background-color:white;text-align: center}
h1 {color: #CCCCCC;}
a {color:#006}
a:hover {text-decoration:none}
</style>
</head>
<body>

<div id="container">
<h1>Modified Table Generator</h1>
<p ><a href="table.php">Generate Table using default values.</a></p>
<p ><a href="?rows=<?php echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //builds a GET query string?>">
Generate Table with GET: table.php?rows=<?php echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //prints a GET query string?>

</a></p>

<div style=";">
<div id="form">
<form name="form1" method="post" action="">
  rows
    <select name="rows">
    <option value="5" <?php echo ($rows == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($rows == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($rows == 7) ? 'selected' : NULL; ?>> 7 </option>

<option value="8" <?php echo ($rows ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($rows == 9) ? 'selected' : NULL; ?>> 9</option>
<option value="10" <?php echo ($rows == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
    cols
  <select name="cols">
    <option value="5" <?php echo ($cols == 5) ? 'selected' : NULL; ?>>5</option>

    <option value="6" <?php echo ($cols == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($cols == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($cols ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($cols == 9) ? 'selected' : NULL; ?>>9</option>
<option value="10" <?php echo ($cols == 10) ? 'selected' : NULL; ?>>10</option>
  </select>

  highlight
  <select name="highlight">
    <option value="5" <?php echo ($highlight == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($highlight == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($highlight == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($highlight ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($highlight == 9) ? 'selected' : NULL; ?>>9</option>

<option value="10" <?php echo ($highlight == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
  
  <br><input type="submit" name="Submit" value="Generate Table with Post using these values">
</form>
</div>
</div>
    

        <?php

        $randomnumber = rand(0,100); //get random number;

        if ($randomnumber % 2 == 0)
        {
        $rowclass = "odd"; //if number is even, set the row class to odd.
        echo "<center>First number is an <span class=\"even\"> even</span> number</center>";
    }
    else
    {
    $rowclass = "even";    //if number is odd, set the row class to even.
    echo "<center>First number is an <span class=\"odd\"> odd</span> number</center>";
    }

    echo "<table>"; //start table.
        for ($row = 0; $row < $rows; $row++) //if the row number is less than the rows wanted, continue the loop.
        {
		$rowclass = ($rowclass == 'even') ? 'odd' : 'even'; //if the row class is even, set it to odd, if it is odd, set it to even.
            echo "<tr class=\"{$rowclass}\">\n";
           
            for ($col = 0; $col < $cols; $col++) //if the col number is less than the cols wanted, continue the loop.
            {
            $columnclass = ($col + 1 == $highlight) ? "class=five" : ""; //if the col number + 1 (since we are always a number behind) is equal to the highlight wanted, set the class.
            echo "<td $columnclass>$randomnumber</td>\n";
            $randomnumber++; //increment number.
            }
		echo "</tr>\n"; //end of row.
        }       
        echo "</table>\n"; //end of table.
    ?>
</p>
</body>
</html>

 

That works...but when I click the link for get table using these values with get link the table disappears

I cannot reproduce that error, the link works for me. Are you using the code as is, or did you change it?

 

I think I am using the same code:

 

<?
include "greenarray.php";

$rows = 10;
$cols = 10;
$highlight = 5;

$rows = (!empty($_GET['rows'])) ? $_GET['rows'] : $rows;
$cols = (!empty($_GET['cols'])) ? $_GET['cols'] : $cols;
$highlight = (!empty($_GET['highlight'])) ? $_GET['highlight'] : $highlight;

$rows = (!empty($_POST['rows'])) ? $_POST['rows'] : $rows;
$cols = (!empty($_POST['cols'])) ? $_POST['cols'] : $cols;
$highlight = (!empty($_POST['highlight'])) ? $_POST['highlight'] : $highlight;

?>


<html>
<head>
<style>
body {background-color:salmon} 
table {	border: 1px solid black;	margin-right: auto;	margin-left: auto;}
table tr.odd, span.odd {background-color:#99CC99}
table tr.even, span.even {background-color:#CCFFCC}
table td {padding:5; }
table td.five {background-color:#990000; color:#FFFFFF }
div#form {	margin-right: auto;	margin-left: auto;	width: 300px;
text-align:center;	background-color: #CCCCCC;}
div#container {border: 5px ridge #990000; margin: auto auto; width: 600px;
			background-color:white;text-align: center}
h1 {color: #CCCCCC;}
a {color:#006}
a:hover {text-decoration:none}
</style>
</head>
<body>
<?= '<pre>'.print_r($_POST,true).'</pre>' ?>
<div id="container">
<h1>Modified Table Generator</h1>
<p ><a href="<?= $_SERVER['PHP_SELF'] ?>">Generate Table using default values.</a></p>
<p ><a href="?rows=<<? echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //builds a GET query string?>">
Generate Table with GET: table.php?rows=<? echo $rows . '&cols=' . $cols . '&highlight=' . $highlight; //prints a GET query string?>

</a></p>

<div style=";">
<div id="form">
<form name="form1" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
  rows
    <select name="rows">
    <option value="5" <?php echo ($rows == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($rows == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($rows == 7) ? 'selected' : NULL; ?>> 7 </option>

<option value="8" <?php echo ($rows ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($rows == 9) ? 'selected' : NULL; ?>> 9</option>
<option value="10" <?php echo ($rows == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
    cols
  <select name="cols">
    <option value="5" <?php echo ($cols == 5) ? 'selected' : NULL; ?>>5</option>

    <option value="6" <?php echo ($cols == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($cols == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($cols ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($cols == 9) ? 'selected' : NULL; ?>>9</option>
<option value="10" <?php echo ($cols == 10) ? 'selected' : NULL; ?>>10</option>
  </select>

  highlight
  <select name="highlight">
    <option value="5" <?php echo ($highlight == 5) ? 'selected' : NULL; ?>>5</option>
    <option value="6" <?php echo ($highlight == 6) ? 'selected' : NULL; ?>>6</option>
    <option value="7" <?php echo ($highlight == 7) ? 'selected' : NULL; ?>>7</option>
<option value="8" <?php echo ($highlight ==  ? 'selected' : NULL; ?>>8</option>
<option value="9" <?php echo ($highlight == 9) ? 'selected' : NULL; ?>>9</option>

<option value="10" <?php echo ($highlight == 10) ? 'selected' : NULL; ?>>10</option>
  </select>
  
  <br><input type="submit" name="Submit" value="Generate Table with Post using these values">
</form>
</div>
</div>
    

        <?php

        $randomnumber = rand(0,100);

        if ($randomnumber % 2 == 0)
        {
        $rowclass = "class=even";
        $alternatingrowclass = "class=odd";
        echo "<center>First number is an <span $rowclass> even</span> number</center>";
    }
    else
    {
    $rowclass = "class=odd";
    $alternatingrowclass = "class=even";
    echo "<center>First number is an <span $rowclass> odd</span> number</center>";
    }

   echo "<table>"; 
        for ($row = 0; $row < $rows; $row++) 
        {
		$rowclass = ($rowclass == 'even') ? 'odd' : 'even';  
            echo "<tr class=\"{$rowclass}\">\n";
           
            for ($col = 0; $col <= $cols; $col++) 
            {
            $columnclass = ($col == 5 || $col == 10) ? "class=five" : ""; 
            echo "<td $columnclass>$randomnumber</td>\n";
            $randomnumber++; 
            }
		echo "</tr>\n"; 
        }       
        echo "</table>\n"; 
    ?>
</p>
</body>
</html>

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.