-1

I need to display rows within a table using php variables and a while loop and I can't figure out what I am doing wrong. I have tried all kinds of different combinations of single quotes and double quotes, but I still can't use the correct syntax needed to make these rows get outputted within the table without generating any errors. I am using Dreamweaver 2019 to code it with. I used Netbeans 8.2 but I still can't figure out the correct syntax for this code.

Here's also what I found on stackoverflow so far but I am still not finding exactly what I need. And I can't find exactly how to use the correct syntax using google either within this context:

php - for loop inside a while loop, correct syntax?

Inline Styling in PHP

https://stackoverflow.com/search?q=use+inline+styling+with+php+html+table

html tables & inline styles

HTML Table with inline PHP

<?php 

    include 'connection.php'; // includes the connection.php file to connect 
    to the database

    // query to database and prepares and executes
    $query = "SELECT id, first_name, last_name FROM customers ORDER BY 
    last_name asc";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($id, $first_name, $last_name);

    // count the number of customers
    $total_customers = $stmt->num_rows;

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>

<!--start container to center design in browser-->
<div class="container">

    <div class="row" style="margin-bottom:30px">
        <div class="col-xs-12">
            <ul class="nav nav-pills">
              <li class="nav-item">
                <a class="nav-link" href="index.php">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="customers.php">Customers</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="">Search</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="">Add New</a>
              </li>
            </ul>
        </div>
    </div>


<div class="row">

    <div class="col-xs-12">
      <table class="table table-bordered table-striped table-hover">
           <p><strong>There are a total of <?PHP echo $total_customers; ?> 
           customers.</strong></p> <!-- output total number of customers -->
              <thead>
                <tr class="success">
                  <th class="text-center">#</th>
                  <th>Customer ID</th>
                  <th>Last Name</th>
                  <th>First name</th>
                  <th class="text-center">Details</th>
                </tr>
              </thead>
              <tbody>           

               <tr>
               <?php   

                 while($result = $stmt->fetch()) {                   
                  echo '<th class="text-center">'#'</th>'
                  echo  <th>"$result"</th>
                  echo <th>"$result"</th>
                  echo <th>"$result"</th>
                  echo <th class="text-center">"Details"</th>
                  }

                ?>
                  </tr>
              </tbody>
            </table>
    </div>

</div>

</div>
<!--end container to center design in browser--> 

</body>

</html>
6
  • 3
    some semi colons would seems like a good start Commented Nov 27, 2018 at 11:30
  • Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); Commented Nov 27, 2018 at 11:31
  • 2
    This line looks a bit odd! echo '<th class="text-center">'#'</th>'S What do you actually want output from that ?? Commented Nov 27, 2018 at 11:32
  • Nobody reported the issue there? ... ?>[newline][newline]<!doctype html> Commented Nov 27, 2018 at 11:36
  • Thank you @RiggsFolly for the suggestions of using semi colons, and especially using error reporting! Accidentally typed in a S after the first echo statment :-) Commented Nov 27, 2018 at 11:44

5 Answers 5

1

Because you have used $stmt->bind_result($id, $first_name, $last_name); then the columns you select will be returned by $stmt->fetch() into variables called $id, $first_name, $last_name

Also remember each echo is a distinct statement and should end with a ;

Also note that when using double quoted string literals your variables will get expanded automatically. Also inside a double quoted literal you can use single quotes without it causing you issues with early termination of the literal. That also applies to using double quotes inside a single quoted literal.

while($stmt->fetch()) {                   
    echo "<th class='text-center'>'#'</th>";
    echo "<th>$id</th>";
    echo "<th>$first_name</th>";
    echo "<th>$last_name</th>";
    echo "<th class='text-center'>Details</th>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again, @riggsfolly! Very helpful comments also! I'm printing this page out for all of you guys information to review and learn for all of the very helpful comments and code.
0

Better approach would be a step closer to a template-like solution.

<tbody>   
<?php while($result = $stmt->fetch()): ?>        
<tr>
    <td clas="text-center">#</td>
    <td><?php echo $id; ?> </td>
    <td><?php echo $first_name; ?> </td>
    <td><?php echo $last_name; ?> </td>
    <td class="text-center">Details</td>
</tr>
<?php endwhile; ?>

1 Comment

Thank you @unamatasanatari! That was very helpful for me also! Just about ready to print this all out to review and learn while at work today :-)
0

First you need to understand how to use the loop. Your code within the loop is not correct. There are many quotes issues. Moreover, as per your code I believe you are fetching the table and not using the columns in the loop, something like $result['id'] or $result->id

You also may need to understand how PHP Concatenation works. PHP allows single quote ' and double quotes " for string. If you use the same code to wrap the variable than you will have to escape it by using backslash \.

Try below code, that may resolve your issue.

<?php
    while($result = $stmt->fetch()) {    

        echo '<th class="text-center">#</th>';
        echo '<th>' . $id . '</th>';
        echo '<th>' . $first_name . '</th>';
        echo '<th>' . $last_name . '</th>';     
        echo '<th class="text-center">Details</th>';

    }
?>

Additionally, you should check this to understand PHP string and concatenation

Comments

0

try using below code

write this way '$id, $last_name, $firs_name

<?php   

while($result = $stmt->fetch()) {                   
    echo '<th class="text-center">#</th>';
    echo  '<th>'.$id.'</th>';
    echo '<th>'.$lastt_name.'</th>';
    echo '<th>'.$first_name.'</th>';
    echo '<th class="text-center">Details/th>';
}

?>

2 Comments

Nudge OP used $stmt->bind_result($id, $first_name, $last_name); So that is not an object that will be returned
Thank you for the code @bhargavchudasama! I am having trouble following you exactly with writing this code: '.$result->id if object else $result['id'] if array within this while loop. How exactly can I do that without getting this error after posting in the above code? Notice: Trying to get property 'id' of non-object in C:\wamp64\www\seanvzwebguy\A10\customers.php on line 75
0

Try using <?=$string; ?>, like this:

<?php while($result = $stmt->fetch()): ?>        
<tr>
    <td clas="text-center">#</td>
    <td><?=$id; ?> </td>
    <td><?=$first_name; ?> </td>
    <td><?=$last_name; ?> </td>
    <td class="text-center">Details</td>
</tr>
<?php endwhile; ?>

Using less php in your html keeps it clean.

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.