Location of DIV that display MySQL within PHP file - php

Ok so I have this below page, it pulls back some details from a DB. The final line shows me how many rows I have with a certain state.
My issue I want to show this at the top of the page not the bottom, if I copy my last line to the top it doesnt count correctly and just shows 1 rather than 2 currently as it does at the bottom. Any ideas how I can include this final div at the top but have it dispaly the correct output?
Any help would be really useful.
<h2>All P1 Issues</h2>
<table class="table table-striped table-bordered table-head-bordered-bottom table-condensed">
<thead>
<tr>
<th class=span1>Ticket ID</th>
<th class=span2>Title</th>
<th class=span2>Submitter</th>
<th class=span2>Owner</th>
<th class=span2>Status</th>
<th class=span1>Created</th>
<th class=span1>Modified</th>
</tr>
</thead>
<tbody>
<?php
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE HD_TICKET.HD_QUEUE_ID IN( 1,3) AND
(HD_PRIORITY.NAME like '%High%') OR
(HD_STATUS.NAME like '%Critical%')
ORDER BY Owner, Created DESC
";
$result1 = mysql_query($query1);
$num = mysql_numrows($result1);
$i = 0;
while ($i < $num)
{
$ID = mysql_result($result1,$i,"ID");
$Title = mysql_result($result1,$i,"Title");
$Status = mysql_result($result1,$i,"Status");
$Type = mysql_result($result1,$i,"Type");
$Created = mysql_result($result1,$i,"Created");
$Modified = mysql_result($result1,$i,"Modified");
$Priority = mysql_result($result1,$i,"Priority");
$Owner = mysql_result($result1,$i,"Owner");
$Submitter = mysql_result($result1,$i,"Submitter");
$ID = stripslashes($ID);
$Title = stripslashes($Title);
$Status = stripslashes($Status);
$Type = stripslashes($Type);
$Created = stripslashes($Created);
$Modified = stripslashes($Modified);
$Priority = stripslashes($Priority);
$Owner = stripslashes($Owner);
$Submitter = stripslashes($Submitter);
$StatusSpan="";
if ($Status=="Stalled")
{
$StatusSpan="<span class='label label-warning'>$Status</span>";
}
$PriortySpan="";
if ($Priority=="High")
{
$PriortySpan="<span class='label label-important'><i class='icon-exclamation-sign icon-white'></i>High</span>";
}
if ($Priority=="Low")
{
$PriortySpan="<span class='label'>Low</span>";
}
if ($Priority=="Medium")
{
$PriortySpan="<span class='label'>Medium</span>";
}
if ($Priority=="Critical")
{
$PriortySpan="<span class='label'><i class='icon-exclamation-sign icon-white'></i>Critical</span>";
}
echo "<tr><td><a href='http://$KaceBoxDNS/adminui/ticket.php?ID=$ID' target='_blank'>$ID</a> $StatusSpan $PriortySpan</td> \n";
echo "<td>$Title</td> \n";
echo "<td>$Submitter</td> \n";
echo "<td>$Owner</td> \n";
echo "<td>$Status</td> \n";
echo "<td>$Created</td> \n";
echo "<td>$Modified</td> \n";
echo "</tr> \n";
$i++;
}
echo "<p><span class='label label-important'>$num P1 Issues</span></p>";
echo "</tbody></table> \n";
?>
<center><div class="alert alert-success" role="alert"><strong>No Current</strong><?php echo "<p><span class='label label-important'>$num P1 Issues</span></p>"; ?> Critical / High Priority Incidents</div></center>

