...

View Full Version : parsing error..cant find an error in the code



puglover
10-12-2009, 04:04 AM
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???


<?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>

Fou-Lu
10-12-2009, 04:33 AM
...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
$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>



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.

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.

puglover
10-12-2009, 06:57 AM
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

Fou-Lu
10-12-2009, 08:07 AM
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:


$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:


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum