XCalibre3 Posted April 22, 2023 Share Posted April 22, 2023 When I first go to do a search, it shows the input box to insert your search, but then it also shows: # Name Email Phone Reservation # I don't want that showing until after I do the search. How can I do that? Here is my code. Thank you for any help. <?php $servername='localhost'; $username="root"; $password=""; try { $con=new PDO("mysql:host=$servername;dbname=calendar",$username,$password); $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //echo 'connected'; } catch(PDOException $e) { echo '<br>'.$e->getMessage(); } ?> <?php $searchErr = ''; $customer_details=''; if(isset($_POST['save'])) { if(!empty($_POST['search'])) { $search = $_POST['search']; $stmt = $con->prepare("select * from reservations where fname like '%$search%' or lname like '%$search%' or resnum like '%$search%' or email like '%$search%' or phone like '%$search%'"); $stmt->execute(); $customer_details = $stmt->fetchAll(PDO::FETCH_ASSOC); //print_r($customer_details); } else { $searchErr = "Please enter the information"; } } ?> <html> <head> <link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous"> <style> .container{ width:70%; height:30%; padding:20px; } </style> </head> <body> <div class="container"> <h3><u>Navarre Beach Scuba Name and Reservation Search</u></h3> <br/><br/> <form class="form-horizontal" action="#" method="post"> <div class="row"> <div class="form-group"> <label class="control-label col-sm-4" for="email"><b>Search Customer Reservations:</b>:</label> <div class="col-sm-4"> <input type="text" class="form-control" name="search" placeholder="search here"> </div> <div class="col-sm-2"> <button type="submit" name="save" class="btn btn-success btn-sm">Submit</button> </div> </div> <div class="form-group"> <span class="error" style="color:red;">* <?php echo $searchErr;?></span> </div> </div> </form> <h3><u>Search Result</u></h3><br/> <div class="table-responsive"> <table class="table"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Reservation #</th> </tr> </thead> <tbody> <?php if(!$customer_details) { } else{ foreach($customer_details as $key=>$value) { ?> <tr> <td><?php echo $key+1;?>  </td> <td><?php echo $value['fname'];?> <?php echo $value['lname'];?></td> <td>    <?php echo $value['email'];?></td> <td>    <?php echo $value['phone'];?></td> <td>    <?php echo $value['resnum'];?></td> </tr> <?php } } ?> </tbody> </table> </div> </div> <script src="jquery-3.2.1.min.js"></script> <script src="bootstrap.min.js"></script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted April 22, 2023 Solution Share Posted April 22, 2023 conditional statements exist in programming languages so that you can write code that makes decisions when it runs, such as only outputting markup when there's data to display. the existing code has a conditional test in it to only output the markup inside the tbody if there is data to display. wouldn't the answer to your question be to move the start of that conditional test so that it only outputs the table/thead markup when there is data to display? Quote Link to comment Share on other sites More sharing options...
XCalibre3 Posted April 22, 2023 Author Share Posted April 22, 2023 4 hours ago, mac_gyver said: conditional statements exist in programming languages so that you can write code that makes decisions when it runs, such as only outputting markup when there's data to display. the existing code has a conditional test in it to only output the markup inside the tbody if there is data to display. wouldn't the answer to your question be to move the start of that conditional test so that it only outputs the table/thead markup when there is data to display? I fixed it by moving: <?php $servername='localhost'; $username="root"; $password=""; try { $con=new PDO("mysql:host=$servername;dbname=calendar",$username,$password); $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //echo 'connected'; } catch(PDOException $e) { echo ''.$e->getMessage(); } ?> <?php $searchErr = ''; $customer_details=''; if(isset($_POST['save'])) { if(!empty($_POST['search'])) { $search = $_POST['search']; $stmt = $con->prepare("select * from reservations where fname like '%$search%' or lname like '%$search%' or resnum like '%$search%' or email like '%$search%' or phone like '%$search%'"); $stmt->execute(); $customer_details = $stmt->fetchAll(PDO::FETCH_ASSOC); //print_r($customer_details); ?> <div class="table-responsive"> <table class="table"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Reservation #</th> </tr> </thead> <tbody> <?php if(!$customer_details) { } else{ foreach($customer_details as $key=>$value) { ?> <tr> <td><?php echo $key+1;?>  </td> <td><?php echo $value['fname'];?> <?php echo $value['lname'];?></td> <td>    <?php echo $value['email'];?></td> <td>    <?php echo $value['phone'];?></td> <td>    <?php echo $value['resnum'];?></td> </tr> <?php } } ?> <?php } else { $searchErr = "Please enter the information"; } } ?> <html> <head> <link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous"> <style> .container{ width:70%; height:20%; padding:20px; } </style> </head> <body> <div class="container"> <h3><u>Navarre Beach Scuba Name and Reservation Search</u></h3> <form class="form-horizontal" action="#" method="post"> <div class="row"> <div class="form-group"> <label class="control-label col-sm-4" for="email"><b>Search Customer Reservations:</b>:</label> <div class="col-sm-4"> <input type="text" class="form-control" name="search" placeholder="search here"> </div> <div class="col-sm-2"> <button type="submit" name="save" class="btn btn-success btn-sm">Submit</button> </div> </div> <div class="form-group"> <span class="error" style="color:red;">* <?php echo $searchErr;?></span> </div> </div> </form> </tbody> </table> </div> </div> <script src="jquery-3.2.1.min.js"></script> <script src="bootstrap.min.js"></script> </body> </html> lol, probably the easiest reply I've had so far... and I knew this but was up 18 hours working on this and just didn't think. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.