You can hop in & out of PHP in an HTML code.
Below is an example of this.
Also in HTML you can use <br> or <br /> as /n is used in texts to break line. A last point: mysql* is deprecated, I'd advise using PDO instead, as it's great fun.
<?php
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE HD_TICKET.HD_QUEUE_ID IN( 1,3) AND
(HD_PRIORITY.NAME like '%High%') OR
(HD_STATUS.NAME like '%Critical%')
ORDER BY Owner, Created DESC
";
$result1 = mysql_query($query1);
$num = mysql_numrows($result1);
$i = 0;
while ($i < $num)
{
$ID = mysql_result($result1,$i,"ID");
$Title = mysql_result($result1,$i,"Title");
$Status = mysql_result($result1,$i,"Status");
$Type = mysql_result($result1,$i,"Type");
$Created = mysql_result($result1,$i,"Created");
$Modified = mysql_result($result1,$i,"Modified");
$Priority = mysql_result($result1,$i,"Priority");
$Owner = mysql_result($result1,$i,"Owner");
$Submitter = mysql_result($result1,$i,"Submitter");
$ID = stripslashes($ID);
$Title = stripslashes($Title);
$Status = stripslashes($Status);
$Type = stripslashes($Type);
$Created = stripslashes($Created);
$Modified = stripslashes($Modified);
$Priority = stripslashes($Priority);
$Owner = stripslashes($Owner);
$Submitter = stripslashes($Submitter);
$StatusSpan="";
if ($Status=="Stalled")
{
$StatusSpan="<span class='label label-warning'>$Status</span>";
}
$PriortySpan="";
if ($Priority=="High")
{
$PriortySpan="<span class='label label-important'><i class='icon-exclamation-sign icon-white'></i>High</span>";
}
if ($Priority=="Low")
{
$PriortySpan="<span class='label'>Low</span>";
}
if ($Priority=="Medium")
{
$PriortySpan="<span class='label'>Medium</span>";
}
if ($Priority=="Critical")
{
$PriortySpan="<span class='label'><i class='icon-exclamation-sign icon-white'> </i>Critical</span>";
}
?>
<h2>All P1 Issues</h2>
<table class="table table-striped table-bordered table-head-bordered-bottom table-condensed">
<thead>
<tr>
<th class=span1>Ticket ID</th>
<th class=span2>Title</th>
<th class=span2>Submitter</th>
<th class=span2>Owner</th>
<th class=span2>Status</th>
<th class=span1>Created</th>
<th class=span1>Modified</th>
</tr>
</thead>
<?php
/* THIS IS THE IMPORTANT PART FOR YOUR STUFF */
echo "<tr><td><a href='http://$KaceBoxDNS/adminui/ticket.php?ID=$ID' target='_blank'>$ID</a> $StatusSpan $PriortySpan</td> \n";
echo "<td>$Title</td><br>";
echo "<td>$Submitter</td><br>";
echo "<td>$Owner</td><br>";
echo "<td>$Status</td><br>";
echo "<td>$Created</td><br>";
echo "<td>$Modified</td><br>";
echo "</tr> \n";
$i++;
}
?>
<tbody>
<?php
echo "<p><span class='label label-important'>$num P1 Issues</span></p>";
echo "</tbody></table><br>";
?>
<center><div class="alert alert-success" role="alert"><strong>No Current</strong><?php echo "<p><span class='label label-important'>$num P1 Issues</span></p>"; ?> Critical / High Priority Incidents</div></center>

Related

Expandable table in PHP

