[PHP] Operator as Variable?

Status
Not open for further replies.

Peter.Cort

TF's First Dry Ice User!
Messages
5,018
Location
Boston, MA
Hi guys, I'm working on a project and I need to output either a multiplication table, addition table, or division table based on whatever the user puts into a form. I have my function all written out to display the table, and as it stands right now I have an IF statement to figure out which kind of operator they want to use and then have that run to display the table, but I have 3 different functions, and the only change is how I have the calculation. The change is ($x * $y) to ($x + $y) or ($x/$y). Seems unnecessary to have to copy 34 lines of code just to change 1 line.

What I'd like to do is set it up as a function where I can do an IF statement to figure out the operator they want and then put the operator in as a function paramater and run the function that way, so the function would look like ($x ($operator) $y) and the operator would be passed through the function paramaters.

Thoughts?
 
We're going to need some code to a) understand what you mean here (it probably makes total sense to you because you understand the project that you're working on) and b) so that we can actually help with more than just saying "yes". Also by IF, I'm sure you mean SWITCH :)

EDIT: actually scratch that last bit
 
Here's the function as it stands.
Code:
if($operator = "Multiplication");
        {
	$x = 1;
	print "<tr>";
        print "<td>*</td>";         /* Showing the operator in the top */
	while($x <= $columns)       /* Says to run for all the rows */
	{
	print "<td>";               
	echo $x;                    /* Creates the columns.*/
        print "</td>";
        $x++;                       /* Incriments to create the appropriate amount of columns. */
        }
        print "</tr>";
        print "<tr>";
        
                                    /* The main part of the function that tells the row to multiply itself by the column */
	$y = 1;
        while($y <= $rows)
        {
        $x = 1;                     /* Resetting $x everytime the loop loops so the correct number of columns are filled in */
        print "<td>";
        print $y;
        print "</td>";
            while ($x <= $columns)  /*sub-loop that populates row x */
            {
            print "<td>";
            echo ($x * $y);
            print "</td>";
            $x++;
            }
                                    /* Start new line beginning with the value of $y and then do operations, end with new line */
        $y++;
        print "</tr>";
        }
        }

