I need help with a simple task, but I am stuck...
I have a database where I pull different data and I am visualizing those data on a web page.
Here is my query for getting the data:
$result = mysqli_query($mysqli, "SELECT *, DATEDIFF(nextcalibration, CURDATE()) AS days FROM tools AS dp ");
Here is my table:
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td>".$user_data['days']. "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
I would like to make conditional formatting only for the last column - "days".
The idea is if the value of "days" <= 30 to become red color text.
I tried with various JS, but honestly, it did not work.
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td>";
$days=$user_data['days'];
if($days <= 30){
echo "<span style='color:red;'>$days</span>";
}else{
echo $days;
}
echo "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
Would something simple like this - without JS- work?
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
$style_days=($user_data['days']<=30)?'style="color:red"':'';
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td ".$style_days.">".$user_data['days']. "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
Of course, apply the necessary guarding for $user_data['days'] (existence, numeric..) :)
Related
Hello the id that im getting is "$id" instead of the int id what i would like to happen is that basically it will go to the full size of the image in the "showImage.php"
<?php
while($row = mysqli_fetch_array($result)) {
$id = $row[0];
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[7]."</td>";
echo "<td>".$row[8]."</td>";
echo "<td>".$row[9]."</td>";
echo "<td>".$row[10]."</td>";
echo '<td>
<img id="$id" src="data:image/jpeg;base64,'.base64_encode($row[11] ).'" height="200" width="200" />
</td> ';
?>
in case you're wondering this is the html
<tr>
<th>ID</th>
<th>Voter's ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Confirmation Code</th>
<th>UserType </th>
<th>Age</th>
<th>Gender</th>
<th>Birthday</th>
<th>Action</th>
<th>Image</th>
<tr>
Below I'll post my code and I am going to explain my problem: The fact is that this code should show the value of every record of the query that I established, the problem is that the query is right (cause I checked it on phpMyAdmin) but the page don't show me the <tbody>, it show me nothing, I tried to put a single echo output with the <td> tag but nothing, can someone solve my problem and see the error that I have done?
<div class="container-fluid col-lg-6">
<table class="table">
<thead class="thead-default">
<tr>
<th>ID Pratica</th>
<th>ID Utente</th>
<th>Data Inizio</th>
<th>Descrizione</th>
<th>Data Stimata</th>
<th>Stato</th>
</tr>
</thead>
<tbody>
<?php
$query_search_all="SELECT * FROM `pratiche`";
$result = mysql_query($query_search_all);
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<td>".$row['id_pratica']."</td>";
echo "<td>".$row['id_utente']."</td>";
echo "<td>".$row['data_inizio']."</td>";
echo "<td>".$row['descrzione']."</td>";
echo "<td>".$row['data_stimata']."</td>";
echo "<td>".$row['stato']."</td>";
}
?>
</tbody>
</table>
</div>
You forgot the <tr> tag.
<div class="container-fluid col-lg-6">
<table class="table">
<thead class="thead-default">
<tr>
<th>ID Pratica</th>
<th>ID Utente</th>
<th>Data Inizio</th>
<th>Descrizione</th>
<th>Data Stimata</th>
<th>Stato</th>
</tr>
</thead>
<tbody>
<?php
$query_search_all="SELECT * FROM `pratiche`";
$result = mysql_query($query_search_all);
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$row['id_pratica']."</td>";
echo "<td>".$row['id_utente']."</td>";
echo "<td>".$row['data_inizio']."</td>";
echo "<td>".$row['descrzione']."</td>";
echo "<td>".$row['data_stimata']."</td>";
echo "<td>".$row['stato']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
Can you please check the following code???
<?php
$query_search_all="SELECT * FROM pratiche ";
$result = mysql_query($query_search_all);
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$row['id_pratica']."</td>";
echo "<td>".$row['id_utente']."</td>";
echo "<td>".$row['data_inizio']."</td>";
echo "<td>".$row['descrzione']."</td>";
echo "<td>".$row['data_stimata']."</td>";
echo "<td>".$row['stato']."</td>";
echo "</tr>";
}
?>
What I am trying to do is get it so there is a different hyper link address for each row echoed. Code is below:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Product Name</th> <th>Product Description</th> <th>Product Price</th> <th>Product Image</th> <th>View Product Details</th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['Product_Name'] . '</td>';
echo '<td>' . $row['Product_Description'] . '</td>';
echo '<td>' . $row['Product_Price'] . '</td>';
echo '<td>Picture Enlarge</td>';
echo '<td>View Details</td>';
echo "</tr>";
}
echo "</table>";
You can put php variables inline in your echo'ed anchors just as you are doing with the other variables.
Assuming that you're using the id field in your database you can do this:
echo '<td>View Details</td>';
If you echo a php variable (e.g. $row['id']) then it will echo out to HTML (not necessarily text). So as this one is contained in an anchor tag (in the HTML) it echos to the anchor tag and builds the id part. :)
<table border='1' cellpadding='10'>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
<th>Product Image</th>
<th>View Product Details</th>
</tr>
while($row = mysql_fetch_array( $result )) {
<tr>
<td> <?php echo $row['Product_Name']; ?></td>
<td><?php echo $row['Product_Description']; ?></td>
<td><?php $row['Product_Price']; ?></td>
<td>Picture Enlarge</td>
<td>View Details</td>
</tr>
}
</table>
I would probably go for something like that. This assumes that your table returns a unique id, from something like an auto incremented column.
This doesn't include any escaping so could be vulnerable to exploitation. I would also look into templating. It may help you with your presentation, makes it a little easier to read the mixed html and php.
"<td align=\"center\" valign=\"top\"><<") . "</td>\n"
I have a table in my DB which I call to show everything in that table on a page one of the items in the table is an image which when moused over needs to show a popup showing data from a separate table which corresponds to that item. I have a JQuery which calls the data depending on the row from the 1st table but when I try to actually put the two together I get the first table showing fine but now I get no pop up on mouseover.
This page is currently included in the index.php page which has the scripts to call the .js / .css files.
Here's the code for what I am trying to do:
<html>
<table border='0' cellpadding='0' cellspacing='0' class="center2">
<tr>
<td width='60'><img src="images/box_tl.png"></td>
<td style="background: url(images/box_tm.png)" align="center"><img src="images/news.png"></td>
<td width='25'><img src="images/box_tr.png"></td>
</tr>
<tr>
<td style="background: url(images/box_ml.png)"><h2>.</h2></td>
<td style="background: url(images/box_mm.png)">
<?php
include 'connect.php';
$query = mysql_query("SELECT * FROM tbl_img") or die(mysql_error());;
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Mob Name</th> <th>Id</th> <th>Health</th> <th>Body</th> <th>Effects</th> <th>Spawn</th></tr></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query )) {
$mob_id = $row['mob_id'];
$mob = $row['mob'];
$body = $row['body'];
$mob_name = $row['mob_name'];
$health = $row['health'];
$level = $row['level'];
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src='/testarea/include/mobs/$mob' />";
echo "</td><td>";
echo $mob_name;
echo "</td><td>";
echo $level;
echo "</td><td>";
echo $health;
echo "</td><td>";
echo
"
<a onmouseover='popup($('#hidden-table').html(), 400);' href='somewhere.html'><img src='/testarea/include/mobs/dead/$body' /></a>
";
echo "
<div id='hidden-table' style='display:none;'>
<table border='0' cellpadding='0' cellspacing='0' class='center3'>
<tr>
<td width='14'><img src='images/info_tl.png'></td>
<td style='background: url(images/info_tm.png)' align='center'></td>
<td width='14'><img src='images/info_tr.png'></td>
</tr>
<tr>
<td style='background: url(images/info_ml.png)'><h2>.</h2></td>
<td style='background: url(images/info_mm.png)'>
";
$query2 = mysql_query("SELECT * FROM tbl_drop WHERE mob_name='$mob_name'") or die(mysql_error());;
echo "<table border='0' cellpadding='1' cellspacing='1' width='250' id='2' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Item Name</th> <th>Qty</th></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query2 )) {
$id = $row['id'];
$item_img = $row['item_img'];
$qty = $row['qty'];
$item_name = $row['item_name'];
// Print out the contents of each row into a table
echo "<tr><td width='50'>";
echo "<img src='/testarea/item/$item_img' />";
echo "</td><td width='150'>";
echo $item_name;
echo "</td><td width='50'>";
echo $qty;
echo "</td></tr>";
}
echo "</tbody></table>";
echo "
</td>
<td style='background: url(images/info_mr.png)'><h2>.</h2></td>
</tr>
<tr>
<td width='14'><img src='images/info_bl.png'></td>
<td style='background: url(images/info_bm.png)' align='center'><h2>.</h2></td>
<td width='14'><img src='images/info_br.png'></td>
</tr>
</table>
</div>"
;
echo "</td><td>";
echo "test";
echo "</td><td>";
echo "test";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
</td>
<td style="background: url(images/box_mr.png)"><h2>.</h2></td>
</tr>
<tr>
<td width='60'><img src="images/box_bl.png"></td>
<td style="background: url(images/box_bm.png)" align="center"><h2>.</h2></td>
<td width='25'><img src="images/box_br.png"></td>
</tr>
</table>
</html>
you have two semi-colons on this line...could be the problem
$query = mysql_query("SELECT * FROM tbl_drop WHERE mob_name='$mob_name'") or die(mysql_error());;
everything works perfect bar the div 'hidden-table' not showing, if i remove the 'style:display:none' then it shows the table with correct data so i know its working and i have tried the taking the popup out of the echo and displaying it separately just to see if it shows which it does.
seems the problem occurs only when its in the echo and seems centred around the 'onmousover'
full page code:
<html>
<table border='0' cellpadding='0' cellspacing='0' class="center2">
<tr>
<td width='60'><img src="images/box_tl.png"></td>
<td style="background: url(images/box_tm.png)" align="center"><img src="images/news.png"></td>
<td width='25'><img src="images/box_tr.png"></td>
</tr>
<tr>
<td style="background: url(images/box_ml.png)"><h2>.</h2></td>
<td style="background: url(images/box_mm.png)">
<?php
include 'connect.php';
$query = mysql_query("SELECT * FROM tbl_img") or die(mysql_error());;
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Mob Name</th> <th>Id</th> <th>Health</th> <th>Body</th> <th>Effects</th> <th>Spawn</th></tr></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query )) {
$mob_id = $row['mob_id'];
$mob = $row['mob'];
$body = $row['body'];
$mob_name = $row['mob_name'];
$health = $row['health'];
$level = $row['level'];
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src='/testarea/include/mobs/$mob' />";
echo "</td><td>";
echo $mob_name;
echo "</td><td>";
echo $level;
echo "</td><td>";
echo $health;
echo "</td><td>";
echo
"
<a onmouseover='popup($('#hidden-table').html(), 400);' href=''><img src='/testarea/include/mobs/dead/$body' /></a>
";
echo "
<div id='hidden-table' style='display:none;'>
<table border='0' cellpadding='0' cellspacing='0' class='center3'>
<tr>
<td width='14'><img src='images/info_tl.png'></td>
<td style='background: url(images/info_tm.png)' align='center'></td>
<td width='14'><img src='images/info_tr.png'></td>
</tr>
<tr>
<td style='background: url(images/info_ml.png)'><h2>.</h2></td>
<td style='background: url(images/info_mm.png)'>
";
$query2 = mysql_query("SELECT * FROM tbl_drop WHERE mob_name='$mob_name'") or die(mysql_error());
echo "<table border='0' cellpadding='1' cellspacing='1' width='250' id='2' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Item Name</th> <th>Qty</th></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query2 )) {
$id = $row['id'];
$item_img = $row['item_img'];
$qty = $row['qty'];
$item_name = $row['item_name'];
// Print out the contents of each row into a table
echo "<tr><td width='50'>";
echo "<img src='/testarea/item/$item_img' />";
echo "</td><td width='150'>";
echo $item_name;
echo "</td><td width='50'>";
echo $qty;
echo "</td></tr>";
}
echo "</tbody></table>";
echo "
</td>
<td style='background: url(images/info_mr.png)'><h2>.</h2></td>
</tr>
<tr>
<td width='14'><img src='images/info_bl.png'></td>
<td style='background: url(images/info_bm.png)' align='center'><h2>.</h2></td>
<td width='14'><img src='images/info_br.png'></td>
</tr>
</table>
</div>"
;
echo "</td><td>";
echo "test";
echo "</td><td>";
echo "test";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
</td>
<td style="background: url(images/box_mr.png)"><h2>.</h2></td>
</tr>
<tr>
<td width='60'><img src="images/box_bl.png"></td>
<td style="background: url(images/box_bm.png)" align="center"><h2>.</h2></td>
<td width='25'><img src="images/box_br.png"></td>
</tr>
</table>
</html>
You're inadvertently ending your onmouseover event when you try to reference your jQuery selector. The use of single quotes for different reasons is the reason. Try this instead:
function ShowPop()
{
popup($('#hidden-table').html(), 400);
}
<a onmouseover='ShowPop()' href=''><img ..... /></a>