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>'";


    }
}   
?>
7
  • 1
    there is an ordering issue in your code ... Commented Jun 9, 2016 at 19:30
  • 4
    WARNING: If you're just learning PHP, please, do not use the mysql_query interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way explains best practices. Your user parameters are not properly escaped and there are SQL injection bugs that can be exploited. Commented Jun 9, 2016 at 19:30
  • Do I have to do the html after the php? Commented Jun 9, 2016 at 19:31
  • 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. Commented Jun 9, 2016 at 19:32
  • 1
    stackoverflow.com/questions/2970936/… Commented Jun 9, 2016 at 19:40

1 Answer 1

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.

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

Comments

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.