I have a table which Displays the book list from mysql Database and search form where user can search books.
I"m looking forward to show the book list as per title and author when user search for by input value of title and author and display " NO Books by name or author" as echo when there is no any record.
My code is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
Go Back
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
My Search form is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<form action = "DisplayBooks.php" method="get">
<br>
<center>Enter the title of the book to be searched :
<input type="text" name="search" size="48">
<br></br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</center>
<br>
</form>
</body>
</html>
But it sucessfully displays list of books but when there is no any record ..it don't launch echo.
ps. How can I add link button such that it shows Back to search results to navigate user to Searchform and user can go back to previous form.
You have duplicated your check for "no results":
if(mysqli_num_rows($result)>0)if(mysqli_num_rows($result)>0)
Remove one.
Try This:
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
Related
I am trying to store information from my database about a customers booked flights into a HTML table.
Here is the HTML code:
<h2> Your Flights: </h2>
<table>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<tr>
<td> <?php echo $row['flight_number'];?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; }?> </td>
</tr>
</table>
Currently it is repeating the table headers over again for each record from my database. I tried moving my while loop after my table headers however that just made the information go everywhere.
<h2> Your Flights: </h2>
<table>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td> <?php echo $row['flight_number'];?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; ?> </td>
</tr>
<?php }?>
</table>
The while loop have to surround the <tr> including the data:
<h2> Your Flights:</h2>
<table>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<?php while($row = $result->fetch_assoc()) { ?><!-- start of while loop -->
<tr>
<td> <?php echo $row['flight_number']; ?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; ?> </td>
</tr>
<?php } ?><!-- end of while loop -->
</table>
You current code displays the header for each line because the while starts before the header. Your table is broken because you end the while loop inside the last column (after last value). So the closing tag of the last column and the row itself is missing.
This question already has answers here:
table header is repeating from a for loop in php
(2 answers)
Closed 4 years ago.
im trying to fetch data from mysqli and display it on the table. everything is find expect for the table heading where if there's 5 data, it will repeat the header 5 times for each data.
Any idea where im doing wrong? I'm suspecting on the if loop on my php code.
Below is the code.
PHP Code:
<div class="table-responsive">
<?php
//get rows query
$query = $db->query("SELECT * FROM register");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<table class="table">
<thead class=" text-primary">
<th>
Name
</th>
<th>
Age
</th>
<th>
Gender
</th>
<th>
Passport Number
</th>
<th>
Nationality
</th>
<th>
Citizen
</th>
<th>
Address
</th>
<th>
Caught Place
</th>
<th>
Current Status
</th>
<th>
Based At
</th>
<th>
Phone Number
</th>
<th>
Family Phone Number
</th>
<th>
Company Phone Number
</th>
</thead>
<tbody>
<tr>
<td>
<?php echo $row["Name"]; ?>
</td>
<td>
<?php echo $row["Age"]; ?>
</td>
<td>
<?php echo $row["Gender"]; ?>
</td>
<td>
<?php echo $row["PassportNumber"]; ?>
</td>
<td>
<?php echo $row["Nationality"]; ?>
</td>
<td>
<?php echo $row["Citizen"]; ?>
</td>
<td>
<?php echo $row["Address"]; ?>
</td>
<td>
<?php echo $row["CaughtPlace"]; ?>
</td>
<td>
<?php echo $row["CurrentStatus"]; ?>
</td>
<td>
<?php echo $row["BasedAt"]; ?>
</td>
<td>
<?php echo $row["PhoneNumber"]; ?>
</td>
<td>
<?php echo $row["FamilyPhoneNumber"]; ?>
</td>
<td>
<?php echo $row["CompanyPhoneNumber"]; ?>
</td>
</tr>
</tbody>
</table>
<?php } }else{ ?>
<p>Data(s) not found</p>
<?php } ?>
</div>
Table output:
When you said "I'm suspecting on the if loop on my php code", you were correct. Just move the table header, start of the body & end of the body out of the loop, leaving only the table rows within ...
// Moved this from inside the loop
<table class="table">
<thead class=" text-primary">
<th>
Name
</th>
<th>
Age
</th>
<th>
Gender
</th>
<th>
Passport Number
</th>
<th>
Nationality
</th>
<th>
Citizen
</th>
<th>
Address
</th>
<th>
Caught Place
</th>
<th>
Current Status
</th>
<th>
Based At
</th>
<th>
Phone Number
</th>
<th>
Family Phone Number
</th>
<th>
Company Phone Number
</th>
</thead>
<div class="table-responsive">
<?php
//get rows query
$query = $db->query("SELECT * FROM register");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<tbody>
<tr>
<td>
<?php echo $row["Name"]; ?>
</td>
<td>
<?php echo $row["Age"]; ?>
</td>
<td>
<?php echo $row["Gender"]; ?>
</td>
<td>
<?php echo $row["PassportNumber"]; ?>
</td>
<td>
<?php echo $row["Nationality"]; ?>
</td>
<td>
<?php echo $row["Citizen"]; ?>
</td>
<td>
<?php echo $row["Address"]; ?>
</td>
<td>
<?php echo $row["CaughtPlace"]; ?>
</td>
<td>
<?php echo $row["CurrentStatus"]; ?>
</td>
<td>
<?php echo $row["BasedAt"]; ?>
</td>
<td>
<?php echo $row["PhoneNumber"]; ?>
</td>
<td>
<?php echo $row["FamilyPhoneNumber"]; ?>
</td>
<td>
<?php echo $row["CompanyPhoneNumber"]; ?>
</td>
</tr>
</tbody>
</table>
<tbody>
<?php } }else{ ?>
<p>Data(s) not found</p>
<?php } ?>
// Moved this from inside the loop
</tbody>
</table>
</div>
Below is the code that i have written that would display data from phpmyadmin table and display the result. Now once the search result is displayed, let's say if i wish to click on one of the search result for e.g. i clicked on "Jon Doe" then upon click i should be redirected to the profile page (HTML page) of Jon Doe. May i know how do i do that ?
<?php
echo "<body style='background-color:gray'>";
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
or die ( "uable to connect to MYSQL database" );
mysql_select_db( $project );
if (isset($_POST['search'])) {
$sql= "SELECT * FROM registration ";
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE first_name= '{$search_term}'";
$sql .= " OR last_name= '{$search_term}'";
$query=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<head>
<title>jon</title>
</head>
<body>
<form name="search_form" method="POST" action="retrieve.php">
<table width="599" border="1">
<tr>
<th>Search
<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">
</tr>
</table>
</form>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">First Name </div></th>
<th width="98"> <div align="center">Last Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="59"> <div align="center">Country </div></th>
<tr>
<?php if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['address_city'];?></td>
<td><?php echo $row['address_country'];?></td>
<tr>
<?php }} ?>
</table>
As Funk Doc already suggested you should make a "profile.php" for example which then queries all informations.
First you need to link the profile page. You need a "id" from your database.
<td><?php echo $row['first_name'];?></td>
Now you will redirect your user to profile.php with a special id.
example: profile.php?id=87341
In your profile.php you now need to get the id variable.
profile.php
<?php
$userid = $_GET['id'];
?>
The id is now saved in ´$userid´. Just search your database for that id and you get all your informations.
Ok lets say you have 2 Users John and Nick. John has id=1 and Nick id=2
John
Nick
handler.php
<?php
$id=$_GET['id'];
$query='SELECT * FROM #__users where id='.$id;
//show your results
?>
SearchText works if record is found but errors appear when record not found.
Here are the errors. Notice: Undefined variable: sample_list in C:\xampp\htdocs\viewsample\samplelist.php on line 166
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ viewsample\samplelist.php on line 166
If I click the search button again, errors disappear.
Is it possible to return to home page if record not found?
I searched here a possible solution to my problem but I found none.
Please help.
Thank you and more power.
Here's the code.
Index.php
//Perform Search from our database
if(isset($_POST['action_type']))
{
echo 'Welcome';
if ($_POST['action_type'] == 'search')
{
$search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
$sql = "select samp_id, daterecv, datecoll, modcoll, aperson, estabname, estabadd, gname, bname, prodcat, dform, dstrength, from sample
where gname like '%$search%' or bname like '%$search%' or estabname like '%$search%'";
$result = mysqli_query($link, $sql);
if(!$result)
{
echo mysqli_error($link);
exit();
}
//Loop through each row on array and store the data to $sample_list[]
while($rows = mysqli_fetch_array($result))
{
$sample_list[] = array('samp_id' => $rows['samp_id'],
'daterecv' => $rows['daterecv'],
'datecoll' => $rows['datecoll'],
'modcoll' => $rows['modcoll'],
'apers' => $rows['apers'],
'estabname' => $rows['estabname'],
'estabadd' => $rows['estabadd'],
'gname' => $rows['gname'],
'bname' => $rows['bname'],
'prodcat' => $rows['prodcat'],
'dform' => $rows['dform'],
'dstrength' => $rows['dstrength']);
}
include 'samplelist.php';
exit();
}
}
samplelist.php
<?php
session_start();
$role = $_SESSION['sess_userrole'];
if(!isset($_SESSION['sess_username']) && $role!="sampler"){
header('Location: index.php?err=2');
}
?>
<?php
include_once 'index.php';
?>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function GotoHome(){
window.location = 'index.php';
}
{
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
}
</script>
</head>
<body>
<div class="wrapper">
<div class="content">
<p>Logout</p>
<p><center><b><font style="arial" color="black">SAMPLE COLLECTION</font></b></center></p>
<div style="margin-bottom: 10px;">
<form method="POST" action="index.php">
<input type="text" id="searchText" name="searchText" style="width:300px"/>
<input type="hidden" name="action_type" value="search" onfocus="Clear (this);"/>
<input type="submit" value="search"/>
</form>
</div>
<div style="max-height: 740px; overflow:auto; max-width: 1600; overflow:auto;">
<table class="pbtable">
<thead>
<tr>
<th>
Date Received
</th>
<th>
Date Collected
</th>
<th style = "display:none">
Mode of Collection
</th>
<th style = "display:none">
Assigned Personnel
</th>
<th>
Establishment Name
</th>
<th style = "display:none">
Establishment Address
</th>
<th>
Generic Name
</th>
<th>
Brand Name
</th>
<th>
Product Category
</th>
<th style = "display:none">
Dosage Form
</th>
<th style = "display:none">
Dosage Strength
</th>
</thead>
<tbody>
<?php foreach($sample_list as $collection) : ?>
<tr>
<td>
<?php echo $collection["daterecv"]; ?>
</td>
<td>
<?php echo $collection["datecoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["modcoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["aperson"]; ?>
</td>
<td>
<?php echo $collection["estabname"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["estabadd"]; ?>
</td>
<td>
<?php echo $collection["gname"]; ?>
</td>
<td>
<?php echo $collection["bname"]; ?>
</td>
<td>
<?php echo $collection["prodcat"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dform"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dstrength"]; ?>
</td>
<form method="post" action="index.php">
<input type="hidden" name="ci"
value="<?php echo $collection["samp_id"]; ?>" />
<input type="hidden" name="action" value="edit" />
<input type="submit" value="Edit" />
</form>
<tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
If no records are found in DB the $sample_list array is empty.
So samplelist.php needs this check before foreach loop,
if (empty($sample_list))
{
header('Location: http://www.homesweet.home/');
exit;
}
foreach($sample_list as $collection) :
...
endforeach;
I want to create a crf form for my university project .
i created course table i select some data from course but where student click the completed course and next click .then next page show same data base table but not those courses, student already selected.and
then student can subset for applied courses.
But when i select some course and click next, But it show the previous row in course table. but i want selected course row not select when i click . cause completed course not want to select for applying course.
I want to select some row from a database when aply it show the same databases all row except the selected row
i add the same line in the problem line..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
CRF Form
</title>
<link rel="stylesheet" href="CSS/admin_style.css" >
</head>
<body>
<div id="header">
<a href="index.php">
<h1>
Welcome to the CRF Form
</h1>
</a>
</div>
<form action="selection.php" method="POST" enctype="multipart/form-data" >
<div id="">
<table width="1000" border="5" align="center">
<tr>
<td colspan="8" align="center" bgcolor="yellow">
<h1>
Slect your completed course
</h1>
</td>
</tr>
<tr bgcolor="orange">
<th>
selection:
</th>
<th>
Course id:
</th>
<th>
Course title:
</th>
<th>
Course credits:
</th>
<th>
Course statust:
</th>
<th>
Delete Post:
</th>
<th>
Edit Post:
</th>
</tr>
<?php
include("includes/connect.php");
$query="select * from course";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$id=$row['id'];
$course_id=$row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
?>
<tr align="center" bgcolor="pink">
<td>
<input type="checkbox" name="complete[]" value="<?php echo $id; ?>" />
</td>
<td>
<?php echo $course_id; ?>
</td>
<td>
<?php echo $course_title; ?>
</td>
<td>
<?php echo $course_credits; ?>
</td>
<td>
<?php echo $course_status; ?>
</td>
<td>
<a href="delete.php?del=<?php echo $post_id ?>">
Delete
</a>
</td>
<td>
<a href="Edit.php?edit=<?php echo $post_id ?>">
Edit
</a>
</td>
</tr>
<?php } ?>
<tr>
<td align="center" colspan="7">
<input type="submit" name="sub" value="NEXT">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
this is the selec.php
<html>
<head>
<title>
CRF Form
</title>
<link rel="stylesheet" href="CSS/admin_style.css" >
</head>
<body>
<div id="header">
<a href="index.php">
<h1>
Welcome to the CRF Form
</h1>
</a>
</div>
<div id="">
<table width="1000" border="5" align="center">
<tr>
<td colspan="8" align="center" bgcolor="yellow">
<h1>
Slect your completed course
</h1>
</td>
</tr>
<tr bgcolor="orange">
<th>selection:</th>
<th>Course id:</th>
<th>Course title:</th>
<th>Course credits:</th>
<th>Course statust:</th>
<th>Delete Post:</th>
<th>Edit Post:</th>
</tr>
<?php
include("includes/connect.php");
$check=$_POST['complete'];
foreach($check as $ch){
$select= " id!='".$ch."' and ";//here is the problem
}
$query="select * from course
where $select id!='0'";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$id=$row['id'];
$course_id=$row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
?>
<tr align="center" bgcolor="pink">
<td>
<input type="checkbox" name="completed" /></input>
</td>
<td><?php echo $course_id; ?></td>
<td><?php echo $course_title; ?></td>
<td><?php echo $course_credits; ?></td>
<td><?php echo $course_status; ?></td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
I think that you're saying the once the course is selected it shouldn't be displayed on the next page where the student can have a look on the other courses?
If is so then you can use following sql query on the next page where you don't want to display the student's completed course.
SELECT * FROM course WHERE id != $course_id
Let me know if I'm wrong. I didn't comment out as my reputations were low and stackoverflow didn't allow me to.
[EDITED]
This is your complete code.
Your select php file:
//assuming that you are logging in the students with their username or email id, if so then store their username in a session where logging in.
<?php
$user = $_SESSION['username'];
include("includes/connect.php");
if (isset($_POST['submit'])){
$course_id= $_POST['course_id'];
$course_title= $_POST['course_title'];
$course_credits= $_POST['course_credits'];
$course_status= $_POST['course_status'];
$query="SELECT course.id,course.title,course.credits,course.status FROM course WHERE course.username = $user";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$course_id= $_SESSION['course_id'] = $row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
}
?>
Now in your next php file :
$already_selected_course = $_SESSION['course_id'];
Now the query should look like.
$query = "SELECT course.id,course.title,course.credits,course.status FROM course WHERE course.id != $already_selected_course";
This is it. Note: This solution might contain some errors of brackets etc but the logic is clear.
For better knowledge have a look at my MySQL Complete Video Series here!