Load more button value according "WHERE" Clouse condition - php

I am using load more button to load content from the database of specific id on the same web page. Here is the code:
<div class="postList col-lg-12">
<legend><h1 style="color:#298208;">Savings Bucks Details</h1> </legend>
<?php
$busi_id = mysqli_real_escape_string($conn, $_SESSION['busi_id']);
if (isset($busi_id)) {
$query = "SELECT * FROM savingsbucks_business WHERE busi_id='$busi_id' ORDER BY sbb_id DESC LIMIT 2";
$result = mysqli_query($conn, $query) or die('Query failed: ' . mysqli_error($conn));
$numrows_savingsbucks = mysqli_num_rows($result);
if ($numrows_savingsbucks == '0') {
echo "<p style='text-align:center; color:#ff4400; margin-top:40px;'>No Data available!</p>";
} else {
?>
<div class="table-responsive">
<table border="1" class="table table-bordered">
<thead>
<th class="consumer_point_text">Shop ID</th>
<th class="consumer_point_text">SenderID</th>
<th class="consumer_point_text">Busi ID</th>
<th class="consumer_point_text">Customer Type</th>
<th class="consumer_point_text">Customer Name</th>
<th class="consumer_point_text">Customer Email</th>
<th class="consumer_point_text">Customer Phone</th>
<th class="consumer_point_text">Two</th>
<th class="consumer_point_text">Five</th>
<th class="consumer_point_text">Ten</th>
<th class="consumer_point_text">Twenty</th>
<th class="consumer_point_text">Fifty</th>
<th class="consumer_point_text">Hundred</th>
<th class="consumer_point_text">Five Hundred</th>
<th class="consumer_point_text">Total Savings Bucks</th>
</thead>
<?php if($numrows_savingsbucks > 0){
while ($row = mysqli_fetch_array($result)) {
$sbb_id = $row['sbb_id'];
$sender_id = $row['sender_id'];
$busi_id = $row['busi_id'];
$type = $row['type'];
$consu_name = $row['consu_name'];
$consu_email = $row['consu_email'];
$consu_phone = $row['consu_phone'];
$two = $row['two'];
$five = $row['five'];
$ten = $row['ten'];
$twenty = $row['twenty'];
$fifty = $row['fifty'];
$hundred = $row['hundred'];
$five_hundred = $row['five_hundred'];
$total_two += $two;
$total_five += $five;
$total_ten += $ten;
$total_twenty += $twenty;
$total_fifty += $fifty;
$total_hundred += $hundred;
$total_five_hundred += $five_hundred;
$total_bucks = $two+$five+$ten+$twenty+$fifty+$hundred+$five_hundred;
$grand_total += $total_bucks;
?>
<tr>
<td class="consumer_point_text"><?=$sbb_id?></td>
<td class="consumer_point_text"><?=$sender_id?></td>
<td class="consumer_point_text"><?=$busi_id?></td>
<td class="consumer_point_text" style="text-transform:capitalize;"><?=$type?></td>
<td class="consumer_point_text"><?=$consu_name?></td>
<td class="consumer_point_text"><?=$consu_email?></td>
<td class="consumer_point_text"><?=$consu_phone?></td>
<td class="consumer_point_text"><?=$two?></td>
<td class="consumer_point_text"><?=$five?></td>
<td class="consumer_point_text"><?=$ten?></td>
<td class="consumer_point_text"><?=$twenty?></td>
<td class="consumer_point_text"><?=$fifty?></td>
<td class="consumer_point_text"><?=$hundred?></td>
<td class="consumer_point_text"><?=$five_hundred?></td>
<td class="consumer_point_text"><?=$total_bucks?></td>
</tr>
<?php } ?>
</table>
<div class="show_more_main" sbb_id="show_more_main<?php echo $sbb_id; ?>">
<span sbb_id="<?php echo $sbb_id; ?>" class="show_more" title="Load more posts">Load more</span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script
<script src="http://demos.codexworld.com/includes/js/bootstrap.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var ID = $(this).attr('sbb_id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'ajax_more_business_shop.php',
data:'sbb_id='+ID,
success:function(html){
$('#show_more_main'+ID).remove();
$('.postList').append(html);
}
});
});
});
</script>
<?php } ?>
</div> <!-- ./col-lg-12 -->
<?php } }?>
On this page its displaying 2 results which have common busi_id (WHERE busi_id=$busi_id), when I click on load more button, its displaying others content too which don't have same "busi_id" as on this page showing first 2 results.
Here is ajax page:
ajax_more_business_shop.php :
<?php
ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
if(!empty($_POST["sbb_id"])) {
require_once ('admin/includes/config.php');
$query = "SELECT COUNT(*) as num_rows FROM savingsbucks_business WHERE sbb_id < ".$_POST['sbb_id']." ORDER BY sbb_id DESC";
$result = mysqli_query ($conn, $query);
$row = mysqli_fetch_assoc($result);
$totalRowCount = $row['num_rows'];
$busi_id = $row['busi_id'];
$showLimit = 2;
$query = "SELECT * FROM savingsbucks_business WHERE sbb_id < ".$_POST['sbb_id']." ORDER BY sbb_id DESC LIMIT $showLimit";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$rowcount=mysqli_num_rows($result);
?>
<table border="1" class="table table-bordered">
<?php
if ($rowcount > '0') {
while($row = mysqli_fetch_assoc($result))
{
$sbb_id = $row['sbb_id'];
$sender_id = $row['sender_id'];
$busi_id = $row['busi_id'];
$type = $row['type'];
$consu_name = $row['consu_name'];
$consu_email = $row['consu_email'];
$consu_phone = $row['consu_phone'];
$two = $row['two'];
$five = $row['five'];
$ten = $row['ten'];
$twenty = $row['twenty'];
$fifty = $row['fifty'];
$hundred = $row['hundred'];
$five_hundred = $row['five_hundred'];
$total_two += $two;
$total_five += $five;
$total_ten += $ten;
$total_twenty += $twenty;
$total_fifty += $fifty;
$total_hundred += $hundred;
$total_five_hundred += $five_hundred;
$total_bucks = $two+$five+$ten+$twenty+$fifty+$hundred+$five_hundred;
$grand_total += $total_bucks;
?>
<tr>
<td class="consumer_point_text"><?=$sbb_id?></td>
<td class="consumer_point_text"><?=$sender_id?></td>
<td class="consumer_point_text"><?=$busi_id?></td>
<td class="consumer_point_text"><?=$type?></td>
<td class="consumer_point_text"><?=$consu_name?></td>
<td class="consumer_point_text"><?=$consu_email?></td>
<td class="consumer_point_text"><?=$consu_phone?></td>
<td class="consumer_point_text"><?=$two?></td>
<td class="consumer_point_text"><?=$five?></td>
<td class="consumer_point_text"><?=$ten?></td>
<td class="consumer_point_text"><?=$twenty?></td>
<td class="consumer_point_text"><?=$fifty?></td>
<td class="consumer_point_text"><?=$hundred?></td>
<td class="consumer_point_text"><?=$five_hundred?></td>
<td class="consumer_point_text"><?=$total_bucks?></td>
</tr>
<?php }
?>
</table>
<?php if($totalRowCount > $showLimit){ ?>
<div class="show_more_main" sbb_id="show_more_main<?php echo $sbb_id; ?>">
<span sbb_id="<?php echo $sbb_id; ?>" class="show_more" title="Load more posts">Show more</span>
<span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>
<?php } ?>
<?php
}
}
?>
I want, when i click "load more button", It must show/load 2 more row of content where busi_id will be common, I mean if busi_id=69, it should show/load only 2 more content/result from database which have busi_id 69, not other data with different busi_id.
I tried to explain,
Thank you.

