beny lim
beny lim

Reputation: 1304

Line break in table cell PHP

I am trying to perform several line break (br) in my table cell. But I have problem with the syntax.

Below is my code to perform line break (br) within a single cell, but it doesn't work. How should I go about doing it?

echo("<tr><td>".$row[0]. 
                <br/> 
                $row[1]. 
                <br/> $row[2]. 
                <br/> 
                "<img src=images/".$row["skuCode"]."_1.jpg>".
                <br/>
                <a href=viewProductItem.php?skuCode=".$row[0].">View</a>".
                 "</td></tr>");

Upvotes: 1

Views: 3382

Answers (5)

Nir Alfasi
Nir Alfasi

Reputation: 53565

You forgot the quotes around the <br/>s , try:

echo("<tr><td>".$row[0]. 
                "<br/>" .
                $row[1]. 
                "<br/>". $row[2]. 
                "<br/>" .
                "<img src=images/".$row["skuCode"]."_1.jpg>".
                "<br/>".
                "<a href=viewProductItem.php?skuCode=".$row[0].">View</a>".
                 "</td></tr>");

Or alternatively:

echo("<tr><td>{$row[0]} 
                <br/> 
                {$row[1]} 
                <br/> {$row[2]} 
                <br/> 
                <img src=images/.$row[skuCode]._1.jpg>
                <br/>
                <a href=viewProductItem.php?skuCode={$row[0]}>View</a>.
                 </td></tr>");

Upvotes: 3

If you want to make its HTML output well-formatted, then you could check this:

echo "<tr>\n";
echo "  <td>" . $row[0] . "<br />\n";
echo "  " . $row[1] . "<br />\n";
echo "  " . $row[2] . "<br />\n";
echo '  <img src="images/' . $row["skuCode"] . '_1.jpg"><br />' . "\n";
echo '  <a href="viewProductItem.php?skuCode=' . $row[0] . '">View</a></td><br />' . "\n";
echo "</tr>\n";

Or this:

echo "<tr>\n";
echo "  <td>" . $row[0] . "<br />\n";
echo "  " . $row[1] . "<br />\n";
echo "  " . $row[2] . "<br />\n";
echo "  <img src=images/" . $row["skuCode"] . "_1.jpg><br />\n";
echo "  <a href=viewProductItem.php?skuCode=" . $row[0] . ">View</a></td><br />\n"
echo "</tr>\n";

Or if you did not like those, look at this:

echo("<tr><td>" . $row[0] . "\n" .
                "<br />\n" .
                $row[1] . "\n" .
                "<br />" . $row[2] . "\n" .
                "<br />\n" .
                "<img src=images/" . $row["skuCode"] . "_1.jpg>\n" .
                "<br />\n" .
                "<a href=viewProductItem.php?skuCode=" . $row[0] . ">View</a>\n" .
                 "</td></tr>\n");

Upvotes: 0

Joel Murphy
Joel Murphy

Reputation: 2512

You forgot to open and close the quotes in your string:

<?php

echo "<tr><td>".$row[0]."<br/>".$row[1]."<br/>".$row[2]."<br/><img src=images/".$row["skuCode"]."_1.jpg><br/><a href=viewProductItem.php?skuCode=".$row[0].">View</a></td></tr>";

?>

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191809

You're not using quotes properly, but I think it would be much easier to use a heredoc.

echo <<<HTML
    <tr><td>$row[0]<br/>$row[1]<br/>$row[2]<br/>
    <img src=images/$row[skuCode]_1.jpg<br/>
    <a href=viewProductItem.php?skuCode=$row[0]>View</a>
    </td></tr>
HTML;

Upvotes: 5

francisco.preller
francisco.preller

Reputation: 6639

Try this:

echo "<tr><td>{$row[0]}<br/>{$row[1]}<br/>{$row[2]}<br/><img src=\"images/{$row['skuCode']}_1.jpg\"><br/><a href=viewProductItem.php?skuCode=\"{$row[0]}\">View</a></td></tr>";

You forgot to wrap the br tags around brackets, also you can use curly braces to never have to exit out of your string and concat it in php

echo "String with $variable works";
echo "String with {$array['key']} works with curly bracers."

Upvotes: 1

Related Questions