1

Is there a way to pass the variables in the while-loop at the bottom of the script into the table? When I run this in my browser no table is displaying. Any advice on how to solve this issue would be greatly appreciated, being new to html/php/sql I'm not sure how to fix this.

This is a front end page to my database for searching people records:

<?php
session_start();
include_once 'dbconnect.php';

if (!isset($_SESSION['userSession'])) {
    header("Location: index.php");
}

$query = $DBcon->query("SELECT * FROM tbl_users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$DBcon->close();

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>

<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen"> 
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen"> 

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand">Sparrowhawk</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li><a href="home.php">Home</a></li>
            <li><a href="vehicleindex.php">Vehicles</a></li>
            <li class="active"> <a href="crudindex2.php">Persons</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"><span class="glyphicon glyphicon-user"></span>&nbsp; <?php echo $userRow['username']; ?></a></li>
            <li><a href="logout.php?logout"><span class="glyphicon glyphicon-log-out"></span>&nbsp; Logout</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

<div class="container" style="margin-top:100px;text-align:center;font-family:Verdana, Geneva, sans-serif;font-size:12px;">
    <?php
include_once 'crud3.php';
?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Person Search</title>
<link rel="stylesheet" href="style2.css" type="text/css" />
</head>

<body>
<center>
<div id="header">
  <label></label>
</div>
<br />
<h1>Person Search</h1>
<br />
<div id="form">
<form method="post">
<table width="100%" border="1" cellpadding="15">
<tr>
<td><input type="text" name="q1" placeholder="Names / License Number" ;  ?></td>
</tr>
<tr>
<td>
<button type="submit" name="search" value="go">Search</button>
</td>
</tr>
</table>
</form>

<br></br>

<?php
    $conn = mysqli_connect("localhost", "root", "", "dbtest");

    if(mysqli_connect_errno()){
        echo "Failed to connect: " . mysqli_connect_error();
    }

    if(isset($_GET['q1']) && $_GET['q1'] !== ' '){
        $searchq = $_GET['q1'];

        $q = mysqli_query($conn, "SELECT * FROM people WHERE fn LIKE '%$searchq%'") or die(mysqli_error());
        $c = mysqli_num_rows($q);
        if($c == 0){
            $output = 'No search results for <b>"' . $searchq . '"</b>';
        } else {
            while($row = mysqli_fetch_array($q))
                $id = $row['id'];
                $fn = $row['fn'];
                $ln = $row['ln'];
                $pa = $row['People_address'];
                $pl = $row['People_licence'];
            }
        }

    } else {
        die(mysqli_error($conn));
    }
    mysqli_close($conn);
?> 

<table width="100%" border="1" cellpadding="15" align="center">
  <tr>
  <td><?php echo "$id"; ?></td>
  <td><?php echo "$fn"; ?></td>
  <td><?php echo "$ln"; ?></td>
</table>
</div>
</center>
</body>
</html>
</div>
</body>
</html>
2
  • Are you adding two <!DOCTYPE html> on the same page? Commented Jan 5, 2018 at 17:45
  • you must insert <tr> in loop and assign parameters for repeat rows. Commented Jan 5, 2018 at 17:46

4 Answers 4

5

You can close the PHP tags to alternate between HTML and PHP, that will allow you to do this:

<table width="100%" border="1" cellpadding="15" align="center">
    <?php while($row = mysqli_fetch_array($q))
    {

            $id = $row['id'];
            $fn = $row['fn'];
            $ln = $row['ln'];
            $pa = $row['People_address'];
            $pl = $row['People_licence'];
    ?>
        <tr>
            <td><?php echo "$id"; ?></td>
            <td><?php echo "$fn"; ?></td>
            <td><?php echo "$ln"; ?></td>
        </tr>
    <?php } ?>
</table>

Maybe this will be more readable:

<table width="100%" border="1" cellpadding="15" align="center">
<?php
    while($row = mysqli_fetch_array($q)):
        $id = $row['id'];
        $fn = $row['fn'];
        $ln = $row['ln'];
        $pa = $row['People_address'];
        $pl = $row['People_licence'];
?>
    <tr>
        <td><?php echo "$id"; ?></td>
        <td><?php echo "$fn"; ?></td>
        <td><?php echo "$ln"; ?></td>
    </tr>
<?php endwhile; ?>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Note, instead of doing <?php echo "$var"; ?> - you are actually using a lot of extra unneeded symbols. You don't need the quotes at all, so you can shorten that to <?php echo $var; ?> regardless of PHP version, but if you are on at least PHP version 5.4 the short echo tag is always available. The code could then be shortened to <?=$var;?>
This seemed like it would work, but the $q variable is returned on the webpage with an error as undeclared: Notice: Undefined variable: q in C:\wamp64\www\dbtest\crudindex3.php on line 99
1

Is there a way to pass the variables in the while-loop at the bottom of the script into the table?

You're looping and updating the variables without ever outputting them, and then you output a table with possibly no data.

What you should do is build the table within the while:

    $table = '<table>';

    ...

    } else {
        while($row = mysqli_fetch_array($q)) {
            $table .= <<<HTML_ROW
<tr>
    <td>{$row['id']}</td>
    <td>{$row['fn']}</td>
    <td>Other data</td>
</tr>
HTML_ROW;
        }
    }
    $table .= '</table>';

    ...

    print $table;

(Also, your while code as shown will not work as indented because you seem to have forgotten an opening brace)

Comments

0

you output the table as youre looping through:

<?php 
if(isset($_GET['q1']) && $_GET['q1'] !== ' '){
        $searchq = $_GET['q1'];

        $q = mysqli_query($conn, "SELECT * FROM people WHERE fn LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
?>

<?php if ($c == 0): ?>
  No search results for <b><?php echo $searchq ?></b>
<?php else: ?>
  <table>
    <?php while($row = mysqli_fetch_array($q)): ?>
       <tr>
         <td><?php echo $row['id'] ?></td>
         <!-- repeat for other fields -->
       </tr>
    <?php endwhile; ?>
  </table>

3 Comments

Hi there, thank you for the response. This method looks promising but is resulting in the following error: Parse error: syntax error, unexpected end of file in C:\wamp64\www\dbtest\crudindex3.php on line 111 Please see the way I implemented the code here: pastebin.com/defLaphK
What is line 111 in the code you are running? (the pastebin isnt that large).
there is a missing semicolon after $q = ... that could be it.
0

Remember, scoping of variable matters, the scope of below variable ends within else block.

$ln = $row['ln'];
$pa = $row['People_address'];
$pl = $row['People_licence'];

On the other hand, you were trying to access above variable after else block. So, could move table html to while loop

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.