Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2009, 03:04 AM   PM User | #1
puglover
New Coder

 
Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
puglover is an unknown quantity at this point
parsing error..cant find an error in the code

after working for about 3 days on this project i finally got most of it working..now i get this parsing error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/students/got5758/public_html/hmwk5.php on line 38

no one i've talked to can figure out whats wrong with my code. can someone help???

Code:
<?php
$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");
?>
<html>
<head>
<title>
</title>
<style type="text/css">
.rowcolor{
	background : <?php echo $_GET['color']; ?>
}
</style>
</head>
<body>
Create a multiplication table<p>
<form 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" value="<?php echo $_GET['color'];?>">
<?php
for ( $i = 0; $i < count( $rowcolors ); $i++ )
{
	echo "<option value=\"" . $rowcolors[$i] . "\">" . $rowcolors[$i] . "</option>\n";

}
?>
</select>
<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\">"; 
****echo*"<tr><td>&nbsp</td>"; 
**** 
****for(*$i=1;*$i<=$_GET['colnum'];*$i++) 
****{ 
********if($i%2==0)
		{
			echo "<tr class=\"rowcolor\">";
		}
		else
		{
			echo "<tr>";
		}	
		echo"<td>".$i."</td>\n"; 
****	echo"</tr>\n"; 

****	for(*$j=1;*$j<=$_GET['rownum'];*$j++) 
****	{ 
********	if($j%2==0)********* 
********	{	
				echo*"<tr*class=\"rowcolor\">"; 
***			}
			else* 
********	{
				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 is offline   Reply With Quote
Old 10-12-2009, 03:33 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by puglover View Post
...no one i've talked to can figure out whats wrong with my code. can someone help???
Seriously? You're talking to the wrong people, experienced PHP programmers will tell you right away that its the asterisks you have on those lines. I'm not trying to be rude on that, its just curious how nobody else would have noticed. I've removed them, reformatted and fixed a couple of other errors that I believe were damaged from the formatting since they appear fine here. There was one asterisks I left for the $j*$k, since I didn't know if you needed to keep it or not:
PHP Code:
<?php
$rowcolors 
= array ("purple""red""yellow""orange""blue""pink");
?>
<html>
<head>
<title></title>
<style type="text/css">
.rowcolor {
    background: <?php echo $_GET'color' ];
    
?>
}
</style>
</head>
<body>
Create a multiplication table
<p>


<form method="get" action="<?php $_SERVER['SCRIPT_NAME'];?> ">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" value="<?php echo $_GET['color'];?>">
<?php
for ( $i 0$i count$rowcolors ); $i++ )
{
    echo 
"<option value=\"" $rowcolors[$i] . "\">" $rowcolors[$i] . "</option>\n";

}
?>
</select> <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\">";
    echo
"<tr><td>&nbsp</td>";

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

        for(
$j=1;$j<=$_GET['rownum'];$j++)
        {
            if(
$j%2==0)
            {
                echo
"<tr class=\"rowcolor\">";
            }
            else
            {
                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>
Edit:
I just removed some more asterisks inside of the html that I came across. I'm assuming that was a copy/paste and the asterisks were representing spaces. I'm not certain how that came about otherwise.
I also changed the PHP_SELF (which was incorrect with php_self) to SCRIPT_NAME - PHP_SELF is XSS exploitable, so it should be avoided.
I think that gets them all.
Edit:
Also, I should mention this since its clearly an assignment based off of you're file name, this help will likely not be constituted as cheating - I mearly removed the *'s for you and changed the PHP_SELF explaining why it should not be used. It is highly probable that you're instructor can find this posting online otherwise, so I thought I'd better mention that for you're benefit.

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 10-12-2009 at 03:39 AM..
Fou-Lu is offline   Reply With Quote
Old 10-12-2009, 05:57 AM   PM User | #3
puglover
New Coder

 
Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
puglover is an unknown quantity at this point
ok those asterisks were not in my code...they must have been put in when i copied and pasted...im not an idiot...that one that you left was for multiplication...i re did the whole page with different code...it looks identical but this page still wont work..the new one does so ill mark this as solved.

and i just took the code that you "corrected" and it still gives me the error...so thanks anyway

Last edited by puglover; 10-12-2009 at 06:02 AM..
puglover is offline   Reply With Quote
Old 10-12-2009, 07:07 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I didn't mean to imply that you were an idiot, I apologize if it sounded that way.
I kinda thought that was what the last asterisks was for, thats why I left that one.

As for still getting the error, ensure you're not cached. I just tried it and its fine (granted, it won't work as anticipated and still has some warnings). To fix you're warnings, anytime you're retrieving user input, always check if its set, otherwise default it:
PHP Code:
$myColor '';
if (isset(
$_GET['color']))
{
   
$myColor $_GET['color'];
}
// Or, a little more elegant:
$myColor = isset($_GET['color']) ? $_GET['color'] : ''
Also, <select> doesn't take a value, instead, if the color is selected you add a 'selected = "selected"' to you're option tag:
PHP Code:
<p>Select a color<br />
<select name="color">
<?php
for ( $i 0$i count$rowcolors ); $i++ )
{
    
$sSelected = (isset($_GET['color']) && $_GET['color'] == $rowcolors[$i]) ? ' selected="selected"' '';
    echo 
"<option value=\"" $rowcolors[$i] . $sSelected "\">" $rowcolors[$i] . "</option>\n";

}
I'll leave you to the table, thats the meat of this one.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 PM.


Advertisement
Log in to turn off these ads.