Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

    Last edited by Fou-Lu; 10-12-2009 at 03:39 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •