PHP - Incorrect formatting - php

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>

Related

Load more button value according "WHERE" Clouse condition

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!

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/mysql basic search not returing results

I have a test query I am performing and it seems to be running OK, however not returning any data within the rows of the tables when using a varible passed or a direct set year for the table
<?
$curyr = date("Y");
isset($_GET['year']) ? $year = $_GET['year'] : $year = "";
$server = $_SERVER['PHP_SELF'];
$dbhost = "localhost";
$dbuname = "";
$dbpass = "";
$dbname = "";
$conn = new mysqli($dbhost,$dbuname,$dbpass,$dbname);
if($conn->connect_errno):
die("$conn->connect_error \n");
endif;
echo ("<title>$year</title>");
if (($year >= 2001) && ($year <=$curyr)) {
echo ("
<table border='0' align='center' width='100%'>
<tr>
<td>
<table border='1' align='center' width='60%' style='border:1 solid #000000; border-collapse: collapse'>
<tr>
<th align='center' colspan='5'>
DETAIL
</th>
</tr>");
$query = "SELECT c.* , p.* FROM $year c, dataset p WHERE c.id = p.id ORDER BY p.name ASC";
$r = $conn->query($query);
while ($row = $r->fetch_assoc()) {
echo ("
<tr>
<td>
$row[namepre]$row[name]
</td>
<td>
</td>
<td>
TEST
</td>
<td>
</td>
</tr>");
}
echo ("
</table>
</td>
</tr>
</table>");
} else {
echo ("ERROR NO ROWS");
}
$conn->close();
?>
I'm returning the "detail" portion of the top of the sample table but nothing else. This is just basic but wondering where I'm possibly missing something.

Will a LIKE function give me better results when Match Against function does not give any results in Mysql

Currently, When I type 'M' in the search engine I do not get any results inspite of having a 'Math' value in my database. (I am using Match Against function of mysql on my website). So I decided to make a code that first runs a Match Against function and if no output is obtained, it again runs the search but this time using 'LIKE' function of my sql and if still no result is obtained it shows alert 'no values found'.
the gist of code is somewhat like this..
mysql(Match against function)
If (mysqli_num_rows > 0)
{output}
elseif (mysqli_num_rows < 1)
{ mysqli (LIKE function)
then show output}
else {show alert'no results'}
Will this code give the result 'Math' when I enter 'M' in the search engine.
The actual code.
<?php
if (isset($_POST['go']))
{ $search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search))
{
if(isset($_POST['colleges']) )
{
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
}
else
{
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
$searchresults = mysqli_num_rows($query_run1);
?><p class = "totalresults"> <br />
<br /> &nbsp &nbsp <font size= "4" >Total Results :
<?php
echo $searchresults ;
?>
</font> </p>
<?php
if(mysqli_num_rows($query_run1)>0)
{
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
elseif(mysqli_num_rows($query_run1) < 1)
{
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?> <table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
}
?>
</table>
<?php
}
else
{
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
else
{ echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
}
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else { lined up with another } else {).
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea.
It helps us read the code and more importantly it will help you debug your code.
Take a quick look at a coding standard for your own benefit.
You may be asked to amend this code in a few weeks/months and you will thank me in the end.
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
&nbsp &nbsp <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the } was ending.
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend PDO, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
One way around could also be to only use full text, if the user types more than 2 chars.
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.

Unexpected output in php function

Here is a simple function which i made on PHP. The problem is that it is giving totally unexepected output.
From my database it takes $takings = 242 and $cost = 81 so it should show 242-81=161 as answer. but instead it is showing 242. What can be the problem ?
function differences($takings,$cost)
{
$difference = $takings - $cost;
if($difference < 0)
{
$color = red;
$difference = '$'.$difference.'million';
}
elseif($difference > 0)
{
$color = green;
$difference = '$'.$difference.='million';
}
elseif($difference = 0)
{
$color = blue;
$difference = "broken even ";
}
return '<span style="color:'.$color.';">'.$difference.'</span>';
}
Guys. here is the complete code for the page. When open the page in the browser it shows this :
<?php
function getdirector($director)
{
global $db;
$query = 'select name from peopledb where peopleid = '.$director;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}
function getactor($actor)
{
$query = 'select name from peopledb where peopleid = '.$actor;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}
function getmovietype($type)
{
$query = 'select movietype from movietype where movieid = '.$type;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype;
}
function differences($takings,$cost)
{
$difference = $takings - $cost;
if($difference < 0)
{
$color = red;
$difference = '$'.$difference.'million';
}
elseif($difference > 0)
{
$color = green;
$difference = '$'.$difference.='million';
}
elseif($difference = 0)
{
$color = blue;
$difference = "broken even ";
}
return '<span style="color:'.$color.';">'.$difference.'</span>';
}
$abc = $_GET['movieid'];
$db = mysql_connect('localhost','root','saw123') or die('Connection error');
mysql_select_db("moviesite",$db) or die(mysyql_error($db));
$query = "select moviename,releaseyear,director,actor,type,movierunningtime,moviecost,movietakings from movie where movieid = ". $abc;
$result = mysql_query($query,$db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
$movie_name = $row['moviename'];
$movie_director = getdirector($row['director']);
$movie_releaseyear = $row['releaseyear'];
$movie_actor = getactor($row['actor']);
$movie_type = getmovietype($row['type']);
$movie_runningtime = $row['movierunningtime'].' minutes';
$movie_cost =$row['moviecost'].'million';
$movie_takings = $row['movietakings'].'million';
$movie_health = differences($row['movietakings'],$row['movie_cost']);
echo <<<ENDHTML
<html>
<head>
<title> Details and Reviews for the movies for $movie_name </title>
</head>
<body>
<div style="text-align: center;">
<h2>$movie_name</h2>
<h3> details </h3>
<table cellpadding = "1" cellspacing = "4"
style = "width = 80% ;margin-left: auto;margin-right: auto;";>
<tr>
<td><strong>Title</strong></strong></td>
<td>$movie_name</td>
</tr>
<tr>
<td><strong>release date</strong></strong></td>
<td>$movie_releaseyear</td>
</tr>
<tr>
<tr>
<td><strong>movie director</strong></strong></td>
<td>$movie_director</td>
</tr>
<tr>
<tr>
<td><strong>Lead Actor</strong></strong></td>
<td>$movie_actor</td>
</tr>
<tr>
<tr>
<td><strong>Running time</strong></strong></td>
<td>$movie_runningtime</td>
</tr>
<tr>
<tr>
<td><strong>Cost</strong></strong></td>
<td>$movie_cost</td>
</tr>
<tr>
<tr>
<td><strong>Takings</strong></strong></td>
<td>$movie_takings</td>
</tr>
<tr>
<tr>
<td><strong>Health</strong></strong></td>
<td>$movie_health</td>
</tr>
<tr>
</table>
</div>
</body>
</html>
ENDHTML;
$table = <<<ENDHTML
<div style="text-align: center;">
<h2>The Ultimate movie database</h2>
<table border='1' cellpadding='1' cellspacing='2'
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie ID </th>
<th>Movie title</th>
<th>year of release </th>
<th>Movie director</th>
<th>Movie Actor</th>
<th>Movie type</th>
</tr>
ENDHTML;
?>
$movie_health = differences($row['movietakings'],$row['movie_cost']);
should be
$movie_health = differences($row['movietakings'],$row['moviecost']);
Notice the last variable.
$movie_cost =$row['moviecost'].'million';
$movie_takings = $row['movietakings'].'million';
$movie_health = differences($row['movietakings'],$row['movie_cost']);
To clarify my earlier answer: note the presences of an underscore in 'movie_cost', and the lack of one in 'moviecost'.
As #dev-null-dweller pointed out, you're likely not actually passing in (242, 81). Write a unit test and you'll find the function works as you expect when given valid inputs.
<?php
function differences($takings,$cost)
{
$difference = $takings - $cost;
if($difference < 0) {
$color = 'red';
$difference = '$'.$difference.'million';
} elseif($difference > 0) {
$color = 'green';
$difference = '$'.$difference.='million';
} elseif($difference == 0) {
$color = 'blue';
$difference = "broken even ";
}
return '<span style="color:'.$color.';">'.$difference.'</span>';
}
class DifferencesTest extends PHPUnit_Framework_TestCase
{
public function testValidInput() {
$this->assertEquals('<span style="color:green;">$161million</span>', differences(242,81));
}
public function testInvalidInput() {
$this->assertEquals('<span style="color:green;">$242million</span>', differences(242,NULL));
}
}
I think value are not assigned to cost variable.so check to echo cost variable in function.

Categories