I have just passed the "busi_id" to Ajax page... with load more button..
<div class="show_more_main" ds_id="show_more_main<?php echo $ds_id; ?>">
<span ds_id="<?php echo $ds_id; ?>" busi_id="<?php echo $busi_id; ?>" class="show_more" title="Load more posts">Load more</span>
<span class="" style="display: none;"><span class="loding_txt">Loading...</span></span>
</div>
And..
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','.show_more',function(){
var ID = $(this).attr('ds_id');
var Busi_id = $(this).attr('busi_id');
$('.show_more').hide();
$('.loding').show();
$.ajax({
type:'POST',
url:'ajax_more_consumer_shop.php',
data: {
'ds_id': ID,
'busi_id': Busi_id
},
success:function(html){
$('#show_more_main'+ID).remove();
$('.postList').append(html);
}
});
});
});
on ajax_more_business_shop.php....
$query = "SELECT COUNT(*) as num_rows FROM savingsbucks_business WHERE ds_id < ".$_POST['ds_id']." AND busi_id=".$_POST["busi_id"]." ORDER BY ds_id DESC";
$result = mysqli_query ($conn, $query);
$row = mysqli_fetch_assoc($result);
$totalRowCount = $row['num_rows'];
$showLimit = 2;
$query = "SELECT * FROM savingsbucks_business WHERE ds_id < ".$_POST['ds_id']." AND busi_id=".$_POST["busi_id"]." ORDER BY ds_id DESC LIMIT $showLimit";
$result = mysqli_query ($conn, $query);
$rowcount=mysqli_num_rows($result);
Modified the query.. and get the desired output.
Thanks!

Related

How to pass article id to pangination page with ajax?

I am trying to do a pagination for category result page which is listing articles with ajax.
I use where clause and need to pass article id to the ajax page.
My codes : ajax.php
include_once '../db.php';
include_once '../class/Articles.php';
$ArticleHandler = new Articles($pdo);
if (!(isset($_GET['pageNumber']))) {
$pageNumber = 1;
} else {
$pageNumber = $_GET['pageNumber'];
}
$perPageCount = 5;
$catid = $_POST['article_id'];
$sql = "SELECT * FROM posts WHERE pcat = ?";
$posts = $ArticleHandler->getPosts($sql, [$catid])->fetchAll();
if ($posts !== '0') {
$rowCount = $ArticleHandler->getPosts("SELECT count(*) FROM posts")->fetchColumn();
}
$pagesCount = ceil($rowCount/$perPageCount);
$lowerLimit = ($pageNumber - 1) * $perPageCount;
$sql = "SELECT * FROM posts WHERE pcat = :pcat ORDER BY pid DESC LIMIT :offset, :limit";
$results = $ArticleHandler->getPosts($sql, array(':pcat'=>$catid, ':offset'=>$lowerLimit, ':limit'=>$perPageCount))->fetchAll();
?>
<table class="table table-hover table-responsive">
<tr>
<th align="center">Name</th>
<th align="center">Experience<br>(in years)
</th>
<th align="center">Subject</th>
</tr>
<?php foreach ($results as $data) { ?>
<tr>
<td align="left"><?php echo $data['ptitle'] ?></td>
</tr>
<?php
}
?>
</table>
<div style="height: 30px;"></div>
<table width="50%" align="center">
<tr>
<td valign="top" align="left"><?php echo $catid; ?></td>
<td valign="top" align="center">
<?php
for ($i = 1; $i <= $pagesCount; $i ++) {
if ($i == $pageNumber) {
?>
<?php echo $i ?>
<?php
} else {
?>
<a href="javascript:void(0);" class="pages"
onclick="showRecords('<?php echo $perPageCount; ?>', '<?php echo $i; ?>');"><?php echo $i ?></a>
<?php
}
}
?>
</td>
<td align="right" valign="top">
Page <?php echo $pageNumber; ?> of <?php echo $pagesCount; ?>
</td>
</tr>
</table>
And index.php
<div id="results"></div>
<div id="loader"></div>
Ajax codes :
function showRecords(perPageCount, pageNumber) {
$.ajax({
type: "GET",
url: "modules/ajax.php",
data: "pageNumber=" + pageNumber,
cache: false,
beforeSend: function() {
$('#loader').html('<img src="loader.png" alt="reload" width="20" height="20" style="margin-top:10px;">');
},
success: function(html) {
$("#results").html(html);
$('#loader').html('');
}
});
}
$(document).ready(function() {
showRecords(10, 1);
});
My tries : I've added a div with artcile id to index.php and used attr to pass it to ajax but couldn't pass it to the page.
Try passing the article id along with pageNumber that you are passing in GET request.
data: {pageNumber : YOUR_PAGE_NUMBER, articleId: ARTICLE_ID}
Do not forget to pass the articleId as 3rd parameter to the function.

