Reputation: 3
I keep getting the error "Parse error: syntax error, unexpected T_SL" when attempting to use the following code :
<?php
$names = array(1 => 'example1', 2 => 'example2', 3 => 'example3', 4 => 'example4');
$code = <<<HEREDOC<div><h3>$names[$i]</h3>
<div class="rating"><div class="id$i" id="0
$stmt = mysqli_prepare($dbc, 'SELECT userid, rating FROM ranks WHERE userid = ? AND id =?');
mysqli_stmt_bind_param($stmt, 'ii', $userid, $i);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $userid, $i, $rating);
while (mysqli_stmt_fetch($stmt)){
echo ($rating);
}_$i"><script type="text/javascript">$(document).ready(function(){$(".id' . $i. '").jRating({isDisabled : false});});</script></div></div><div class="push">
$query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."'");
$result = mysqli_query($dbc, $query) or die("Error querying database.");
while($row = mysqli_fetch_array($result)){
echo "<h3 class='average'>" . $row["ROUND( AVG(rating),1 )"] . "/5" . "</h3>";}
</div><br/></div><div class="line"></div>;HEREDOC;
for($i=1; $i<5; $i++)
{
echo $code
}?>
I'm sure I'm going at it wrong, I've tried using php opening and closing tags every time it switches to JS/HTML and also tried using single quotes and got errors. Thanks for your time!
Upvotes: 0
Views: 89
Reputation: 1908
First of all: there is an error in the code. The line
$query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."'");
should be
$query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."')";
Second: an alternative solution is to concatenate your content
<?php
$names = array(1 => 'example1', 2 => 'example2', 3 => 'example3', 4 => 'example4');
$code = '<div><h3>'.$names[$i].'</h3>
<div class="rating"><div class="id'.$i.'" id="0'.(
$stmt = mysqli_prepare($dbc, 'SELECT userid, rating FROM ranks WHERE userid = ? AND id =?');
mysqli_stmt_bind_param($stmt, 'ii', $userid, $i);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $userid, $i, $rating);
while (mysqli_stmt_fetch($stmt)){
echo ($rating);
}).'_'.$i.'"><script type="text/javascript">$(document).ready(function(){$(".id' . $i. '").jRating({isDisabled : false});});</script></div></div><div class="push">'.(
$query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."')";
$result = mysqli_query($dbc, $query) or die("Error querying database.");
while($row = mysqli_fetch_array($result)){
echo "<h3 class='average'>" . $row["ROUND( AVG(rating),1 )"] . "/5" . "</h3>";}).'
</div><br/></div><div class="line"></div>';
for($i=1; $i<5; $i++)
{
echo $code
}?>
Suggestion: transform things like the queries in functions (and the call these functions when you need it) so your code is cleaner and more comprehensible.
Upvotes: 0
Reputation:
The correct style of heredoc is:
$code = <<<HEREDOC
HEREDOC;
each notation should be at beginning of the line and ends with line break.
UPDATE: as mentioned by @Mike
can't assign variables or do while loops within HEREDOC notation
Upvotes: 2