Unexpected output in php function - php

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.

Related

PHP SQLSRV Pagination function returns one row

My pagination isn't working correctly. I get the page numbers but each page only has one row when it should have 10 rows. I new to php and am adapting code that I found on this site. When I load the page I can tell by the id number that the result showing is what I would expect the 10th row in the query to be...rows 1-9 are missing.
<?php
//Function to return rows for each page
function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE,$offset + $i)) && $i < $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
return $rows;
}
?>
<?php
// Set the number of rows to be returned on a page.
$rowsPerPage = 10;
$usr = $_SESSION['user'];
if ($_SESSION['admin']="YES") {
$query = "SELECT iID,FirstName,LastName, convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where Status='Pending' ORDER BY iID desc";
}else {
$query = "SELECT iID,FirstName,LastName,convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where SubmitBy='".$usr."' ORDER BY iID desc";
}
$stmt = sqlsrv_query($connect,$query, array(), array( "Scrollable" => 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));
?>
<table class="table table-striped table-bordered">
<thead class="thead-light">
<tr>
<th width="54%" scope="col">Submitted By</th>
<th width="14%" scope="col">Date Submitted</th>
<th width="13%" scope="col">Status</th>
<th width="19%" scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);
foreach($page as $row)
$ideanum = $row[0];
$fname = $row[1];
$lname = $row[2];
$submitdate = $row[3];
$idea = $row[4];
$status = $row[5];
echo '<tr>';
echo '<td>'.$ideanum.' '.$fname.' '.$lname.' - '.$idea.'...</td>';
echo '<td>'.$submitdate.'</td>';
echo '<td>'.$status.'</td>';
echo '<td><button type="button" class="btn btn-success"><i class="la la-eye"></i></button><button type="button" class="btn btn-info"><i class="la la-edit"></i></button></td>';
echo "</tr>";
?>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
echo "<br/><br/>";
}
?>
</tbody>
</table>
SOLVED: The issue was that I was missing the brackets in my foreach statement as a redditor pointed out to me.

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>

Random selection of dynamic check boxes

