tags:

views:

45

answers:

1

hello, for some reason the below code is outputting the correct ticker in the location of LPrice for all items, but only outputting the correct data for the second data element for PCT and PNL. Meaning, row 1 output only populating in one section, while row 2 populating in all correct sections. Note: there are currently only 2 elements in the table.

<?php
$host="localhost"; // Host name 
$username="abc"; // Mysql username 
$password="password"; // Mysql password 
$db_name="abc"; // Database name 
$tbl_name="portfolio"; // Table name
$conn=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="select * from ".$tbl_name.";";
$result = mysql_query($sql) or die(mysql_error()); 
echo '<table class="tickerContain">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>TICKER</td>';
echo '<td>PRICE</td>';
echo '<td>CommIn</td>';
echo '<td>CommOut</td>';
echo '<td>DateIn</td>';
echo '<td>LPrice</td>';
echo '<td>%CHG</td>';
echo '<td>PNL</td>';

$tblOut='';
while ($row = mysql_fetch_array($result))
{
  $tick='';
  $tick=$row["ticker"];
  $tblOut.= '<tr>';
  $tblOut.= '<td id="'.$tick.'id">' . $row["id"] . '</td>';
  $tblOut.= '<td id="'.$tick.'ticker">' . $tick . '</td>';
  $tblOut.= '<td id="'.$tick.'price">' . $row["price"] . '</td>';
  $tblOut.= '<td id="'.$tick.'commissionIn">' . $row["commissionIn"] . '</td>';
  $tblOut.= '<td id="'.$tick.'commissionOut">' . $row["commissionOut"] . '</td>';
  $tblOut.= '<td id="'.$tick.'dateIn">' . $row["dateIn"] . '</td>';
  $tblOut.= '<td><textarea class="realTime"  id="'.$tick.'LPrice">'.$tick.'</textarea></td>';
  $tblOut.= '<td><textarea class="realTime"  id="'.$tick.'pctChange">'.$tick.'</textarea></td>';
  $tblOut.= '<td><textarea class="realTime"  id="'.$tick.'pnl">'.$tick.'</textarea></td>';
  $tblOut.= '</tr>';
}
echo $tblOut;
echo '</table>';
+2  A: 

Your while loop was closing the <tr> tag above it on every iteration, but there's no way you could have seen that with that totally unreadable code.

I took the liberty of tidying it up for you:

You also had some entirely useless variables assigned. I fixed that too.

<?php
$host="localhost"; // Host name 
$username="abc"; // Mysql username 
$password="password"; // Mysql password 
$db_name="abc"; // Database name 
$tbl_name="portfolio"; // Table name
$conn=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="select * from ".$tbl_name.";";
$result = mysql_query($sql) or die(mysql_error());

?>

<table class="tickerContain">
    <tr>
        <td>ID</td>
        <td>TICKER</td>
        <td>PRICE</td>
        <td>CommIn</td>
        <td>CommOut</td>
        <td>DateIn</td>
        <td>LPrice</td>
        <td>%CHG</td>
        <td>PNL</td>

    </tr>
    <?php while ($row = mysql_fetch_array($result)) : ?>
    <tr>
        <td id="<?php echo $row["ticker"]; ?>id">
            <?php echo $row["id"]; ?>
        </td>
        <td id="<?php echo $row["ticker"]; ?>ticker">
            <?php echo $row["ticker"]; ?>
        </td>
        <td id="<?php echo $row["ticker"]; ?>price">
            <?php echo $row["price"]; ?>
        </td>
        <td id="<?php echo $row["ticker"]; ?>commissionIn">
            <?php echo $row["commissionIn"]; ?>
        </td>
        <td id="<?php echo $row["ticker"]; ?>commissionOut">
            <?php echo $row["commissionOut"]; ?>
        </td>
        <td id="<?php echo $row["ticker"]; ?>dateIn">
            <?php echo $row["dateIn"]; ?>
        </td>
        <td>
            <textarea class="realTime" id="<?php echo $row["ticker"]; ?>LPrice">
                <?php echo $row["ticker"]; ?>
            </textarea>
        </td>
        <td>
            <textarea class="realTime" id="<?php echo $row["ticker"]; ?>pctChange">
                <?php echo $row["ticker"]; ?>
            </textarea>
        </td>
        <td>
            <textarea class="realTime" id="<?php echo $row["ticker"]; ?>pnl">
                <?php echo $row["ticker"]; ?>
            </textarea>
        </td>
    </tr>
    <?php endwhile; ?>
</table>

Additionally, I would suggest moving your database code to another file, and including it into your scripts.

Take a look at MVC Architecture (http://en.wikipedia.org/wiki/Model–view–controller), it will significantly help you keep your code clean.

jordanstephens
@meagar how would you suggest improving it?
jordanstephens
@jordanstephens Combining all the `<td>` lines was a big help, but I wouldn't bother with all the context switching inside the while loop, and the alternate control structure syntax (`while :`/`endwhile;`) is horrendous.
meagar
yes the tr close seems to be an issue. thank you all.
jason m
@meagar I think the context switching is far better than a million echo statements and concatenating strings. I would also be willing to bet that the context switching is faster in this case--although i haven't tested it. while the alternate syntax for the loop isn't ideal, i do feel like its better than `<?php while() { ?>` then fifty or so lines down having `<?php } ?>` which means almost nothing to me. `endwhile;` gives me more information at a glance.
jordanstephens