Multiple Images Uploaded Remove Selected Image on Multiple Images in PHP MySqli

Hello Every One i have small problem i will upload multiple images single input file and database store with comma i will stored but i have problem is remove the uploaded images selected and remove the image when we click the image above delete icon that only image deleted
My Code for Retrieve the Multiple images
<div class="panel-body">
<div class="table-responsive">
<table id="dataTableExample1" class="table table-striped table-bordered">
<thead>
<tr>
<th>S.No</th>
<th>Gallery Images</th>
<th>Gallery Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<?php
extract($_REQUEST);
$sql="SELECT * FROM `smw_gallery`";
$result = $conn->query($sql);
$count=$result->num_rows;
if ($count > 0) {
$i=1;
while ($row = $result->fetch_object()) {
$primages = $row->smw_gallery_images;
$imgp = explode(",", $primages);
$realPath = '../assets/images/gallery/';
?>
<tr>
<td style="width:10%"><?=$i;?></td>
<td style="width:50%">
<?php foreach($imgp as $img)
{
echo '<a class="example-image-link" href="#" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="'.$realPath.'/'.$img.'" alt="" height="80" width="80" style="margin: 10px;"/></a>';
} ?>
</td>
<td style="width:10%">
<?php
$limit = 30;
$td_title1 = $row->smw_gallery_name;
if (strlen($td_title1) > $limit)
$td_title1 = substr($td_title1, 0, strrpos(substr($td_title1, 0, $limit), ' '))."...";
echo $td_title1;
?></td>
<td style="width:20%">
<center>
<i class="fa fa-pencil-square-o btn btn-warning" aria-hidden="true"></i>
</center>
</td>
</tr>
<?php $i++; } } ?>
</tbody>
</table>
</div>
</div>
the above code Output look like this
each image i will place delete button and hole gallery delete button
how to made to single image delete button and remove that image only remain will be as it is display
You can do it with small help of jquery, ajax and mysql.
On click delete icon you have to make one ajax request with image name and id parameters. Update image name in database using below query. On success ajax response you can remove that image block using jquery.
Html code for image. Just a sample line.
DeleteIcon
Jquery Code
$(document).on('click', '.delete-image', function(){
var $this = $(this);
var imagname = $(this).data('name');
$.post("delete_image.php",
name: imagname,
id: 1
},
function(data, status){
$this.closest(tr).remove(); //Write your remove code for single image
});
})
I have write code is look like this :
<img class="btn-delete" id="photo-<?=$photo_index_key;?>" data-id="<?=$photo_index_key;?>" data-name="<?=$photo_name;?>" style="margin: 3px 1px 74px -17px; cursor: pointer;" src="../assets/images/closes.png";>
each one of indexkey value can taken in jquery var $imageId = $(this).attr('id');
<script>
$(document).on('click', '.btn-delete', function(){
var imageId = $(this).attr('data-id');
var imageName = $(this).attr('data-name');
var dataString = {id:imageId, name: imageName}
$.ajax({
type: "POST",
url: "remove.php",
data: dataString,
cache: false,
success: function(html){
$('#photo'+imageId).remove(); // you can write your logic
}
});
});
</script>
remove.php
//get ajax data:
$id = $POST['id'];
$name = $POST['name'];
UPDATE smw_gallery
SET `smw_gallery_images` = REPLACE(`smw_gallery_images`, $name,'')
WHERE `id` = $id;
What if you do it in appearance.
That is, you can define the array by indexes
<div class="panel-body">
<div class="table-responsive">
<table id="dataTableExample1" class="table table-striped table-bordered">
<thead>
<tr>
<th>S.No</th>
<th>Gallery Images</th>
<th>Gallery Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<?php
extract($_REQUEST);
$sql = "SELECT * FROM `smw_gallery`";
$result = $conn->query($sql);
$count = $result->num_rows;
if ($count > 0) {
$i = 1;
while ($row = $result->fetch_object()) {
$primages = $row->smw_gallery_images;
$imgp = explode(",", $primages);
$realPath = '../assets/images/gallery/';
?>
<tr>
<td style="width:10%"><?= $i; ?></td>
<td style="width:50%">
<?php foreach ($imgp as $photo_index_key => $img) {
echo '<a class="example-image-link" href="#" data-lightbox="example-set" data-title="Click the right half of the image to move forward.">
<img class="example-image" src="' . $realPath . '/' . $img . '" alt="" height="80" width="80" style="margin: 10px;"/>
</a>';
echo "<a href='".$realPath . "/remove.php?smw_gallery_id=" . $row->id . "&photo_index_key=" . $photo_index_key . "'></a>";
} ?>
</td>
<td style="width:10%">
<?php
$limit = 30;
$td_title1 = $row->smw_gallery_name;
if (strlen($td_title1) > $limit)
$td_title1 = substr($td_title1, 0, strrpos(substr($td_title1, 0, $limit), ' ')) . "...";
echo $td_title1;
?></td>
<td style="width:20%">
<div style="text-align: center;">
<a href="gallery-edit.php?edit=<?= $row->smw_gallery_id; ?>" title="Edit"><i
class="fa fa-pencil-square-o btn btn-warning" aria-hidden="true"></i></a>
</div>
</td>
</tr>
<?php $i++;
}
} ?>
</tbody>
</table>
</div>
I don't know English. This was done via google translate
remove.php
<?php
if (!empty($_GET['smw_gallery_id'] && !empty($_GET['photo_index_key']))) {
$sql = sprintf("SELECT `smw_gallery_images` FROM `smw_gallery` WHERE `id` = %d", $_GET['smw_gallery_id']);
$result = $conn->query($sql);
$result->fetch_assoc();
if (!is_null($result) && is_array($result)) {
while ($row = $result->fetch_assoc()) {
$smw_gallery_images = explode(",", $row['smw_gallery_images']);
$new_smw_gallery_images = array_splice($smw_gallery_images, $_GET['photo_index_key'], 1);
$new_smw_gallery_images = implode(',', $new_smw_gallery_images);
$updateSql = sprintf("UPDATE smw_gallery SET `smw_gallery_images` = %s WHERE `id` = %d", $new_smw_gallery_images, $_GET['smw_gallery_id']);
$conn->query($updateSql);
}
}
}