I am working on building a small php file. I need to select random check boxes. check boxes dynamic as they are pulled from a table with some conditions.
Please let me know if i can randomly select few check boxes say if i get 100 results from the mysql table, 10 random check box needs to be selected.
<?php
$n1=0;
$rambo = strip_tags(#$_POST["assoocc"]);
$r11 = strip_tags(#$_POST["ccdate"]);
$r12 = strip_tags(#$_POST["rrdate"]);
$submit = #$_POST["submit22"];
?>
<body><div>
<form action="selectedcc.php" method="post">
<center> <table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th colspan="3"> Total cases for QA</th>
<th colspan="2"><?php
$ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");
$m1=mysql_num_rows($ticket);
echo "$m1";
?></th>
<th colspan="2"><select name="assoocc1" class="form-control" id="sel1" style="width: 150px;" >
<?php echo "<option value=$rambo selected>".$rambo."</option>";
?>
</select></th>
<th colspan="1"><input type="submit" name="sub" value="Submit For QA "/></th>
</tr>
<tr>
<th>Select Here</th>
<th>Case Id</th>
<th>Task</th>
<th>Number of ASINs</th>
<th>SLA Details</th>
<th>Create date</th>
<th>Resolved date</th>
<th>Rootcause</th></tr>
<?php
if ($submit)
{
if ($rambo&&$r11&&$r12)
{
if ($r11<$r12)
{
$ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");
$m1=mysql_num_rows($ticket);
for($k=0; $k<$m1; $k++)
{
$caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
${"caseid".$k}=mysql_result($ticket,$k, "case_id");
${"task".$k}=mysql_result($ticket,$k, "sub_issue");
${"asin".$k}=mysql_result($ticket,$k, "asin");
${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
${"create_date".$k}=mysql_result($ticket,$k, "create_date");
${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
${"associate".$k}=mysql_result($ticket,$k, "associate");
echo "<tr>"."<th>"."<input type='checkbox' value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>";
}
}
else
{
echo "<script>alert('Error:START DATE should be less than END DATE. Go back to QA Home page and enter all fields')</script>";
}
}
else
{
echo "<script>alert('Error:Go back to QA Home page and enter all fields')</script>";
}
}
?>
</table></center></form></div></body></html>
Took the random function UniqueRandomNumbersWithinRange from Generating UNIQUE Random Numbers within a range - PHP
<?php
function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$arr = array_slice($numbers, 0, $quantity);
sort($arr);
return $arr;
}
$chkArr = array();
$rand = UniqueRandomNumbersWithinRange(0, 100, 10);
$rIdx = 0;
for($k=0; $k<100; $k++){
$caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
${"caseid".$k}=mysql_result($ticket,$k, "case_id");
${"task".$k}=mysql_result($ticket,$k, "sub_issue");
${"asin".$k}=mysql_result($ticket,$k, "asin");
${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
${"create_date".$k}=mysql_result($ticket,$k, "create_date");
${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
${"associate".$k}=mysql_result($ticket,$k, "associate");
$chk = '';
if( in_array($k, $rand) )
$chk = "checked";
array_push($chkArr, "<tr>"."<th>"."<input type='checkbox' ".$chk ." value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>");
}
echo implode($chkArr);
?>

nested while loops php mysql

I have this code:
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3" >
<tr>
<td align="center"><strong><font size="2">Students</font></strong></td>
<td align="center"><strong><font size="2">Date</font></strong></td>
<td align="center"><strong><font size="2">Attendance</font></strong> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
$date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?>
<tr>
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font> </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong> </td>
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>
<?php
}
?>
which gives the below output.
Now my question is how to modify my code (use nested loops?!) so the output is:
Thank you kindly.
NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.
It may not be the best solution, but I cannot think of something better right now.
In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.
<?php
if( isset($_POST['groups'])){
$groups = $_POST['groups'];
$subject = $_POST['subject'];
$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno
FROM table_1 a, table_2 b
WHERE b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql);
$dates = array();
$display = array();
while ($rows=mysqli_fetch_array($result)) {
if (!isset($display[$rows['groupstudents']])) {
$display[$rows['groupstudents']] = array();
}
if (!isset($dates[strtotime($rows['date'])])) {
$dates[strtotime($rows['date'])] = count($dates);
}
$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
$display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}
echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';
foreach ($display as $student => $row) {
echo '<tr>';
echo '<td align="center"><font size="2">' . $student . '</font></td>';
foreach ($dates as $date => $index) {
echo '<td align="center"><font size="2">';
if (isset($row[$index])) {
echo $row[$index];
} else {
echo '';
}
echo '</font></td>';
}
echo '</tr>';
}
echo '</table>';
?>

how to give style to the SQL table when checkbox selected?

SO i have form which consist of "Event Name" "Event Description" "Event Date" and checkbox "is important". When i check checkbox value "yes" its important, it sends to the sql value = "1" to table "is_important". Everything is all right, but i give the bootstrap style "bg-danger" for that "is_important" = 1 table and it doesnt show up. What's the problem?
You can see in the code:
<?php
if (isset($_POST['important'])) {
$error = array();
$success = array();
$eventTime = time();
$important = $_POST['important'];
$eventName = trim(mysql_real_escape_string($_POST['EventName']));
$eventDesc = htmlentities(trim(mysql_real_escape_string($_POST['EventDesc'])), ENT_QUOTES);
if (!isset($eventName) || empty($eventName)) {
$error['eventName'] = "Prasome ivesti ivykio varda";
} else if (strlen($eventName) > 32 || strlen($eventName) < 3) {
$error['eventName'] = "Ivykio pavadinimas turi buti tarp 3 ir 32 simboliu";
}
if (!isset($eventDesc) || empty($eventDesc)) {
$error['eventDesc'] = "Prasome ivesti ivykio aprasyma";
}
if (empty($error)) {
$sql = "INSERT INTO notes_list (title, description, timestamp,is_important) VALUES ('$eventName', '$eventDesc','$eventTime','$important')";
$result = mysqli_query($con, $sql);
$success[] = "SEKME !";
} else {
}
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Event name</th>
<th>Event description</th>
<th>Event date</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM notes_list ORDER BY id DESC LIMIT 10";
$result2 = mysqli_query($con, $query);
print_r($_POST);
if ($result2) {
while ($note = mysqli_fetch_assoc($result2)) {
?>
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
<td><?php echo $note['title']; ?></td>
<td><?php echo $note['description'] ?></td>
<td><?php echo date('l M jS', $note['timestamp']); ?></td>
</tr>
<?php
}
mysqli_free_result($result2);
}
/* close connection */
mysqli_close($con);
?>
</tbody>
</table>
Full Example in this picture:
https://www.dropbox.com/s/h650h2spy2487dm/chechbox.jpg?dl=0
This:
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
would render this:
<trclass='bg-danger'>
in case is_important is 1. You need a space there, before the class.

Categories