0

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>';
8
  • Did you know that echo can display more than a few characters? That would drastically improve readability. Could you also add the contents of your table? Commented Sep 14, 2010 at 16:39
  • Choose one: echo '</td>', '<td>' or echo '</td>' . '<td>' or (best of all) echo '</td><td>'. Whichever way you choose, stop doing what you're doing. It's painful. Commented Sep 14, 2010 at 16:45
  • You're missing a </tr> right before $tblOut=''; Commented Sep 14, 2010 at 16:51
  • 1
    Whoa! I want to have a little chat with whoever taught you to write PHP like this... Commented Sep 14, 2010 at 16:52
  • 1) $tick=''; at the top of the loop doesn't accomplish anything as you immediately assign $row['ticker'] to it on the next line. 2) There's no reason to build your table contents inside $tblOut, just echo the contents directly. 3) None of your output is being escaped; this could be why some of it isn't appearing properly Commented Sep 14, 2010 at 16:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@meagar how would you suggest improving it?
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.