PHP : Failed to delete multiple selected data

Currently, I created a system that enables the user to delete multiple data by using the checkbox.
The step is like this:
1) user select team, time before and time after at dashboard.php. The user click button 'Search' to display the result. The display result use AJAX. AJAX will redirect to range.php
2) User can select any data row that displays and delete.
My problem is, delete function is failed although I do the correct step and code. Below is the current code:
range.php
<?php
require_once "../../../config/configPDO.php";
require_once "../../../config/check.php";
$email = $_SESSION['login_user'];
if(isset($_POST["From"], $_POST["to"], $_POST["team"]))
{
$result = '';
$query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid WHERE ot_users.team_id = '".$_POST['team']."' AND report_date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."' ORDER BY ot_report.report_date DESC";
$sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$sql -> execute();
if(isset($_POST['save'])){
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql2 = "DELETE FROM ot_report WHERE report_id=:report_id";
$query2 = $conn->prepare($sql2);
$query2->execute(array(':report_id' => $del_id));
header("Location: dashboard.php");
}
}
if($sql->rowCount() > 0)
{
echo'
<form method="post" action="">
<div><u>PDF View</u></div><br>
<div class="row" style="height: 300px; overflow-y: scroll;">
<div class="col-lg-12 grid-margin stretch-card">
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "10%"><input type="checkbox" id="checkAl"> All</th>
<th width = "3%">id</th>
<th width = "15%">Date</th>
<th width = "25%">Supervisor</th>
<th width = "30%">Task Name</th>
<th width = "10%">Status</th>
<th width = "7%">Action</th>
</tr>
</thead>
<tbody>';
$i=0;
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
$datereport = $row['report_date'];
$datereport2 = strtotime($datereport);
$report_date = date('d M Y', $datereport2);
$status=$row['report_status'];
if($status=="Pending")
{
$color="color:blue";
}
else
{
$color="color:green";
}
if ($row['ot_start'] == '00:00:00'){
$ot_start = '-';
}else{}
if ($row['ot_end'] == '00:00:00'){
$ot_end = '-';
}else{}
echo'<tr>';
echo '<td><input type="checkbox" id="checkItem" name="check[]" value='.$row['report_id'].'></td>';
echo '<td>'.$row["report_id"].'</td>';
echo '<td>'.$report_date.'</td>';
echo '<td>'.$row["fullname"].'</td>';
echo '<td>'.$row["task_name"].'</td>';
echo '<td align="center" style='.$color.'><strong>'.$status.'</strong></td>';
echo '<td align="center">';
echo '<a class="btn-view btn-primary btn-sm" href="view_task/view_task.php?report_id='. $row["report_id"] .'" data-toggle="tooltip">View</a>';
echo '<a class="btn-view btn-danger btn-sm" href="delete.php?report_id='. $row["report_id"] .'" onClick=\'return confirm("Do you want to remove team?")\'>Delete</a></td>';
echo '</td>';
echo '</tr>';
$i++;
}
echo '<tr>';
echo '<td><p align="center"><button type="submit" class="btn-danger btn-sm" name="save">DELETE</button></p></td>';
echo '</tr>';
echo '</form>';
}
else
{
echo '
<table class = "table-bordered" width = "100%">
<thead>
<tr>
<th width = "5%">id</th>
<th width = "12%">Date</th>
<th width = "29%">Supervisor</th>
<th width = "23%">Task Name</th>
<th width = "7%">From</th>
<th width = "7%">To</th>
<th width = "10%">Status</th>
<th width = "7%">Action</th>
</tr>
<tr>
<td colspan="8">No report found</td>
</tr>';
}
echo '</body></table></div></div>';
}
?>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
Can anyone knows how to solve the problem?
$sql2 = "DELETE FROM ot_report WHERE report_id=:report_id";
Try changing this to:
$query = "DELETE FROM ot_report WHERE report_id LIKE ?";
$sql2 = $conn->prepare($query);
$sql2->bind_param("i", $del_id);
$sql2->execute();