Sorry for all the table tags, yes they are kinda necessary :(
 
a few notes


PHP:
// most of this page should be static.  only use decisions where the page is dynamic.
// in this case where your operator is.  leave all else the same
//if($operator = "Multiplication");
        {
	$x = 1;
	print "<tr>";
// you want to switch off here because it has a operator
        print "<td>*</td>";         /* Showing the operator in the top */
	while($x <= $columns)       /* Says to run for all the rows */
	{
	print "<td>";               

// $x is used as a counter but also enters the value.  
// if the value if coming from the form input (im assuming POST)
// then use _POST array;  like _POSt["numerator"]
//	echo $x;                    /* Creates the columns.*/

        print "</td>";
        $x++;                       /* Incriments to create the appropriate amount of columns. */
        }
        print "</tr>";
        print "<tr>";
        
                                    /* The main part of the function that tells the row to multiply itself by the column */
	$y = 1;
        while($y <= $rows)
        {
        $x = 1;                     /* Resetting $x everytime the loop loops so the correct number of columns are filled in */
        print "<td>";

// $y is used as a counter but also enters the value.  
// if the value if coming from the form input (im assuming POST)
// then use _POST array;  like _POSt["numerator"]
//      print $y;
        print "</td>";
            while ($x <= $columns)  /*sub-loop that populates row x */
            {
            print "<td>";
// you want to switch off here because it has a operator
            echo ($x * $y);
            print "</td>";
            $x++;
            }
                                    /* Start new line beginning with the value of $y and then do operations, end with new line */
        $y++;
        print "</tr>";
        }
        }
 
I left out some stuff, I'm just using a basic extract($_REQUEST, EXTR_SKIP); to get the info out of the form. Also when you say 'switch off' with the x/y counter what do you mean?

Also the page has to be dynamic because it's based off of user inputted rows and columns, this is the HTML front page,

Code:
	<!-- 
	Project 1
	Author: Peter Cort
	Date: 3/7/2012
	Script: Write a form that allows the user to select several paramaters about a table's size as well as an operator to produce a table of values.
	-->

	<html>
	<head>
	<title>
	Operator Tables
	</title>
	</head>
	
	<body>
	
	<form name="input" action="project1.php" method="post">
	<label>Rows</label>
	<select name="rows">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<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">10</option>
	</select>
	
	<br />
	
	<label>Columns</label>
	<select name="columns">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<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">10</option>
	</select>
	
	<br />
	<br />
	
	<label>Operator:</label><br />
	<input type="checkbox" name="Operator" value="Multiplication" />Multiplication<br />
	<input type="checkbox" name="Operator" value="Division" />Division<br />
	<input type="checkbox" name="Operator" value="Addition" />Addition
	
	<br />
	<br />
	
	<label>Font Size</label>
	<select name="Fontsize">
	<option value="8">Size 8</option>
	<option value="12">Size 12</option>
	<option value="16">Size 16</option>
	</select>
	<br />
	<br />
	<label>Show Header</label><br />
	<input type="radio" name="header" value="yes" checked />Yes<br />
	<input type="radio" name="header" value="no" />No
	<br />
	<br />
	<input type="Submit" value="Submit"/> 
	<input type="Reset" value="Reset"/>
	</form>
	</body>
	</html>
 
Maybe something like this:
PHP:
<?php

function symbol($operator)
{
    switch($operator)
    {
        case "Multiplication":
            return "*";
        default:
            return "";
    }
}

function do_math($operator, $x, $y)
{
    switch($operator)
    {
        case "Multiplication":
            return $x * $y;
        default:
            return " ";
    }
}

$operator = "*";
$columns = 4;
$rows = 5;
echo '<html><body><table>';
	$x = 1;
	print "<tr>";
        print "<td>".symbol($operator)."</td>";         /* Showing the operator in the top */
	while($x <= $columns)       /* Says to run for all the rows */
	{
	print "<td>";               
	echo $x;                    /* Creates the columns.*/
        print "</td>";
        $x++;                       /* Incriments to create the appropriate amount of columns. */
        }
        print "</tr>";
        print "<tr>";
        
                                    /* The main part of the function that tells the row to multiply itself by the column */
	$y = 1;
        while($y <= $rows)
        {
        $x = 1;                     /* Resetting $x everytime the loop loops so the correct number of columns are filled in */
        print "<td>";
        print $y;
        print "</td>";
            while ($x <= $columns)  /*sub-loop that populates row x */
            {
            print "<td>";
            echo do_math($operator, $x, $y);
            print "</td>";
            $x++;
            }
                                    /* Start new line beginning with the value of $y and then do operations, end with new line */
        $y++;
        print "</tr>";
        }
echo '</table></body></html>';
?>

EDIT: ultra ninja'd. That's what I get for replying to a post I loaded an hour ago...
 
Ok, when I googled Switch I did Switch Off not just switch and got a whole bunch of **** on debugging haha. Makes sense now, and it looks like that would work.

Also another thing that's really bugging me and it could be from staring at it for 2 hours now, or something, but it seems like it should be super easy, but I'm trying to get the font size to change based on the user input, and I'm doing it via an if statement that pulls the font size from the form and then if it matches the value it prints the header style associated with the font size change (I know header style is silly and dumb, but it's a school project so it is what it is :p ) This is what I have and it always defaults to the first item in the if clump.

Code:
<style type="text/css">
        <?php
        extract($_REQUEST, EXTR_SKIP);
        
        if($fontsize="8")
        {
        print 'table {font-size:8px}';
        }
        if($fontsize="12")
        {
        print "table {font-size:12px}";
        }
        if($fontsize="16")
        {
        print "table {font-size:16px}";
        }
        ?>
        </style>

The name from the form is fontsize, and it does display the correct font size, just of whatever font is listed first.

What I'm thinking is that it's not pulling which one is selected, cause when I do a view source they all show up as being there.

EDIT:
Yup. that was it. I just did a simple function call and it works!!! yayyyyyyy haha. small fish are fried!
 
Status
Not open for further replies.
Back
Top Bottom