0

I keep getting the error: Parse error: syntax error, unexpected 'sNameâ€' (T_STRING), expecting ']'on line 5 I need some help solving this problem. Could anyone tell me what's wrong with my code?

<!DOCTYPE html>
<?php
$data = array [
                ["sName” => “Gervase”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
                ["sName” => “Matt”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
                ["sName” => “Kid”, "sNumber" => "s1234567", "mark” => 95, “comments” => “Well done!”],
                ["sName” => “Nathan”, "sNumber" => "s1234567", "mark” => 85, “comments” => “Well done!”]
  ]
?>
<html>
<head>
    <link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
    <body>
        <div>
            <div class="jumbotron">
                 <div>
                     <div class="container">
                         <h1>Data</h1>
                 </div>
                 <div>
                 <div class="container">
                   <table>
                        <table class="table">
                             <thead>  
                           <?php

                            foreach ($data as $data)
                            {
                                echo"<tr>";
                                echo"<td>".$data["sName"]."</td>";
                                echo"<td>".$data["sNumber"]."</td>";
                                echo"</tr>";
                            }
                            ?>
                                 <tr>
                                   <th>Student number</th>
                                   <th>Student name</th>
                                   <th>Mark</th>
                                   <th>Comments</th>
                                 </tr>
                                 <tr>
                                   <td>Something</td>
                                 </tr>
                             </thead>
                   </table>
                 </div<
       </div>
    </body>

Shroomy
  • 23
  • 3

3 Answers3

0

Given the current code, it may be related to the missing semi-colon after the closing bracket of the $data array.

Beyond that, it looks like your code is mixing different characters that represent quotation marks. You can see this based on the formatting colors SO applied in your question block. Try re-typing the array directly in your editor (rather than copy-and-pasting or something) so that all the characters match up, and see if that helps.

badandyomega
  • 252
  • 1
  • 4
0

"I keep getting the error: Parse error: syntax error, unexpected 'sNameâ€' (T_STRING), expecting ']'on line 5"

You see these curly quotes? “ ”.

You need to change them all to regular quotes ".

["sName" => "Gervase", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Matt", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Kid", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
["sName" => "Nathan", "sNumber" => "s1234567", "mark" => 85, "comments" => "Well done!"]

You may have coded with some kind of "Word" processor or pulled it off the web somewhere, that didn't convert those quotes properly. It's best to use a code "editor".

Plus, if your PHP version is < 5.4, you will need to change your code to array(). However, this doesn't seem to be the case here, given the error message you posted. Otherwise, you would be getting Parse error: syntax error, unexpected '[', expecting '(' instead.

I.e.:

$data = array (
    ["sName" => "Gervase", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
    ["sName" => "Matt", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
    ["sName" => "Kid", "sNumber" => "s1234567", "mark" => 95, "comments" => "Well done!"],
    ["sName" => "Nathan", "sNumber" => "s1234567", "mark" => 85, "comments" => "Well done!"]
)

and just in case, add a closing semi-colon should you have any PHP following that block inside the same PHP block

"comments" => "Well done!"]
    );
  • It's good practice.

Footnotes:

Seeing you only have 2 <td>...</td> with missing elements, add the other two:

echo "<td>".$data["mark"]."</td>";
echo "<td>".$data["comments"]."</td>";

This being a bonus add-on.

I find it strange though in having the column headers below your data. If you want them on top (which by my opinion would be better), put it above it.

You also have the wrong closing here </div< which should have been </div> and a stray <table> tag, and would have generated an error in debug/HTML source.

I.e.:

 <div class="container">
        <table class="table">
             <thead>  

                 <tr>
                   <th>Student number</th>
                   <th>Student name</th>
                   <th>Mark</th>
                   <th>Comments</th>
                 </tr>

           <?php

            foreach ($data as $data)
            {
                echo"<tr>";
                echo"<td>".$data["sName"]."</td>";
                echo"<td>".$data["sNumber"]."</td>";

                echo"<td>".$data["mark"]."</td>";
                echo"<td>".$data["comments"]."</td>";
                echo"</tr>";
            }
            ?>

                 <tr>
                   <td>Something</td>
                 </tr>
             </thead>
   </table>
 </div>

You also have a few missing closing </div>'s in there, but that's beyond the scope of this question. You need to match those up.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Copy and paste following code, there were subtle error like array[] it should be array(), foreach($data as $data) it should be like foreach($data as $datum) $datum can be anything but not $data, and the most important thing is something error with your text editor to.... Look at the display color even this code you may experience different color for presenting $data, please use some other text editor, because your current text editor changes your regular quote to something else so....

<!DOCTYPE html>
<?php
$data = array(
                ["sName"=>"Gervase", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
                ["sName"=>"Matt", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
                ["sName"=>"Kid", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"],
                ["sName"=>"Nathan", "sNumber"=>"s1234567","mark"=>95,"comment"=>"Well Done!"]
  );
?>
<html>
<head>
    <link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
    <body>
        <div>
            <div class="jumbotron">
                 <div>
                     <div class="container">
                         <h1>Data</h1>
                 </div>
                 <div>
                 <div class="container">
                   <table>
                        <table class="table">
                             <thead> 
                              <tr>
                                   <th>Student number</th>
                                   <th>Student name</th>
                                   <th>Mark</th>
                                   <th>Comments</th>
                                 </tr>
                              </tr>
                            </thead>
                            <tbody> 
                           <?php

                            foreach ($data as $datum)
                            {
                                echo"<tr>".
                                      "<td>".$datum["sName"]."</td>".
                                      "<td>".$datum["sNumber"]."</td>".
                                      "<td>".$datum["mark"]."</td>".
                                      "<td>".$datum["comment"]."</td>".
                                "</tr>";
                            }
                            ?>

                                 <tr>
                                   <td>Something</td>
                                 </tr>
                             </thead>
                   </table>
                 </div<
       </div>
    </body>
Veshraj Joshi
  • 3,544
  • 3
  • 27
  • 45
  • *"it should be array()"* - Not necessarily and given the error message. Otherwise, they'd be getting a different error message being `Parse error: syntax error, unexpected '[', expecting '('` which they didn't put in the question, but `Parse error: syntax error, unexpected 'sNameâ€' (T_STRING)`. If their PHP version were and seem to be > 5.4 – Funk Forty Niner May 29 '16 at 03:09
  • 1
    doing that type of comment is frowned upon ^ – Funk Forty Niner May 29 '16 at 03:09
  • @fred-ii i wrote there were the errors.... can you please read the answer, – Veshraj Joshi May 29 '16 at 03:11
  • I have and that isn't the problem, believe me. – Funk Forty Niner May 29 '16 at 03:12
  • *"foreach($data as $data) it should be like foreach($data as $datum) $datum can be anything but not $data"* - Sorry, but both `foreach($data as $data)` and what you suggest do the same thing. The real problem here was the curly quotes. – Funk Forty Niner May 29 '16 at 03:18
  • @fred-ii i have wrote in last line of paragraphp - "please use some other text editor, because your current text editor changes your regular quote to something" – Veshraj Joshi May 29 '16 at 03:21
  • @Fred-ii, i came in to this problem before so i suggest that having other text edotor – Veshraj Joshi May 29 '16 at 03:22