Hi,

DW complains of a syntax error that I cannot understand. I use the code as a standalone and it is not a problem, but when used as a table element as written below, it complains of a syntax error. The red highlight is where it complains, the green seems to work just fine. Any help is appreciated. Thanks

stony

------------------------------ code follows

while ($row = $result->fetch()) {
	$output[] = '<tr>';
	$output[] = '<td>'.$row['manufacturer'].'</td>';
	$output[] = '<td>'.$row['model'].'</td>';
	$output[] = '<td>'.$row['description'].'</td>';
	$output[] = '<td>'.'&#x24;.'.$row['price'].'</td>';

	$output[] = '<td>' echo "<input type='text' name='Value[{$row['id']}]' value='{$row['value']}'>" '</td>';
    $output[] = '<td>' echo "<input type='checkbox' name='Update[{$row['id']}]' value='Box #{$row['id']}'>" '</td>';

// echo "<input type='text' name='Value[{$row['id']}]' value='{$row['value']}'>"
	


	$output[] = '<td>'.'<a href="cart.php?action=add&id='.$row['id'].'&product='.$product.'">Add to list</a>'.'<td>';
	$output[] = '</tr>';
}
$output[] = '</table>';
echo join('',$output);

Dani AI

Generated

The syntax error was caused by trying to use the echo language construct inside an assignment (for example, putting echo in the middle of $output[] = ...). correctly pointed this out: echo is a statement, not an expression, so it cannot appear inside another expression. Build the HTML string instead (concatenate, use sprintf, or emit HTML with separate echo statements).

A compact, safe pattern that avoids the echo-in-assignment trap and also prevents HTML/attribute breaking is shown below. It also casts the id to integer and escapes user data:

$id    = (int) $row['id'];
$value = htmlspecialchars($row['value'], ENT_QUOTES, 'UTF-8');

$output[] = '<tr>';
$output[] = sprintf(
  '<td><input type="text" name="Value[%d]" value="%s"></td>',
  $id, $value
);
$output[] = sprintf(
  '<td><input type="checkbox" name="Update[%d]" value="Box #%d"></td>',
  $id, $id
);
$output[] = '</tr>';

Notes and troubleshooting:

  • Using $output[] repeatedly is fine; it appends array elements and echo join("", $output) will produce the page. If you prefer one array element per row (for readability or grouping), follow ’s suggestion and build each row with .=.
  • If you still see parsing errors, run php -l filename.php to lint the file and check the exact error line. Common culprits are stray semicolons, mismatched quotes, or unescaped quotes inside attribute values.
  • Remember checkboxes only submit when checked. If you need a value for every row, add a hidden field or normalize on the server.
  • Always escape output placed into HTML attributes (e.g., htmlspecialchars) and validate/cast IDs to avoid injection or broken markup.

This explains why the original wrapped line failed and gives a reliable pattern to generate inputs safely.

Recommended Answers

All 10 Replies

I am not quite sure what the red line of code is supposed to do.

$output[] = '<td>';
 echo "<input type='text' name='Value[{$row['id']}]' value='{$row['value']}'>";
$output[] = '</td>';
    $output[] = '<td>';
 echo "<input type='checkbox' name='Update[{$row['id']}]' value='Box #{$row['id']}'>"; 
$output[] = '</td>';

Is that what you were looking for, or did you want the whole thing in one string like this

$output[] = '<td>'."<input type='text' name='Value[{$row['id']}]' value='{$row['value']}'>".'</td>';
    $output[] = '<td>'."<input type='checkbox' name='Update[{$row['id']}]' value='Box #{$row['id']}'>".'</td>';

Both of these should compile, but whether or not they actually do what you want them to is another story.

commented: good eye +0

,

You are the captain of the ship. I kept using the echo to print the row id attached to the checkbox but as you pointed out, that was the syntax error. I am a beginner-to-inter PHP programmer. Thanks for pointing the error out. You are right, I am still working on seeing whether that code would work. I surmised that it would after reading about it here on another thread.

http://www.daniweb.com/web-development/php/threads/227698

Again, thanks

sb

,

the red line was supposed to be one line. it just wrapped over.

sb


Then why did you put a semi-colon in the middle of it? It seemed as though it was supposed to be 2 lines, the way you put it.

Anyway, is this thread solved?

,

>> Anyway, is this thread solved?

absolutely, the checkboxes and the textbox started showing up. I am putting code in to see whether they are getting the right values.

The whole thing below is supposed to be one line, like you pointed out. I don't know why it wrapped into two lines :-(

$output[] = '<td>' echo "<input type='text' name='Value[{$row['id']}]'value='{$row['value']}'>" '</td>';

The "echo" was the problem

Thanks

sb

You should not use '[]' by repeating in the loop. The '[]' will generate array room every time you assign it. You have 7 elements each time mysql_fetch looping. Specify the index for one looping. For example:

$count = 0;
while ($row = $result->fetch()) {
	$output[$count] = '<tr>';
	$output[$count] .= '<td>'.$row['manufacturer'].'</td>';
	$output[$count] .= '<td>'.$row['model'].'</td>';
	$output[$count] .= '<td>'.$row['description'].'</td>';
	$output[$count] .= '<td>&#x24;'.$row['price'].'</td>';

	$output[$count] .= '<td><input type="text" name="Value[{'.$row['id'].'}]" value="{'.$row['value'].'}"></td>';
    $output[$count] .= '<td><input type="checkbox" name="Update[{'.$row['id'].'}]" value="Box #{'.$row['id'].'}"></td>';

	$output[$count] .= '<td><a href="cart.php?action=add&id='.$row['id'].'&product='.$product.'">Add to list</a><td>';
	$output[$count] .= '</tr>';
        $count++;
}

Don't forget to put '.' to assign new value to the array element. If not, the last will override the first one and so on. You can easily check the array with 'var_dump($input)', and ensure that your data entered in the array.

Can you mark this thread as solved? I'm a bit pedantic that way. Thanks.

done. thanks

@zero13,

thanks for the response. I am a little confused about using the '[]' syntax. I thought that it created space only when required at the end of the array for more elements. Am I wrong in understanding that? thanks

sb

it works like this.

for($array as $value){
   $newarray[] = "1";
   $newarray[] = "2";
}// that is the same as
for($array as $value){
   $newarray[$counter] ="1";
   $counter++
   $newarray[$counter] = "2";
   $counter++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.