need help getting css to work with php variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
puglover
Forum Newbie
Posts: 5
Joined: Tue Oct 06, 2009 3:09 pm

need help getting css to work with php variables

Post by puglover »

the object of my program is to make a multiplication table by getting user input for number of rows, columns and the color you want the background of everyother row to be. I FINALLY got the table to display correctly now my only trouble is the css. i've tried it every which way i can think of...i've googled throughout the night looking for answers if anyone could help me it would be greatly appreciated. if you dont understand my problem please just ask me to clerify because i'm not sure if i'm being clear enough.

Code: Select all

 
<html>
<head>
<title>
</title>
<script type="text/javascript">
 
function checkForm()
{
    if( isNaN(document.multable.rownum.value) )
    {
        alert ("Sorry row number is not a number");
        return false;
    }
    if( isNaN(document.multable.colnum.value) )
    {
        alert ("Sorry column number is not a number");
        return false;
    }
    return true;
}
</script>
<?php
$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");
 
?>
</head>
<body>
Create a multiplication table<p>
<form name="multable" onSubmit="return checkForm()"method="get" action="<?php $_SERVER['php_self'];?> ">
Enter number of rows<br />
<input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p>
Enter number of columns<br />
<input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p>
Select a color<br />
<select name="color">
<?php
for ( $i = 0; $i < count( $rowcolors ); $i++ )
{
    echo "<option value=\"" . $_GET['color'] . "\">" . $rowcolors[$i] . "</option>\n";
 
}
?>
<input type="hidden" name="do_php" value="true">
<p><input type="submit" value="Create Table">
</form>
<?php
 
if(isset($_GET['do_php']))
{
    echo"<table width=\"50%\"border=\"3\">\n";
    echo"<tr><td>&nbsp</td>";
    
    if ( $i%2 == 0 )
    {
        echo "<div style=\"background:". $_GET['color'] . ";\">\n";
    }
    for( $i=1; $i<=$_GET['colnum']; $i++)
    {
        echo"<td>".$i."</td>\n";
    }
 
    echo"</tr>\n";
 
    for( $j=1; $j<=$_GET['rownum']; $j++)
    {
        echo"<tr>";
        echo"<td>".$j."</td>\n";
 
        for( $k=1; $k<=$_GET['colnum']; $k++)
        {
            echo"<td>".$j*$k."</td>\n";
        }
    
    }
    echo"</tr>\n";
    echo"</div>\n";
}
 
echo"</table>\n";
 
?>
 
</body>
</html>
 
puglover
Forum Newbie
Posts: 5
Joined: Tue Oct 06, 2009 3:09 pm

Re: need help getting css to work with php variables

Post by puglover »

just to add more information when my program runs no value for color is stored anywhere. i believe thats what my issue is but im not sure how to fix it. at one point the value being sent to the style tag was the number from the option tag of the corresponding number but that doesnt happen anymore
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: need help getting css to work with php variables

Post by markusn00b »

Hi, puglover.

The problem is this line:

Code: Select all

echo "<option value=\"" . $_GET['color'] . "\">" . $rowcolors[$i] . "</option>\n";
You are trying to grab $_GET['color'], which, when the page is first loaded, most likely won't exist, so all the options will have a value of nada damn thing! Or, possibly the text of a PHP error, if you have errors on (undefined index). You should, in fact, be using the $rowcolors array that you use in the same line to grab the current index ($i) for the color. So, something like:

Code: Select all

echo "<option value=\"" . $rowcolors[$i] . "\">" . $rowcolors[$i] . "</option>\n";
Hope this helps,
Mark.
puglover
Forum Newbie
Posts: 5
Joined: Tue Oct 06, 2009 3:09 pm

Re: need help getting css to work with php variables

Post by puglover »

yes i fixed that problem and now when i view source code it should technically be working...but its not...maybe im still doing something wrong..i dunno

Code: Select all

<?php
$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");
?>
<html>
<head>
<title>
</title>
<script type="text/javascript">

function checkForm()
{
	if( isNaN(document.multable.rownum.value) )
	{
		alert ("Sorry row number is not a number");
		return false;
	}
	if( isNaN(document.multable.colnum.value) )
	{
		alert ("Sorry column number is not a number");
		return false;
	}
	return true;
}
</script>
<style type="text/css">
.rowcolor{
	background : <?php echo $_GET['color']?>;
}
</style>
</head>
<body>
Create a multiplication table<p>
<form name="multable" onSubmit="return checkForm()"method="get" action="<?php $_SERVER['php_self'];?> ">
Enter number of rows<br />
<input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p>
Enter number of columns<br />
<input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p>
Select a color<br />
<select name="color">
<?php
for ( $i = 0; $i < count( $rowcolors ); $i++ )
{
	echo "<option value=\"" . $rowcolors[$i] . "\">" . $rowcolors[$i] . "</option>\n";

}
?>
<input type="hidden" name="do_php" value="true">
<p><input type="submit" value="Create Table">
</form>
<?php

if(isset($_GET['do_php']))
{
	echo"<table width=\"50%\"border=\"3\">\n";
	echo"<tr><td>&nbsp</td>";

	
	for( $i=1; $i<=$_GET['colnum']; $i++)
	{		
		if ( $i%2 == 0 )
		{
			echo "<div class=\"rowcolor\">\n";
		}
		echo"<td>".$i."</td>\n";
	}

	echo"</tr>\n";
	
	for( $j=1; $j<=$_GET['rownum']; $j++)
	{
		echo"<tr>";
		echo"<td>".$j."</td>\n";

		for( $k=1; $k<=$_GET['colnum']; $k++)
		{
			echo"<td>".$j*$k."</td>\n";
		}
	
	}
	echo"</div>\n";
	echo"</tr>\n";
}

echo"</table>\n";

?>

</body>
</html>/code]
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: need help getting css to work with php variables

Post by markusn00b »

What exactly isn't working? I cannot read your mind.
puglover
Forum Newbie
Posts: 5
Joined: Tue Oct 06, 2009 3:09 pm

Re: need help getting css to work with php variables

Post by puglover »

the color of the row is not changing. its showing in the css that the variable is there but nothing is changing colors
Post Reply