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..
Related
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>";
I am creating a page in PHP, HTML and using MySQL. Currently When I load the page, it selects all the data from the staff table and displays it.
I have a search function so the user can filter by first name, last name or full name.
When the user clicks the search button, is it possible to have the data that's showing every row and just to replace it with my searched criteria.
Currently my code is searching correct but its just adding it as a row to all the data rows. I thought i could use a regex to replace but the variable is not accessible globally since it is inside an if statement.
<?php
$output = NULL;
if(isset($_POST['submit'])) {
$regex = '/<table[^>]*>.*?<\/table>/s'; //test doesnt work
$replace = ''; //test doesnt work
$result = preg_replace($regex, $replace, $html); //test doesnt work "no html variable"
echo($result); //test doesnt work
$search = $con->real_escape_string($_POST['search']);
$res = $_POST['searchGroup'];
if($res == "first") {
$resultSet = $con->query("SELECT * FROM staff WHERE firstname LIKE '%$search%'");
if($resultSet->num_rows > 0) {
while($rows = $resultSet -> fetch_assoc()) {
$field1name = $rows["firstname"];
$field2name = $rows["lastname"];
$field3name = $rows["dob"];
$field4name = $rows["created"];
$field5name = $rows["last_updated"];
$field6name = $rows["is_user"];
$output .= '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
}
else {
$output = "No Results";
}
}
And then my code which is currently displaying all the data
<?php
$query = "SELECT * FROM staff";
echo '<div class="tableFixHead">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial"><b>First Name</b></font> </td>
<td> <font face="Arial"><b>Last Name</b></font> </td>
<td> <font face="Arial"><b>Date of Birth</b></font> </td>
<td> <font face="Arial"><b>Creation Date</b></font> </td>
<td> <font face="Arial"><b>Last Updated</b></font> </td>
<td> <font face="Arial"><b>Is User</b></font> </td>
</tr>
</div>';
echo $output;
if ($result = $con->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["firstname"];
$field2name = $row["lastname"];
$field3name = $row["dob"];
$field4name = $row["created"];
$field5name = $row["last_updated"];
$field6name = $row["is_user"];
$html= '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
echo $html;
}
$result->free();
}
?>
My approach is probably wrong but I was wondering if it is possible to somehow remove that data that is in the current $html echo tag when my search button is pressed.
Besides the code style, vulnerabilities i would like to offer you an example of some sorts. as i presume that you are still a student.
just a simple example what you could do;
$query = "SELECT * FROM staff";
if(isset($_POST['submit'])) {
$search = $con->real_escape_string($_POST['search']);
$query .= " WHERE firstname LIKE '%$search%'"
}
This should be sufficient to help you forward.
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 am following The book "Beginning PHP, Apache, MySQL web development" by Wrox. I have been following it verbatim and for some reason I am having a issue with the code. The editor says that there are no errors in my code. but when I run it gives me the following message:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3" here is the following code
<?php
//take in the id of a director and return his/her full name
function get_director() {
global $db;
$query = 'SELECT people_fullname
FROM people
WHERE people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
//take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT people_fullname
FROM people
WHERE people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
$extract($row);
return $people_fullname;
}
// take in the id of a movie type
// and return the meaningful textual description
function get_movietype($type_id) {
global $db;
$query = 'SELECT movietype_label
FROM movietype
WHERE movietype_id = ' . $type_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
// conect to MySQL
$db = mysql_connect('localhost', 'root', 'root') or
die ('unable to connect. Check your parameters');
// make sure you are yousing the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db) );
// retrieve information
$query = 'SELECT movie_name, movie_year, movie_director, movie_leadactor, movie_type
FROM movie
ORDER BY movie_name ASC, movie_year DESC';
$result = mysql_query($query, $db) or die ($mysql_error($db) );
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
$table = <<<ENDHTML
<div style ="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2" style="width: 70%; margin-left: auto; margin-right:auto;">
<tr>
<th>Movie Title</th>
<th>Year of the release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
ENDHTML;
//loop throught the results
while ($row = mysql_fetch_assoc($result) ) {
extract($row);
$director = get_director($movie_director);
$leadactor = get_leadactor($movie_leadactor);
$movietype = get_movietype($movie_type);
$table .= <<<ENDHTML
<tr>
<td>$movie_name</td>
<td>$movie_year</td>
<td>$director</td>
<td>$leadactor</td>
<td>$movietype</td>
</tr>
ENDHTML;
}
$table .= <<<ENDHTML
</table>
<p>$num_movies Movies</p>
</div>
ENDHTML;
echo $table
?>
After that I tried copying and pasting the exact code thinking maybe I did something wrong
and here it is the following code:
<?php
// take in the id of a director and return his/her full name
function get_director($director_id) {
global $db;
$query = 'SELECT
people_fullname
FROM people
WHERE
people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT
FROM
people WHERE
people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// take in the id of a movie type and return the meaningful textual
// description
function get_movietype($type_id) {
global $db;
$query = 'SELECT
movietype_label
FROM
movietype
WHERE
movietype_id = ' . $type_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
//connect to MySQL
$db = mysql_connect('localhost', 'root', 'root') or
die ('Unable to connect. Check your connection parameters.');
// make sure you’re using the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// retrieve information
$query = 'SELECT
movie_name, movie_year, movie_director, movie_leadactor,
movie_type
FROM
movie
ORDER BY
movie_name ASC,
movie_year DESC';
$result = mysql_query($query, $db) or die(mysql_error($db));
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
$table = <<<ENDHTML
<div style="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2"
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
ENDHTML;
// loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$director = get_director($movie_director);
$leadactor = get_leadactor($movie_leadactor);
$movietype = get_movietype($movie_type);
$table .= <<<ENDHTML
<tr>
<td>$movie_name</td>
<td>$movie_year</td>
<td>$director</td>
<td>$leadactor</td>
<td>$movietype</td>
</tr>
ENDHTML;
}
$table .= <<<ENDHTML
</table>
<p>$num_movies Movies</p>
</div>
ENDHTML;
echo $table;
?>
when I ran the code this time, the table header shows but part of the code is also displayed on the browser. It looks like this:
ENDHTML; // loop through the results while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .= << ENDHTML; } .= <<
any help will be appreciated I am new to programming thanks
When you end an heredoc you must not put any char at the begining of the line. In your code there are heredocs with spaces or tabs before them. Delete the spaces and put your heredoc at the begining of the line.
More info on heredoc.
Closing heredoc statement must be at the very first position in the string:
ENDHTML; // works
ENDHTML; // won’t work