Jump to content

Help with hiding search titles


XCalibre3
Go to solution Solved by mac_gyver,

Recommended Posts

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;?>&nbsp&nbsp</td>
		    	 		<td><?php echo $value['fname'];?>&nbsp<?php echo $value['lname'];?></td>
		    	 		<td>&nbsp&nbsp&nbsp&nbsp<?php echo $value['email'];?></td>
		    	 		<td>&nbsp&nbsp&nbsp&nbsp<?php echo $value['phone'];?></td>
		    	 		<td>&nbsp&nbsp&nbsp&nbsp<?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>

 

Link to comment
Share on other sites

  • Solution

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?

Link to comment
Share on other sites

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;?>&nbsp&nbsp</td>
								<td><?php echo $value['fname'];?>&nbsp<?php echo $value['lname'];?></td>
								<td>&nbsp&nbsp&nbsp&nbsp<?php echo $value['email'];?></td>
								<td>&nbsp&nbsp&nbsp&nbsp<?php echo $value['phone'];?></td>
								<td>&nbsp&nbsp&nbsp&nbsp<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.