-6

I have this code and it get something weird just after the title they are ()()()(). I been looking for this and I still don't have idea how to fix it. Thank you in advance.

<html>
    <head>
        <title>VER PRECIOS DE DESTINOS</title>
        <style>
            table,th,td
            { 
                font-family: Tahoma;
                border:1px solid black;
                border-collapse:collapse;
            }
            th,td
            {
                padding:5px;
            }
            th
            {
                text-align:left;
            }
        </style>
    </head>
    <body>
        <span style="font-family: Tahoma; font-size: 45.0px; font-weight: bold;  ">
            VER PRECIOS DE DESTINOS
        </span> 
        <table style="width:300px">
            <tr>
                <th>DESTINO</span</th>
                <th>PRECIO</th>     
            </tr>
            <tr><?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("Destinos") or die(mysql_error());
$order = "SELECT * FROM Destinos ORDER BY Destino";
$result = mysql_query($order);
while($data = mysql_fetch_row($result)){
     print "(<tr><td>$data[0]</td>)"; 
     print "(<td>$$data[1]</td>)";
    }
    ?>
        </table>
            </tr>
    </body>
</html>
pixanim
  • 11
  • 3

6 Answers6

5

The problem is that TABLE/TR/TH do not allow mixed content between the elements.

As such, the final HTML might look something like the following which is invalid:

<tr>(<td>..</td)(<td>..</td)(<td>..</td)</tr>

The browser does it's best to handle this situation and "moves" the ()()() parenthesis up, outside of the TABLE - as they are outside the TD elements. The parenthesis should probably just be omitted from the output or include inside the TD elements.

user2864740
  • 60,010
  • 15
  • 145
  • 220
3

When including content in a table that is outside the tr/td tags, that content is displayed outside of your table.

The problem is with the parens here:

 print "(<tr><td>$data[0]</td>)"; 
 print "(<td>$$data[1]</td>)";

Probably just remove them and you should be good.

See this Fiddle

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
1

Please Try the following, let me know if it helps ..

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("Destinos") or die(mysql_error());
$order = "SELECT * FROM Destinos ORDER BY Destino";
$result = mysql_query($order); 
?>



 <table style="width:300px">
        <tr>
            <th>DESTINO</span</th>
            <th>PRECIO</th>     
        </tr>
        <? while($data = mysql_fetch_row($result)){ ?>
        <tr>
            <td> <?=$data[0]?> </td>
            <td> <?=$data[1]?> </td>
        </tr> <?}?>
 </table>

If you need the brackets around the value try the following ..

 <table style="width:300px">
        <tr>
            <th>DESTINO</span</th>
            <th>PRECIO</th>     
        </tr>
        <? while($data = mysql_fetch_row($result)){ ?>
        <tr>
            <td> (<?=$data[0]?>) </td>
            <td> (<?=$data[1]?>) </td>
        </tr> <?}?>
 </table>
m82amjad
  • 248
  • 2
  • 9
1

You don't have correct HTML structure

  • Assuming $data[0] is 'foo', print "(<tr><td>$data[0]</td>)"; will output (<tr><td>foo</td>. This is not valid HTML.
  • You have a closing tag for table row (</tr>) after the closing tag of table (</table>) which not only is invalid HTML, but does not make sense.

Do not mix up HTML structure with business logic

  • Try separating your logic from structure
  • Don't print the whole HTML with prints/echos. PHP was designed to be a template creator (which is a sad affair on it's own, not suitable for an answer). So print just what needed to be printed dynamically.

    <body>
        <?php
            mysql_connect("localhost", "user", "password") or die(mysql_error());
            mysql_select_db("Destinos") or die(mysql_error());
            $order = "SELECT * FROM Destinos ORDER BY Destino";
            $result = mysql_query($order);
        ?>
        <span style="font-family: Tahoma; font-size: 45.0px; font-weight: bold;  ">
            VER PRECIOS DE DESTINOS
        </span> 
        <table style="width:300px">
            <tr>
                <th>DESTINO</span</th>
                <th>PRECIO</th>     
            </tr>
            <?php
            while ($data = mysql_fetch_row($result)) {
            ?>
                <tr>
                <td><?php echo $data[0]; ?></td>
                <td><?php echo $data[1]; ?></td>
                </tr>
            ?php
            }
            ?>
        </table>
    </body>
    

    Now, it is clear that you are trying to create multiple rows inside a PHP while loop. Also <td><?php echo $data[0]; ?></td> can be clearly interpreted as: make a table cell, but it's content should be whatever the value of $data[0].

Other

  • You have two $ in this statement: print "(<td>$$data[1]</td>)";.
sampathsris
  • 21,564
  • 12
  • 71
  • 98
0

Try this out.And let me know if it works.

<html>
<head>
    <title>VER PRECIOS DE DESTINOS</title>
    <style>
        table,th,td
        { 
            font-family: Tahoma;
            border:1px solid black;
            border-collapse:collapse;
        }
        th,td
        {
            padding:5px;
        }
        th
        {
            text-align:left;
        }
    </style>
</head>
<body>
    <span style="font-family: Tahoma; font-size: 45.0px; font-weight: bold;  ">
        VER PRECIOS DE DESTINOS
    </span> 
    <table style="width:300px">
        <tr>
            <th>DESTINO</th>
            <th>PRECIO</th>     
        </tr>
        <tr><?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("Destinos") or die(mysql_error());
$order = "SELECT * FROM Destinos ORDER BY Destino";
$result = mysql_query($order);
while($data = mysql_fetch_row($result)){

?>
<tr><td><?php
echo $data[0]; ?> </td><td><?php
echo $data[1]; ?> </td></tr> <?php
}
?>
    </table>

</body>
</html>
-1
 <table style="width:300px">
        <tr>
            <th>DESTINO</span</th>
            <th>PRECIO</th>     
        </tr>
        <tr><?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("Destinos") or die(mysql_error());
$order = "SELECT * FROM Destinos ORDER BY Destino";
$result = mysql_query($order);
//while($data = mysql_fetch_row($result)){
//     print "(<tr><td>$data[0]</td>)"; 
//     print "(<td>$$data[1]</td>)";
//    }
?>
<td></td>
<td></td>
</tr>
    </table> 

try comment out, i am suspecting semantic issue

SiGaban
  • 91
  • 1
  • 14