I want to add this line
a href="r.php?id=<?php echo $row['id']; ?>" >Details>
which will display details of this id on another page.
but i don't know how to write this line correctly. can you help me please thank you.
$conn = new mysqli('localhost', 'root', '', 'dbmtc');
$perPage = 10;
$page = 0;
if (isset($_POST['page'])) {
$page = $_POST['page'];
} else {
$page=1;
};
$startFrom = ($page-1) * $perPage;
$sqlQuery = "SELECT fname, lname, dept, email
FROM staff ORDER BY fname ASC LIMIT $startFrom, $perPage";
//echo $sqlQuery;
$result = mysqli_query($conn, $sqlQuery);
$paginationHtml = '';
while ($row = mysqli_fetch_assoc($result)) {
$paginationHtml.='<tr>';
$paginationHtml.='<td>'.$row["fname"].' '.$row["lname"].'</td>';
$paginationHtml.='<td>'.$row["dept"].'</td>';
$paginationHtml.='<td>'.$row["email"].'</td>';
**this line should be here**
$paginationHtml.='</tr>';
}
$jsonData = array(
"html" => $paginationHtml,
);
echo json_encode($jsonData);
?>
This part is in index.php
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Email</th>
<th>Operations</th>
</tr>
</thead>
<tbody id="content">
</tbody>
</table>
This is the table
And when i insert
$paginationHtml .= '<td>Details</td>';
I got this I don't why, that why I said it's not good
enter image description here
First of all: You didn't select the ID in your sql query. Please add it in the selected fieds.
These are the possibilities to write this and you can choose one of them:
1- $paginationHtml .= '<td><a href="r.php?id='.$row['id'].'" >Details</a></td>';
2- $paginationHtml .= "<td><a href=\"r.php?id={$row['id']}\" >Details</a></td>";
Related
I created a table which is updated through a form and each row gets assigned a specific number.
When viewing this table, I want to click on that assigned number and get a page where all the details of that row are displayed.
If I do $sql = "SELECT * FROM clients WHERE nif_id='114522';"; - where the nif_id is the assigned number - I get the values for that number, but I need it to change with every number in the table.
Any ideas?
UPDATE
This is the table code:
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
You can use the get request parameters.
ex: www.myapp.com/table?id=3920393
add functionality in your PHP file as follows
if(isset($_GET["id"])){
$id = $_GET["id"];
$sql = "SELECT * FROM clients WHERE nif_id='".$id."';";
//make db call & display HTML
}
This is a very simple implementation and does not implement any security or SQL injection security. This was more of a conceptual answer as to how you can tackle your problem.
This is quite a common scenario for web-based systems.
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
where the detail.php is another page to query specific details regarding the query nifid.
As a reminder, if the data type of the column is INT, there is no need to use single quotes to surround the value in the SQL statement.
Sample detail.php:
<?php
if(!isset($_GET['nifid']) || (int)$_GET['nifid'] <= 0) {
// Invalid or missing NIFID
header('Location: table.php');
}
include_once '../includes/db.inc.php';
$id = (int)$_GET['nifid'];
$sql = "SELECT * FROM clients WHERE nif_id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// TODO: display the result in whatever way you like
?>
Firstly, the headings are stored in h1 tags. This is taken from a separate table named "menu_type". Which is linked through a "menu" table.
I am trying to display data on the base like this:
HEADING
Table Data
2nd Heading
Second Data
--- In a loop until it is all completed ---
Here is a like page of what it is doing
I believe I have the methods correct and can see what it is doing, it is printing the first heading, then a blank table, then the second heading and then the data from the first table.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $numRows; $i++){
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>$result_array[$i]</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
<tr>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo "
<td>$name - <small>$description</small></td>
<td class='text-right'>£$price </td>
";
}
echo
"
</tr>
</tbody>
</table>
";
}
// print_r($result_array[2]);
?>
You don't need these anymore, it's like you're repeating the query. It looks incorrect also.
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
The menu items are already in this query, you just have to loop through it;
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
UPDATE 1: this code is incorrect, it doesn't insert the value to the array. I updated the code (after "try this").
$result_array[] = $menuType;
UPDATE 2: the $result is repeatedly used in mysqli functions, the index is being moved. What I did is copied the initial $result to $resultCopy. Try code again, haha
Use array_push($array,$value_you_insert) function, for inserting elements to an array.
Try this;
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$resultCopy = $result;
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($resultCopy)){
$menuType = $row['type'];
array_push($result_array,$menuType);
}
for($i = 0; $i < $numRows; $i++){
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>".$result_array[$i]."</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>Item</th>
<th class='text-right'>Price</th>
</tr>
</thead>
<tbody>
";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo"
<tr>
<td>".$name." - <small>".$description."</small></td>
<td class='text-right'>£".$price."</td>
</tr>
";
}
echo
"
</tbody>
</table>
";
}
?>
enter image description herei am working on project CMS with php and one of my while loops doesn't work and i cant find why.
i am echo on (td) inside of loop and nothing showed on the page but when i echo outside of while loop it work very well.
i have commentet the loop to see you.
Can you give me some help where i have the problem?
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Author</th>
<th>Comment</th>
<th>Email</th>
<th>Status</th>
<th>In Response to</th>
<th>Date</th>
<th>Approve</th>
<th>Unapprove</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM comments ";
$select_comments = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_comments)) {
$comment_id = $row['comment_id'];
$comment_post_id = $row['comment_post_id'];
$comment_author = $row['comment_author'];
$comment_content = $row['comment_content'];
$comment_email = $row['comment_email'];
$comment_status = $row['comment_status'];
$comment_date = $row['comment_date'];
echo "<tr>";
echo "<td>$comment_id</td>";
echo "<td>$comment_author</td>";
echo "<td>$comment_content</td>";
/
echo "<td>$comment_email</td>";
echo "<td>$comment_status</td>";
//THIS IS THE LOOP DOESN'T WORK
$query = "SELECT * FROM posts WHERE post_id = $comment_post_id";
$select_post_id_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_post_id_query)) {
$post_id = $row['post_id'];
$post_title = $row['post_title'];
echo "<td><a href='../post.php?p_id=$post_id'>$post_title</a></td>";
}
echo "<td>$comment_date</td>";
echo "<td><a href='comment.php?approve=$comment_id'>Approve</a></td>";
echo "<td><a href='comment.php?unapprove=$comment_id'>Unapprove</a></td>";
echo "<td><a href='comment.php?delete=$comment_id'>Delete</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
//approve posts
if(isset($_GET['approve'])){
$the_comment_id = $_GET['approve'];
$query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $the_comment_id ";
$approve_comment_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
//unapprove posts
if(isset($_GET['unapprove'])){
$the_comment_id = $_GET['unapprove'];
$query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id = $the_comment_id ";
$unapprove_comment_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
//delete posts
if(isset($_GET['delete'])){
$the_comment_id = $_GET['delete'];
$query = "DELETE FROM comments WHERE comment_id = {$the_comment_id} ";
$delete_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
?>
You are overwriting variable $row in your second loop. You should change it eg. to $subRow to avoid such situation.
i have find the missing part on of my db with the connection.
THNX all you friends :D
I've made this code using a tutorial that allows me to upload users to the database. The whole thing works great, but the only problem is that it starts to show 2 of the same user over and over, the list starts expanding 5 every user i add... What could be the problem causing this?
Item in index that lays out the whole list:
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysql_error());
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
echo $names;
}
} else {
echo "No Users Found";
}
?>
</table>
Either output the one record per iteration; or build the whole HTML block, then output the block. I think the simplest would be:
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
echo "<tr><td>$user</td></tr>";
}
... alternative approach
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysqli_error($db));
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
}
} else {
$names = "No Users Found";
}
echo $names;
?>
</table>
Also you can't use mysql_* functions with mysqli_*. See http://php.net/manual/en/mysqli.error.php.
Simplest example of the issue: https://eval.in/627250
Here's the PHP files in my project. I used a href for 'get movie details' as per that it goes to another page and shows the results.
<a href = "movie_details.php?movie_id=$movie_id" ...
I need to get those details with ajax request and render response in same page (better if it in a inside a div).
table1.php
<?php
$link = mysql_connect("localhost","root","") or die(mysql_error());
mysql_selectdb("MovieSite") or die(mysql_error());
$query = "SELECT movie_id, movie_name, movie_director, movie_leadactor FROM Movie ";
//"WHERE movie_year > 1990 ORDER BY movie_type";
$result = mysql_query($query, $link) or die(mysql_error());
$num_movies = mysql_num_rows($result);
$movie_header = <<<EOD
<h2><center>Movie Review Database</center></h2>
<table width = "70%" border = "1" cellpadding = "2" cellspacing = "2" align = "center">
<tr>
<th>Movie Title</th>
<th>Movie Director </th>
<th>Movie Lead Actor</th>
</tr>
EOD;
function get_director(){
global $movie_director;
global $director;
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d = mysql_query($query_d) or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}
function get_leaderactor(){
global $movie_leadactor;
global $leadactor;
$query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";
$result_l = mysql_query($query_l);
$row_l = mysql_fetch_array($result_l);
extract($row_l);
$leadactor = $people_fullname;
}
$movie_details = '';
while ($row = mysql_fetch_array($result)){
$movie_id = $row['movie_id'];
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
get_director();
get_leaderactor();
$movie_details .= <<<EOD
<tr>
<td>$movie_name</td>
<td>$director</td>
<td>$leadactor</td>
</tr>
EOD;
}
$movie_details .= <<<EOD
<tr>
<td>Total : $num_movies Movies </td>
</tr>
EOD;
$movie_footer = "</table>";
$movie = <<< MOVIE
$movie_header;
$movie_details;
$movie_footer;
MOVIE;
echo $movie;
?>
movie_details.php
<?php
$link = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('MovieSite') or die("DB Select" . mysql_error());
function calculate_differences($takings, $cost){
$difference = $takings - $cost;
if ($difference < 0){
$difference = substr($difference, 1);
$font_color = 'red';
$profit_or_loss = "Rs. " . $difference . "M";
}elseif ($difference > 0){
$font_color = 'green';
$profit_or_loss = "Rs. " . $difference . "M";
}else {
$font_color = 'blue';
$profit_or_loss = "Broke even";
}
return "<font color = \"$font_color\"> ". $profit_or_loss . "</font>";
}
function get_director(){
global $movie_director;
global $director;
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d = mysql_query($query_d) or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}
function get_leaderactor(){
global $movie_leadactor;
global $leadactor;
$query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";
$result_l = mysql_query($query_l);
$row_l = mysql_fetch_array($result_l);
extract($row_l);
$leadactor = $people_fullname;
}
$movie_query = "SELECT * FROM Movie WHERE movie_id = '" . $_GET['movie_id'] . "'";
$result = mysql_query($movie_query, $link) or die(mysql_error());
$movie_header = <<<EOD
<h2><center>Movie Review Database</center></h2>
EOD;
while ($row = mysql_fetch_array($result)){
//$movie_id = $row['movie_id'];
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time'] . "Mins";
$movie_takings = $row['movie_taking'];
$movie_cost = $row['movie_cost'];
get_director();
get_leaderactor();
}
$movie_health = calculate_differences($movie_takings, $movie_cost);
$page_start = <<<EOD
<html>
<head>
<title>Details and Review for: $movie_name </title>
</head>
<body>
EOD;
$movie_table_headings =<<<EOD
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director </th>
<th>Movie Lead Actor</th>
<th>Movie running Time</th>
<th>Movie Health</th>
</tr>
EOD;
$movie_details = <<<EOD
<table width = "70%" border = "1" cellpadding = "2" cellspacing = "2" align = "center">
<tr>
<th colspan = "6"><u><h2>$movie_name: details</h2></u></th>
</tr>
$movie_table_headings
<tr>
<td width = "33%" align = "center">$movie_name</td>
<td align = "center">$movie_year</td>
<td align = "center">$director</td>
<td align = "center">$leadactor</td>
<td align = "center">$movie_running_time</td>
<td align = "center">$movie_health</td>
</tr>
</table>
<br /><br />
EOD;
$page_end = <<<EOD
Home
</body>
</html>
EOD;
$movie = <<< MOVIE
$page_start;
$movie_details;
$page_end;
MOVIE;
echo $movie;
mysql_close();
AJAX works by allowing you to retrieve the contents of a page using clientside code (e.g. , javascript/jQuery) without making the user leave the current page.
When you make an ajax request, you can specify parameters to send via GET or POST (or not send any parameters at all). Your code will then receive the content of the page as if you had visited it with your browser (what you would get if you went to the page, right-clicked and viewed source).
So what you would want to do with your PHP is to output the values that you need on your current page. It is common practice to encode this data using JSON or XML (which is the X in ajax), so that your clientside code can read it more easily. (I personally prefer JSON).
Learn jQuery: http://w3schools.com/jquery/default.asp
Learn about AJAX: http://w3schools.com/ajax/default.asp
Learn about Ajax with jQuery: http://api.jquery.com/jQuery.ajax/
or http://api.jquery.com/jQuery.get/
or http://api.jquery.com/jQuery.post/
Let me know if that answers your question..