php search form and put into a table - php

Using this code, I do search from my database and display the result. Now its show all the result of my table first.
Then when I search for a new Item then its show the item. I do not want to display all result first. I want to display only searched Item.
<table class="table table-striped">
<tr>
<th>No</th>
<th>Teacher Name</th>
<th>Gender</th>
<th>Date of Birth</th>
<th>Place of Birth</th>
<th>Degree</th>
<th>Salary</th>
<th>Married</th>
<th>Phone</th>
<th>E-mail</th>
</tr>
<?php
$key="";
if(isset($_POST['searchtxt']))
$key=$_POST['searchtxt'];
if($key !="")
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
else
$sql_sel=mysql_query("SELECT * FROM teacher_tbl");
$i=0;
while($row=mysql_fetch_array($sql_sel)){
$i++;
?>
<tr >
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
?>
</table>

Using the following code, the search will be carried out and displayed only when you have submitted a search key:
<?php
$key="";
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt'])) {
$key=$_POST['searchtxt'];
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
$i=0;
while($row=mysql_fetch_array($sql_sel)) {
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
}
?>

You just have to loop through the results only if $_POST['searchtxt'] is set.
<?php
$key=$_POST['searchtxt'];
if($key !="")
$sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name like '%$key%' or l_name like '%$key%'");
else
$sql_sel=mysql_query("SELECT * FROM teacher_tbl");
$i=0;
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt']))
{
while($row=mysql_fetch_array($sql_sel)){
$i++;
?>
<tr >
<td><?php echo $i;?></td>
<td><?php echo $row['f_name']." ".$row['l_name'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['pob'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['salary'];?></td>
<td><?php echo $row['married'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php
}
}
?>
</table>

Related

How to make sql image clickable on display board

I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp

php table not displaying contents and only creating table boxes

I would like the table to display all the values inside of it, but it is currently not displaying the values and only creating an empty table
[what the table is displayed as (image)][1]
$result = mysqli_query( $conn,'SELECT * FROM Pictures ');
$conn->close();
Html
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;"
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if( $result != null){
Result is not empty
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
}else{
echo "Something went wrong with the result";
}
?>
</tbody>
</table>
<?php //mysqli_close($conn); ?>
</body>
</html>
changed the out put to match the answer you gave but the output came out as picture 2 while my table is actually picture 3 any ideas
output:
table im trying to display
Display the data in this way:
<td><?php echo $row['id']; ?></td>
Try changing
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
to
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
or the shorthand variant
<td><?= $row['id']; ?></td>
<td><?= $row['hname']; ?></td>
<td><?= $row['himage']; ?></td>
<td><?= $row['hdesc']; ?></td>
And as Samuel pointed out in a comment, are you sure there is a need for the extra foreach considering you're already looping with the while?
Update: OP have you tried the following?
while( $row = mysqli_fetch_assoc( $result ) ){
//removed foreach()
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
</tr>
<?php
}
}else{
Update 2 OP wishes to have the image load instead of showing the raw URL.
To do this, we need to use an actual image tag and insert the url into the src.
I assume that this line is the image URL
<td><?php echo $row['himage']; ?></td>
So change it to
<td> <img src="<?php echo $row['himage']; ?>" > </td>

I want ziparchive or other php script to download all files from a folder in zip

<table id="b" class="table table-hover" border="1" cellspacing="0" cellpadding="5">
<tr>
<th>Serial No</th>
<th>Student Name</th>
<th>Student ID</th>
<th>Point ID</th>
<th>CV</th>
<th>USI</th>
<th>Reference</th>
<th>Academic Certificate</th>
<th>Training Documents</th>
<th>Pay Slips</th>
<th>Work Pictures</th>
<th>Other Documents</th>
<th>Work Documents</th>
<th>Applicants Form</th>
<th>Employer Form</th>
<th>Action</th>
</tr>
<?php
$result = mysql_query("select * from student_portal");
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['student_id']; ?></td>
<td><?php echo $row['point_id']; ?></td>
<td><?php echo $row['cv']; ?></td>
<td><?php echo $row['usi']; ?></td>
<td><?php echo $row['reference']; ?></td>
<td><?php echo $row['academic_certificate']; ?></td>
<td><?php echo $row['training_documents']; ?></td>
<td><?php echo $row['Pay_slips']; ?></td>
<td><?php echo $row['work_pictures']; ?></td>
<td><?php echo $row['other_documents']; ?></td>
<td><?php echo $row['work_documents']; ?></td>
<td><?php echo $row['declaraction_form']; ?></td>
<td><?php echo $row['declaraction_employer']; ?></td>
</tr>
Here is the code, I want to download all file in a zip format. In zip file all in a folder.
You will probably have to create a row HTML file of the output and run it through a php zipper function.
Here's one to get you started
function unzip_file($location,$newLocation){
if(exec("unzip $location",$arr)){
mkdir($newLocation);
for($i = 1;$i< count($arr);$i++){
$file = trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location.'/'.$file,$newLocation.'/'.$file);
unlink($location.'/'.$file);
}
return TRUE;
}else{
return FALSE;
}
}

need help for my php coding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to do a school project and the result only shows me 1 data from my MySQL table. Here is the code:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
}
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
</table>
Can anyone help tell me what did I am missing?
As the other commenters have said, you need to include the output block within the while loop, else you will only get the output from the last row.
Try this:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php } ?>
</table>
Really all the intermediate variables you're using are redundant, so this is equivalent and much shorter:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
Or more concise still, without the messy switching between html and php modes:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
echo "<table>";
while($row= mysql_fetch_array($run)){
echo "<tr>";
echo "<td>$row['Name']</td>";
echo "<td>$row['startdate']</td>";
echo "<td>$row['finaldate']</td> ";
echo "<td>$row['within']</td>";
echo "<td>$row['reason']</td>";
echo "<td>$row['remaining']</td>";
echo "<td>$row['status']</td>";
echo "</tr>";
}
echo "</table>";
If you want headers too, you can do as below:
<?php include("connect.php");?>
<table>
<tr>
<th>Name</th>
<th>Start Date</th>
<th>Final Date</th>
<th>Within</th>
<th>Reason</th>
<th>Remaining</th>
<th>Status</th>
</tr>
<?php
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query) or die(mysql_error());
?>
<?php
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>

