0

I am not sure what I am missing. The page loads with the table but does not pull any data into the rows. I connected to the database and have pulled data before I edited the code to this. And the first line does show the 9 rows I have.

<?php if (mysql_num_rows($result) > 0) : ?>
        <?php

        print '<table class="col-md-12">
            <tr>
                <th class="col-md-1">Category</th>
                <th class="col-md-2">Subcategory</th>
            </tr>
 <tr>'.$rowsfortable.'
</tr>
</table>';
//Get Category.


while ($row = mysql_fetch_array($result)) {
            $qry = "SELECT * FROM general_category WHERE categoryID = {$row['categoryID']}";
            $tmpResult = qry($qry);
            $tmpRow = mysql_fetch_array($tmpResult);
            $category = $tmpRow['category_desc'];

            //Get Sub Category.
            $qry = "SELECT * FROM general_category WHERE categoryID = {$row['sub_categoryID']}";
            $tmpResult = qry($qry);
            if (mysql_num_rows($tmpResult) > 0) {
                $tmpRow = mysql_fetch_array($tmpResult);
                $sub_category = "/ " . $tmpRow['category_desc'];
                } else {
                    $sub_category = '';
                }
 for ($i=0; $i < count($row); $i++) {
            $rowsfortable = "print '<td>' '.$category.' '.$sub_category.'</td>'";


    }
}   
?>
DDJ
  • 807
  • 5
  • 13
  • 31
  • 1
    there is an ordering issue in your code ... – cmorrissey Jun 09 '16 at 19:30
  • 4
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Jun 09 '16 at 19:30
  • Do I have to do the html after the php? – DDJ Jun 09 '16 at 19:31
  • I wanted to make it work first, then convert it to pdo. – DDJ Jun 09 '16 at 19:32
  • 1
    It'd save you a lot of time just to do it properly first, with either MySQLi or PDO - doesn't matter too much as long as you use prepared statements with placeholders. Neither is hard to learn. – Qirel Jun 09 '16 at 19:32
  • I am redo pages of code one section at a time, but for some reason my mind is missing the structure – DDJ Jun 09 '16 at 19:34
  • 1
    http://stackoverflow.com/questions/2970936/how-to-echo-out-table-rows-from-the-db-php – SML Jun 09 '16 at 19:40

1 Answers1

1

It looks to me like you're printing out your table before $rowsfortable is ever assigned any value. PHP will execute in order.

Try moving the block of code that grabs your data and assigns it to $rowsfortable before the block that actually prints the table.

thatrandomguy
  • 56
  • 1
  • 6