I have 4 tables as below: account_catagory, chartofaccount, general_leadger, and account_money.
Every accounts in chartofaccount has relationship with account_catagory.
Every leadger has relationship with chartofaccount.
every account_money has relationship with general_leadger.
now I want to select my all account_catagories and join it with chartofaccount, join it with general leadger, and join it with account_money which gives me lots of record.
I want to show them in expandable table so every child come under its own parent.
I wrote some code but didn't get the result as expected.
<table class="table table-condensed table-striped mt-1 p-0">
<tbody class="p-0">
<?php
$counter = 0;
$conn = new Connection();
$query = "SELECT * FROM account_catagory
LEFT JOIN chartofaccount ON account_catagory.account_catagory_id = chartofaccount.account_catagory
WHERE account_catagory.catagory = ? AND account_catagory.company_id = ?";
$result = $conn->Query($query, ["Revenue", $user_data->company_id]);
$results = $result->fetchAll(PDO::FETCH_OBJ);
foreach ($results as $item) {
$q = "SELECT * FROM general_leadger
LEFT JOIN account_money ON general_leadger.leadger_id = account_money.leadger_ID
WHERE general_leadger.recievable_id = ? OR general_leadger.payable_id = ?";
$r = $conn->Query($q, [$item->chartofaccount_id, $item->chartofaccount_id]);
$RES = $r->fetchAll(PDO::FETCH_OBJ);
$total = 0;
foreach ($RES as $LID) {
if ($item->chartofaccount_id == $LID->account_id) {
if ($LID->currency_rate != 0) {
$total += ($LID->amount * $LID->currency_rate);
} else {
$total += $LID->amount;
}
}
}
echo "<tr data-toggle='collapse' data-target='#row$counter' class='accordion-toggle p-0'>
<td>
<button class='btn btn-blue btn-xs p-0'><span class='las la-plus'></span></button>
<span>$item->account_name</span>
</td>
<td class='text-right'>$total</td>
</tr>";
$total = 0;
$counter++;
if (checkChilds($item->account_catagory_id) > 0) {
recurSearch2($user_data->company_id, $item->account_catagory_id);
}
}
?>
</tbody>
</table>
I have a function that checks if the parent has a child which is below:
function checkChilds($patne)
{
$conn = new Connection();
$query = "SELECT * FROM account_catagory WHERE parentID = ?";
$result = $conn->Query($query, [$patne]);
$results = $result->rowCount();
return $results;
}
and another function that selects all the children using the recursive function:
function recurSearch2($c, $parentID)
{
$counter = 0;
$conn = new Connection();
$query = "SELECT * FROM account_catagory
INNER JOIN chartofaccount ON account_catagory.account_catagory_id = chartofaccount.account_catagory
WHERE account_catagory.parentID = ? AND account_catagory.company_id = ?";
$result = $conn->Query($query, [$parentID, $c]);
$results = $result->fetchAll(PDO::FETCH_OBJ);
foreach ($results as $item) {
$q = "SELECT * FROM general_leadger
LEFT JOIN account_money ON general_leadger.leadger_id = account_money.leadger_ID
WHERE general_leadger.recievable_id = ? OR general_leadger.payable_id = ?";
$r = $conn->Query($q, [$item->chartofaccount_id, $item->chartofaccount_id]);
$RES = $r->fetchAll(PDO::FETCH_OBJ);
$total = 0;
foreach ($RES as $LID) {
if ($item->chartofaccount_id == $LID->account_id) {
if ($LID->currency_rate != 0) {
$total += ($LID->amount * $LID->currency_rate);
} else {
$total += $LID->amount;
}
}
}
echo "<tr class='p-0'>
<td colspan='3' class='hiddenRow'>
<div class='accordian-body collapse' id='row$counter'>
<table class='table table-striped'>
<tbody>
<tr data-toggle='collapse' class='accordion-toggle' data-target='#row$counter'>
<td>
<button class='btn btn-blue btn-xs p-0'><span class='las la-plus'></span></button>
<span>$item->account_name</span>
</td>
<td>$total</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>";
$total = 0;
$counter++;
if (checkChilds($item->account_catagory_id) > 0) {
recurSearch2($c, $item->account_catagory_id);
}
}
}
the result I get using code is below:
the result I want is as below:

Select query executing twice

