-1

I want to style the following mysqli output:

while($row = mysqli_fetch_array($result)) {
    $date = $row['name'];
    $comment = $row['id'];
    $amount = $row['icon'];
    echo 
"<tr><td style='width: 600px;'>".$date."</td></tr>
<tr><td style='width: 600px;'>".$comment."</td></tr>
<tr><td>".$amount."</td>
</tr>";
} 

echo "</table>";

I want to use for the <td style='width: 600px;'> a css class. If I use the following code, my mysql data disappears. Why?

<tr><td class="links">".$date."</td></tr>
wogsland
  • 9,106
  • 19
  • 57
  • 93
Hertus
  • 147
  • 2
  • 11
  • 1
    This is not the complete code. This being in an `echo` and since you aren't really using `"` for the variable, you could change to `echo ''.$date.';'`. Also, that wouldn't just dissapear. It would probably give you an erro or warning. If you are using even notepadd++, you would be abe to see that the `"links"` part is actually closing the quote from the `echo` – FirstOne Dec 24 '15 at 13:32
  • 1
    Where is `".$date."` in your code? – chris85 Dec 24 '15 at 13:38

3 Answers3

2

What about, or am I saying something weird .. ?

<tr><td class='links'>".$date."</td></tr>

You need to escape the "", so it won't be interpreted as end of te echo. Use can also use \ to escape it. My way just solves it by switching the "" from links to ''.

Tredged
  • 586
  • 2
  • 17
1

Your code is wrong at

<tr><td class="links">".$date."</td></tr>

Should be

<tr><td class='links'>".$date."</td></tr>

If your echo is between quotes ",you must put the HTML stuff inside apostrophes '.

Also you won't need to chain the variables, since you are using quotes, so you can do it as follows:

<tr><td class='links'>$date</td></tr>

Here you can see a full text explaining the differences:

What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Phiter
  • 14,570
  • 14
  • 50
  • 84
1

conver THIS

echo 
"<tr><td style='width: 600px;'>".$date."</td></tr>
<tr><td style='width: 600px;'>".$comment."</td></tr>
<tr><td>".$amount."</td>
</tr>";

if you start the echo statement with double quotation you need to use single in between like this

convert it TO

echo "<tr><td class='links'>".$date."</td></tr>"
."<tr><td class='links'>".$comment."</td></tr>"
."<tr><td>".$amount."</td>"
."</tr>";

OR you could right the html code out of your php code like this

?>
<tr><td class='links'><?php echo $date; ?></td></tr>
<tr><td class='links'><?php echo $comment; ?></td></tr>
<tr><td><?php echo $amount; ?></td></tr>
<?php
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30