-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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
skatersean
  • 13
  • 4
  • 3
    some semi colons would seems like a good start – RiggsFolly Nov 27 '18 at 11:30
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Nov 27 '18 at 11:31
  • 2
    This line looks a bit odd! `echo ''#''S` What do you actually want output from that ?? – RiggsFolly Nov 27 '18 at 11:32
  • Nobody reported the issue there? `... ?>[newline][newline] ` – Cid Nov 27 '18 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 :-) – skatersean Nov 27 '18 at 11:44
  • Can anyone tell me why I got two negative downvotes when I did my best (took about 30-45 minutes) to research this question on google and stackoverflow (and read through all of the posts through google and stackoverflow) and tried all kinds of syntax rearranging in Dreamweaver and used Netbeans to try to debug the code? – skatersean Nov 27 '18 at 12:14

5 Answers5

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>";
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 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. – skatersean Nov 27 '18 at 12:06
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>';
}

?>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • 1
    _Nudge_ OP used `$stmt->bind_result($id, $first_name, $last_name);` So that is not an object that will be returned – RiggsFolly Nov 27 '18 at 11:36
  • 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 – skatersean Nov 27 '18 at 11:46
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; ?>
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
  • 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 :-) – skatersean Nov 27 '18 at 12:09
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

Code Lover
  • 8,099
  • 20
  • 84
  • 154
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.

tiezkoz
  • 41
  • 4