I'm writing a simple page to retrieve "PO Numbers" that belong to the logged-in user. That portion is working fine with the exception of the query returning 2 complete sets of records. I have checked for page refresh issues, as mentioned in other posts I've read, but none have been found.
poTable.php
<table class="table" style="width: 100%">
<thead class="thead-dark">
<tr>
<th scope="col">PO#</th>
<th scope="col">Job#</th>
<th scope="col">Job Name</th>
<th scope="col">Salesperson</th>
<th scope="col">Vendor</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
<?php include 'usersPOs.php'?>
</tbody>
</table>
usersPOs.php
$db = mysqli_connect('localhost', 'root', '', 'acs-jobs-pos');
$userID = $_SESSION['userID'];
$userPrivs = $_SESSION['admin'];
if ($userPrivs == 0) { // User IS NOT an admin
// Get ALL main table records that belong to the logged-in user
$sqlmain = "SELECT * FROM main INNER JOIN users ON main.userID = '" . $userID . "'";
//debug_print_backtrace();
$resultmain = mysqli_query($db,$sqlmain);
while($rowmain = mysqli_fetch_assoc($resultmain))
{
?>
<tr>
<?php
$PONumb = $rowmain["PONumb"];
$jobNumber = $rowmain["jobNumber"];
$jobName = $rowmain["jobName"];
$salespersonID = $rowmain["salespersonID"];
$vendorID = $rowmain["vendorID"];
$timestamp = $rowmain["timestamp"];
?>
<td><?php echo $PONumb ?></td>
<td><?php echo $jobNumber ?></td>
<td><?php echo $jobName ?></td>
<?php
$sqlsp = "SELECT * FROM users WHERE userID = '" . $salespersonID . "'";
$resultsp = mysqli_query($db,$sqlsp);
$rowsp = mysqli_fetch_assoc($resultsp);
if ($rowsp["lastname"] == '' && $rowsp["firstname"] == '') {
?>
<td></td>
<?php
} else {
?>
<td><?php echo $rowsp["lastname"] . ', ' . $rowsp["firstname"] ?></td>
<?php
}
$sqlvendor = "SELECT * FROM vendor
WHERE vendorID = '" . $vendorID . "'";
$resultvendor = mysqli_query($db,$sqlvendor);
$rowvendor = mysqli_fetch_assoc($resultvendor);
?>
<td><?php echo $rowvendor["vendorname"] ?></td>
<td><?php echo $timestamp ?></td>
</tr>
<?php
}
mysqli_close($db);
}
This code could be hugely simplified by use of joins. I'd also recommend using PDO for your database access; it's less verbose and makes prepared statements easier (and those are not optional in any environment.) Only relevant columns are selected from the database. A heredoc block is also used to simplify output.
<?php
$db = new PDO('mysql:host=localhost;dbname=acs-jobs-pos', 'root', '');
$userID = $_SESSION['userID'];
$userPrivs = $_SESSION['admin'];
if ($userPrivs == 0) {
$sql = "SELECT m.PONumb, m.jobNumber, m.jobName, m.salespersonID, m.vendorID, m.timestamp,
u.lastname, u.firstname, v.vendorname
FROM main m
LEFT JOIN users u ON (m.salespersonID = u.userID)
LEFT JOIN vendors v ON (m.vendorID = v.vendorID)
WHERE m.userID = ?";
$stmt = $db->prepare($sql);
$stmt->execute([$userID]);
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
foreach ($results as $row) {
echo <<< HTML
<tr>
<td>$row[PONumb]</td>
<td>$row[jobNumber]</td>
<td>$row[jobName]</td>
<td>$row[lastname], $row[firstname]</td>
<td>$row[vendorname]</td>
<td>$row[timestamp]</td>
</tr>
HTML;
}
}
follow sql error
"SELECT * FROM main INNER JOIN users ON main.userID = '" . $userID . "'";
you can not use join as this, something like
"SELECT * FROM main INNER JOIN users ON main.userID = users.ID where users.ID='" . $userID . "'";

Not printing in correct columns

I have set up a for and while loop to print through the contents of my database for my menu system. It works fine, however, the places to where each of the table contents is displayed is one below where it should be. See this picture below:
The problem is that the items which are showing under "Main Courses" are supposed to be under the "Starters" section.
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 < $countArray; $i++){
echo "<h1 id='starters' class='head-font text-center head'>$result_array[$i]</h1>";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo
"
<div id='hide'>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
";
$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
"<tr>" .
"<td>$name - <small>$description</small></td>" .
"<td class='text-right'>" . "£" . $price . "</td>" .
"</tr>";
}
echo "</tbody>
</table>
</div>";
}
for($j = 1; $j < $numRows+1; $j++){
echo $j;
}
// print_r($result_array[2]);
?>