Codeigniter: adding label to foreach echo

This are my view code:
<?php
foreach($books as $books) {
echo $books->book_id;
echo $books->book_name;
echo $books->description;
echo $books->author;
echo $books->publisher;
echo $books->pages;
echo $books->publication_date;
echo $books->price;
echo $books->status;
echo $books->quantity;
echo $books->genres;
echo $books->user_rating;
echo $books->reviews;
}
?>
the output is
However i want the output to be like Book ID:1, Book Name:qweqweqweqweqwe, price 123.00 and so on, in a table form am i able to do it?
I hope this will help you..
<table>
<th>
<td>Book Id</td>
<td>Book Name</td>
<td>Description</td>
<td>Author</td>
<td>Publisher</td>
<td>Pages</td>
<td>Publication Date</td>
<td>Price</td>
<td>status</td>
<td>Quantity</td>
<td>Genres</td>
<td>User Rating</td>
<td>Reviews</td>
</th>
<?php foreach($books as $book) { ?>
<tr>
<td><?php echo $book->book_id ?></td>
<td><?php echo $book->book_name ?></td>
<td><?php echo $book->description ?></td>
<td><?php echo $book->author ?></td>
<td><?php echo $book->publisher ?></td>
<td><?php echo $book->pages ?></td>
<td><?php echo $book->publication_date ?></td>
<td><?php echo $book->price ?></td>
<td><?php echo $book->status ?></td>
<td><?php echo $book->quantity ?></td>
<td><?php echo $book->genres ?></td>
<td><?php echo $book->user_rating ?></td>
<td><?php echo $book->reviews ?></td>
</tr>
<?php } ?>
</table>
concat your string with php function.
echo "Book ID:".$books->book_id.", Book Name: ".$books->book_name;
Like this way.
Use like this:
echo "Book ID : $books->book_id";
double quotes will automatically identify variables and execute them.
you can also try:-
echo 'Book ID:'. $books->book_id;
Please choose which one easily readable for you.

Categories