PHP Mysql query optimization for report

I have this report which filters data from 5 tables but it is very slow takes around 10 seconds. I tried using index on some columns but it did not help.
Basically the first query is the master and the others are for filtering it if the other queries condition met then it will skip it.
this is the script:
<div class="col-md-12">
<h3>List of Outstandings</h3>
<table class="table table-condensed table-bordered table-hover small">
<thead>
<tr>
<th >#</th>
<th>PR #</th>
<th>PR Type</th>
<th>Description</th>
<th>Dep.</th>
<th>Date</th>
<th>Requester</th>
<th>Assigned to</th>
</tr>
</thead>
<tbody>
<?php
$chkk = 0;
$sql = "SELECT * FROM msr WHERE Status ='Approved' ";
$result6 = $connn->query($sql);
if ($result6->num_rows > 0) {
$vo = 1;
while ($row0 = $result6->fetch_assoc()) {
$chkk = 0;
$MSRID = $row0["MSRID"];
$MSRType = $row0["MSRType"];
$result4 = "SELECT owner FROM tracking WHERE MSRID='$MSRID' ";
$MyRow4 = $connn->query($result4);
$row4 = $MyRow4->fetch_assoc();
$actionBy = $row4["owner"];
$resultusr = "SELECT RFQID FROM rfq WHERE MSRID='$MSRID' AND NOPO='No' ";
$MyRowusr = $connn->query($resultusr);
$rowusr = $MyRowusr->fetch_assoc();
$rfqcount = mysqli_num_rows($MyRowusr);
if ($rfqcount > 0) {
$chkk = 1;
}
$resultusr4 = "SELECT POID FROM po WHERE MSRID='$MSRID' ";
$MyRowusr4 = $connn->query($resultusr4);
$rowusr4 = $MyRowusr4->fetch_assoc();
$rfqcount4 = mysqli_num_rows($MyRowusr4);
if ($rfqcount4 > 0) {
$chkk = 1;
}
$resultusr1 = "SELECT MSRID FROM contract WHERE MSRID='$MSRID' ";
$MyRowusr1 = $connn->query($resultusr1);
$rowusr1 = $MyRowusr1->fetch_assoc();
$rfqcount1 = mysqli_num_rows($MyRowusr1);
if ($rfqcount1 > 0) {
$chkk = 1;
}
if ($chkk == 1) {
continue;
}
?>
<tr>
<td>
<?php echo $vo; ?>
</td>
<td>
<?php echo $row0["MSRID"]; ?>
</td>
<td>
<?php echo $row0["MSRType"]; ?>
</td>
<td>
<?php echo $row0["purposeofbuying"]; ?>
</td>
<td>
<?php echo depName($row0["DepRequester"]); ?>
</td>
<td>
<?php echo $row0["RequestDate"]; ?>
</td>
<td>
<?php echo reqName($row0["RequestPer"]); ?>
</td>
<td>
<?php echo reqName($actionBy); ?>
</td>
</tr>
<?php
$vo++;
}
}
?>
</tbody>
</table>
</div>
</div>
You can use subquery method instead of looping.
Example:
$sql = "SELECT *,
( SELECT owner
FROM tracking
WHERE tracking.MSRID= msr.MSRID
) AS _owner,
( SELECT RFQID
FROM rfq
WHERE rfq.MSRID= msr.MSRID
AND rfq.NOPO='No'
) AS _RFQID
FROM msr
WHERE rfq.Status ='Approved'";

