how to fetch data and display it in the same php page - php

I want to fetch some data from one table in the same page when a row of other table is clicked.
Here is the code for of what I am trying to do and I am stuck:
<?php
$db_table="area";
$db_query='select areaid,area_name from ' . $db_table;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');?>
<div id="area1" >
<h3>Select ony two area from Category </h3>
<form>
<fieldset style="float: left; width: 25%">
<legend>Project Area</legend>
<table border=1>
<tr>
<th> </th>
<th> Area Name </th>
</tr>
<?php
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['areaid'];?>" class="area" onlick="showfaculty()">
<td> <input class="selectarea" type="radio" name = "proarea" value="<?php echo $row['areaid'];?>"></td>
<td> <?php echo $row['area_name']; ?></td>
</tr>
<?php }
echo '</table></br></br>
</fieldset>
<fieldset id="avaguide" style="width: 25%">
<legend>Available Guide</legend>
<table border=1>
<tr>
<th> </th>
<th> Available Guides </th>
</tr>';
$db_table2="facultytable";
$db_query='select facultyID,facultName from ' . $db_table2.' where project_count<2';
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['facultyID'];?>" class="area">
<td> <input class="selectguide" type="radio" name = "areaguide" value="<?php echo $row['facultyID'];?>"></td>
<td> <?php echo $row['facultName']; ?></td>
</tr>
<?php }
echo '</table></fieldset>
</form>
</div>;`
Now my code is working fine it shows both area and faculty table. But it displays full faculty table. What I want to do is when click the row of area table some function should be called which get the all the faculty id of the selected area id from the table faculty_area(a_id, f_id) and faculty table shows only the name of faculty with fetched faculty id. An example of that function is below.
function showFaculty($aeraid){
$db_table2="faculty_area";
$db_query='select f_id from ' . $db_table2.' where a_id = '.$areaid;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){ some code that can be used to display faculty table with feteched f_id}
Please tell me how can I do it.

In my opinion the best way to do this task is using AJAX or you can build one page application with JS and RESTful service on PHP. I like AngularJS as frontend.

Related

php not returning any echo

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>

Click on a particular row of MySql Table, such that it automatically creates a new file having data of that row in a PHP Web Page

I just want to click on a row, such that it automatically creates a new file having the data of that particular row and also this file should be automatically downloaded. Please help me do this.
<html>
<head>
<title>Click on Particular Row fo MySql Table, then Create New File having data of that Row in PHP</title>
</head>
<body>
<table class="table table-striped table-hover" border="1" id="dataTable1" style="text-align: justify;margin-left:30px;">
<tr class="success" style="border-bottom: solid;border-top: solid;">
<th>
I.D.
</th>
<th>
Date
</th>
<th>
Website
</th>
<th>
E-Mail
</th>
<th>
Mobile Number(Head)
</th>
<th>
Courses
</th>
<th>
File
</th>
</tr>
<?php
while($row = mysqli_fetch_array($fetch))
{
?>
<tr>
<td>
<?php echo $row['id'];?> <!-- Want to Click on I.D. Such that Automatically Create New File having data of this whole Row -->
</td>
<td>
<?php echo $row['date_Time'];?>
</td>
<td>
<?php echo $row['web_link'];?>
</td>
<td>
<?php echo $row['e_mail']; ?>
</td>
<td>
<?php echo $row['mobile_number']; ?>
</td>
<td>
<?php echo $row['courses_offer']; ?>
</td>
<td>
View File
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Have the info needed in a hidden input OR make a page which takes the $_GET value for row id, runs a query and outputs the info.
Onclick, bring up hidden pop-up div with an iframe. The page either takes the id/value from $_GET, runs query if needed, file_put_contents($file, $data) to output the data to a text file. Have a download link/image to download the file, pointing to just created file.

How do i link search result with HTML page in PHP

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
?>

Select Data by check box click next and unselect the selected row

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!

Display foreign key records on a new page

i have this table of trucks.
<div class="myform">
<form id="form" name="form" method="post" action="">
<table border="1">
<tr>
<th>Plate Number</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo strtoupper($row_Trucks['truck_plate_no']); ?></td>
</tr>
<?php } while ($row_Trucks = mysql_fetch_assoc($Trucks)); ?>
</table>
</form>
</div>
and this list of trucks is related to a delivery details table
tbl_delivery_details, tbl_po, tbl_waybill, tbl_fuel_details, tbl_driver...
and also connected to a bunch of other tables in the database. How can i display the records related to each of the trucks when i click the truck plate number on the table and link me to another page that will show me all the records related to that truck, the truck table is a foreign key on the other tables.
Try something like this.
If truck_plate_number is unique,you can pass it along with the link as shown below.Otherwise,get the ID of record(primary key) and replace the $truck_plate_no with that ID in the below code.
<div class="myform">
<form id="form" name="form" method="post" action="">
<table border="1">
<tr>
<th>Plate Number</th>
</tr>
<?php do {
$truck_plate_no=$row_Trucks['truck_plate_no'];
echo '<tr><td><a href=anotherpage.php?truck_plate_no='.$truck_plate_no.'>'.strtoupper($truck_plate_no).'</td></tr>';
} while ($row_Trucks = mysql_fetch_assoc($Trucks));
?>
</table>
In anotherpage.php
<?php
$truck_plate_number=$_GET['truck_plate_no'];//get value from url
?>
Then query, details from the table using this id.

Categories