Date function not working with bind_result

I have this date format in place in my files. It works great if I do a normal SELECT query, but when I do a prepared statement query that does not have a bind_param and assign it a bind_result variable, I cannot get this to work.
Here is the function. Which it displays in US Central time. How can I get that to Eastern time?
function fixDate($strDateTime) {
$strFormat = 'M, j, Y';
$strFormatTime = '\a\t g:ia';
$intTimeStamp = strtotime($strDateTime);
$strDate = date($strFormat, $intTimeStamp);
$strTime = date($strFormatTime, $intTimeStamp);
if($strDate == date($strFormat)) {
return "Today " . $strTime;
}
elseif($strDate == date($strFormat, strtotime('yesterday'))) {
return "Yesterday " . $strTime;
}
else {
return " on " . $strDate . " " . $strTime;
}
}
The function will work if I do this with a normal SELECT query.
$date = $row2['topic_date'];
$date = fixDate($date);
BUT if I have a bind_result of $date and try to do this
$date = fixDate($date);
It won't work.
How can I get this function to work with a prepared statement's bind_result variable?
UPDATE:
This works..
$query2 = mysqli_query($con,"SELECT * FROM forum_topics
INNER JOIN forum_categories ON
forum_topics.category_id = forum_categories.id
INNER JOIN users
ON forum_topics.topic_creator = users.id
ORDER BY forum_topics.topic_reply_date DESC
LIMIT 3")
or die ("Query2 failed: %s\n".($query2->error));
$numrows2 = mysqli_num_rows($query2);
if($numrows2 > 0){
$topics .= "<table class='top_posts_table'>";
$topics .= "<tr><th class='top_posts_th'>Topic Title</th><th class='top_posts_th'>Replies</th><th class='top_posts_th'>Views</th></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
while($row2 = mysqli_fetch_assoc($query2)){
$cid = $row2['cid'];
$tid = $row2['id'];
$title = $row2['topic_title'];
$views = $row2['topic_views'];
$replies = $row['tid2'];
$date = $row2['topic_date'];
$date = fixDate($date);
$creator = $row2['username'];
$topics .= "<tr><td class='top_posts_td'><a href='forum_view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted
by: ".$creator."<br>".$date."</span></td><td class='top_posts_td'>0</td><td class='top_posts_td'>".$views."</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
This doesn't work...
if ($announcements_stmt = $con->prepare("SELECT announcements.id, announcements.user_id, announcements.message, announcements.date, users.username FROM announcements
INNER JOIN users
ON announcements.user_id = users.id")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date, $announcements_username);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
}
$announcements_stmt->store_result();
$announcements_result = array();
$announcements_date = $announcements_date;
$announcements_date = fixDate($announcements_date);
?>
<div class="index_announcements_out">
<div id="announcements_title">League Announcements:</div>
<div class="index_announcements_wrap">
<table class="index_announcements_table">
<?php
while ($row = $announcements_stmt->fetch()) {
?>
<tr class="index_announcements_border">
<td class="index_announcement_pic"></td>
<td class="index_announcement_username_td">FROM: <?php echo $announcements_username; ?><br>on <?php echo $announcements_date; ?></td>
<td class="index_announcement_message_td"><?php echo $announcements_messages; ?></td>
</tr>
Basically, when using bind_result(), you need to do a fetch() before the variables get populated. In this case, you're trying to access them before that happens.
In the given code, it's not necessary to get the fixed $announcements_date outside the while loop, so just replacing <?php echo $announcements_date; ?> with <?php echo fixDate($announcements_date); ?> in the loop should do the trick.

How to include Ajax request / response for php project

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

Categories