PHP - Incorrect formatting

I am currently working through a PHP textbook to create a movie review website, however, the format of the page I have created is incorrect and I can't see the difference between my code and the code in the textbook. Here is a screenshot of the page I have created:
http://imgur.com/a/EBNxk
"Date" is supposed to be a table header alongside "Reviewer", "Comments" and "Rating". The "Reviews" header is also meant to be below the movie details and above "Date", "Reviewer", "Comment", and "Rating". The full code of the page is included below, I know the problem is probably going to be between the html tags but I'll include it all just incase.
EDIT: I see now that I left a quote mark out on line 130. Fixing this puts "Date" along with the other headers as it should be. But the Reviews header is still above the movie details.
<?php
function get_director($director_id)
{
global $db;
$query = "SELECT people_fullname FROM people WHERE people_id = ". $director_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $people_fullname;
}
function get_leadactor($leadactor_id)
{
global $db;
$query = "SELECT people_fullname FROM people WHERE people_id = ". $leadactor_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $people_fullname;
}
function get_movietype($type_id)
{
global $db;
$query = "SELECT movietype_label FROM movietype WHERE movietype_id = ". $type_id;
$result = mysqli_query($db,$query) or die(mysqli_error($db));
$row = mysqli_fetch_assoc($result);
extract($row);
return $movietype_label;
}
function generate_stars($rating)
{
$stars = " ";
for($i = 0; $i <= $rating; $i++)
{
$stars.= '<img src = "star.png" alt = "star " />';
}
return $stars;
}
function calculate_difference($takings,$cost)
{
$difference = $takings - $cost;
if($difference<0)
{
$color = "red";
$difference = "$ ".abs($difference)." million";
}
else if ($difference > 0)
{
$color = "green";
$difference = "$ ".abs($difference)." million";
}
else
{
$color = "blue";
$difference = "$ ".abs($difference)." million";
}
return "<span style = \"color:".$color.";\">".$difference."</span>";
}
$db = mysqli_connect('localhost','root') or die('Unable to connect');
mysqli_select_db($db,'moviesite') or die (mysqli_error($db));
$query = "SELECT movie_name, movie_year, movie_director, movie_leadactor,
movie_type, movie_running_time, movie_cost, movie_takings
FROM movie
WHERE movie_id = ".$_GET["movie_id"];
$result = mysqli_query($db,$query);
$row = mysqli_fetch_assoc($result);
$movie_name = $row['movie_name'];
$movie_director = get_director($row['movie_director']);
$movie_leadactor = get_leadactor($row['movie_leadactor']);
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_takings = $row['movie_takings']." million";
$movie_cost = $row['movie_cost']." million";
$movie_health = calculate_difference($row['movie_takings'],$row['movie_cost']);
echo <<<ENDHTML
<html>
<head>
<title> Details for $movie_name </title>
</head>
<body>
<div style = "text-align:center">
<h2> $movie_name </h2>
<h3> Details </h3>
<table cellpadding = "2" cellspacing = "2" style = "width:70%; margin-left:auto; margin-right:auto">
<tr>
<td> <strong> Title </strong></td>
<td> $movie_name </td>
<td> <strong> Release Year </strong></td>
<td> $movie_year </td>
</tr>
<tr>
<td> <strong> Movie Director </strong></td>
<td> $movie_director </td>
<td> <strong> Cost </strong></td>
<td> $movie_cost </td>
</tr>
<tr>
<td> <strong> Lead Actor </strong></td>
<td> $movie_leadactor </td>
<td> <strong> Takings </strong></td>
<td> $movie_takings </td>
</tr>
<tr>
<td> <strong> Running Time </strong></td>
<td> $movie_running_time </td>
<td> <strong> Health </strong></td>
<td> $movie_health </td>
</tr>
ENDHTML;
$query = 'SELECT review_movie_id, review_date, reviewer_name, review_comment, review_rating
FROM reviews
WHERE review_movie_id = '.$_GET['movie_id'] . '
ORDER BY review_date DESC';
$result = mysqli_query($db, $query) or die(mysqli_error($db));
echo <<<ENDHTML
<h3><em>Reviews</em></h3>
<table cellpadding = "2" cellspacing = "2"
style = "width:90%; margin-left:auto; margin-right:auto;>
<tr>
<th style = "width: 7em"> Date </th>
<th style = "width: 10em"> Reviewer </th>
<th> Comments </th>
<th style = "width: 5em"> Rating </th>
</tr>
</table>
ENDHTML;
while($row = mysqli_fetch_assoc($result))
{
$date = $row['review_date'];
$name = $row['reviewer_name'];
$comment = $row['review_comment'];
$rating = generate_stars($row['review_rating']);
echo <<<ENDHTML
<td style = "vertical-align: top; text-align:center"> $date </td>
<td style = "vertical-align: top; text-align:center"> $name </td>
<td style = "vertical-align: top; text-align:center"> $comment </td>
<td style = "vertical-align: top; text-align:center"> $rating </td>
<br/>
ENDHTML;
}
echo <<< ENDHTML
</div>
</body>
</html>
ENDHTML;
?>
Try closing the quote marks on style:
<table cellpadding = "2" cellspacing = "2" style = "width:90%; margin-left:auto; margin-right:auto;">
<tr>
<th style = "width: 7em"> Date </th>

Categories