-2

I am using echo inside the HTML tr tag where I am getting an error.

Here is my code

index.php

<?php
$i=0;
    while($row=mysql_fetch_array($ros))
    {
    if($i%2==0)
$classname="evenRow";
else
$classname="oddRow";
echo '<tr class="id" >';
echo '<tr class="'echo $classname'">';
?>

I am getting following error:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in E:\xampp\htdocs\pagination\index.php on line 64

Where am I going wrong and how can I achieve my desired output?

Thanks in advance

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
shakti sharma
  • 47
  • 1
  • 2
  • 10

5 Answers5

3

Just do this. Don't echo twice!

echo '<tr class=" '. $classname .' ">';
John Robertson
  • 1,486
  • 13
  • 16
1

The problem is not that you are inside a table row, but that you are inside a PHP string, and the answer is: You don't.

You either:

  • Interpolate your variable
  • Concatenate your variable
  • Don't use echo and a string for the outside output

Such:

<?php
$i=0;
while($row=mysql_fetch_array($ros)) {
    if($i%2==0) {
        $classname="evenRow";
    } else {
        $classname="oddRow";
?>
<tr class="id">
    <tr class="<?php echo $classname; ?>">
<?php
    }
# ...

NB: You appear to be trying to nest table rows, which isn't allowed.

You can probably dispense with the odd/even class names and just use :nth-child(odd) and :nth-child(even) in your stylesheet.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Change to this

<?php
$i=0;
    while($row=mysql_fetch_array($ros))
    {
    if($i%2==0)
$classname="evenRow";
else
$classname="oddRow";
echo '<tr class="id" >';
echo '<tr class="'.$classname.'">';
?>
arunrc
  • 628
  • 2
  • 14
  • 26
0
<?php
$i=0;
while($row=mysql_fetch_array($ros))
{
    if($i%2==0)
        $classname="evenRow";
    else
        $classname="oddRow";

    echo "<tr class='id'>";
    echo "<tr class=".$classname.">";
}
?>
Sal00m
  • 2,938
  • 3
  • 22
  • 33
Klapsius
  • 3,273
  • 6
  • 33
  • 56
-1

You can not use echo inside an echo

echo '<tr class="'echo $classname'">';

Use it like this

echo '<tr class="'.$classname.'">';
Raja
  • 851
  • 9
  • 22