table with an url image inside header - html

I have a sortable table in html and looking to put an add.png inside each table header so if a user wants to add another url - they just click the add.png and redirects them to the addurl.php (if they click outside it will still sort the table just like normal). Right now it sorting when I click on the .png and/or outside it. If this something that can't be done, I've also thought about trying to add a final row with [add another site] but have no idea how to work it into the $j++ so it appears at the final row.
what it looks like
[PHP snippet]
<table class="datatable sortable selectable full">
<thead>
<tr class="theader">
<th width="100%">
<b><li><img src="images/logos/googlebuzz-2-icon.png" height="18" border="0" />Google <img src="img/icons/add.png" height="18" border="0" align="right"/></li></b>
</th>
<th>
<b>Coins</b>
</th>
<th>
<b>CPC</b>
</th>
</tr>
</thead>
<tbody>
<?
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
for($j=1; $site = mysql_fetch_object($site2); $j++)
{
?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<?}?>
</tbody>
</table>

First of all, instead of a for loop, you should use while:
while($site = mysql_fetch_object($site2))
If you want to add a final row, just add it outside the iteration, before </tbody>:
<tbody>
<? while($site = mysql_fetch_object($site2)): ?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<? endwhile; ?>
<tr>
<td colspan="3">
[add another site]
</td>
</tr>
</tbody>
It seems you are using a javascript to do the sorting. I don't know if that can be changed so your add button in the header works.

You can check if it's the final row by checking the actual row against all rows:
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
$rows = mysql_num_rows($site2);
$i = 0;
And add this in your for loop:
$i++;
if($rows == $i) {
// do something only at the final row
}

Related

AppScript - create dynamic HTML table from AppScript variable

I´m trying to create an HTML table which get´s data from an array that is being passed from request.gs to template.html.
My current setup is:
request.gs (partial snippet):
var products = tempSheet.getRange("B2:B").getValues();
var template = HtmlService.createTemplateFromFile('template');
template.products = products;
var emailToSend = template.evaluate().getContent();
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'test mail ' + Utilities.formatDate(new Date(), "GMT+2", "dd.MM.yyyy"),
htmlBody: emailToSend
});
template.html (partial snippet):
<table>
<tr>
<th>Product Name</th>
</tr>
<tr>
<td>placeholder</td>
</tr>
</table>
I do have access to the products variable within the template.html using <?= products ?>.
Where my current knowledge and tries ended so far is how to add table rows and the data of products in each of the rows.
Here is how to add rows dynamically to the template. However I suggest you filter out blank rows first.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<table>
<tr>
<th>Product Name</th>
</tr>
<? for (var i = 0; i < products.length; i++) { ?>
<tr>
<td><?= products[i][0] ?></td>
</tr>
<? } ?>
</table>
</body>
</html>
Reference
Templated HTML
It will be something like:
<table>
<tr>
<th>Product Name</th>
</tr>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<td><?= products[i] ?></td>
</tr>
<? } ?>
</table>

How to show data in html table using PHP

This is my code the table appears up with the header and everything but the row with the td tag data shows nothing at all
echo "<p></p>";
//display name of the page and some random text
echo "<h2>".$pagename."</h2>";
if($_POST['h_prodid'] > 0){
$newprodid = $_POST['h_prodid'];
$reququantity =$_POST['quantity'];
$_SESSION['basket'][$newprodid]=$reququantity;
echo "<p>Your basket has been updated</p>";
} else {
echo "<p>Existing basket</p>";
}
echo "<table border = '1'>
<tr><th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>";
echo "<td>".$reququantity."</td>";
echo "</table>";
The reason your data is NOT displaying in the html is because it is NOT in a tr ( table row ) - simply a floating table cell
if( $_SERVER['REQUEST_METHOD']=='POST' ){
echo "<h2>".$pagename."</h2>";
if( isset( $_POST['h_prodid'],$_POST['quantity'] ) ){
if( $_POST['h_prodid'] > 0 ){
$newprodid = $_POST['h_prodid'];
$reququantity =$_POST['quantity'];
$_SESSION['basket'][$newprodid]=$reququantity;
echo "<p>Your basket has been updated</p>";
}else{
echo "<p>Existing basket</p>";
}
echo "
<table border = '1'>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td>{$reququantity}</td>
<td> </td>
</tr>
</table>";
}
}

css color condition in html based on database

I used here bootstrap. I want to red mark those row which Activity contains "N" .In my database Activity field has two characters. they are Y & N. I also used here jquery data table.What can i do?
<?php
while($reg_data = mysqli_fetch_array($result_sql2))
{
?>
<tr class="success">
<td > <?php echo $reg_data['ID'] ?> </td>
<td> <?php echo $reg_data['Name'] ?> </td>
<td> <?php echo $reg_data['Email'] ?> </td>
<td> <?php echo $reg_data['Type'] ?> </td>
<td> <?php echo $reg_data['Dept'] ?> </td>
<td> <?php echo $reg_data['Activity'] ?> </td> // based on this field //////
</tr>
One best quick and easy way to do is to give an inline style:
<td style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>;">
<?php echo $reg_data['Activity'] ?>
</td>
You might want to add !important just in case if needs an overriding:
<td style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?> !important;">
Note: I know inline styles are bad.
Update: Sorry I missed that you need to do it on the row. You can just apply the same thing on the <tr> instead of that <td>. So your code will be:
<tr class="success" style="background-color: <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>;">
<td > <?php echo $reg_data['ID'] ?> </td>
<td> <?php echo $reg_data['Name'] ?> </td>
<td> <?php echo $reg_data['Email'] ?> </td>
<td> <?php echo $reg_data['Type'] ?> </td>
<td> <?php echo $reg_data['Dept'] ?> </td>
<td> <?php echo $reg_data['Activity'] ?> </td>
</tr>
Thanks to Darren Sweeney for pointing it out.
Check for window.onload.
Reference : https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload
Set a class based on activity on the desired <td> then upon window.onload use jQuery .parent() function and add css to the parent of <td> which containes "Y" as a class.
window.onload = function() {
$('.YClassName').parent().css('background', 'YOURCOLOR');
};
Another approach would be creating a CSS class and adding it to the element if your condition is met.
.red {
background-color: red;
}
.green {
background-color: green;
}
And then
<tr class="success <?php echo ($reg_data['Activity'] == "Y") ? "green": "red" ?>">
//table data
</tr>
As a sidenote, it'd be better to use "1" and "0" instead of "Y" and "N". Integer table-row types tend to perform better? At least, that's what I've believed until now.

Multiple Headers Occur

This is supposed to be a simple high scores table. but everytime it pulls another player from the database it looks like this.
(table headers)
player
(table headers)
player
(table headers)
player
and so on you get the picture... I want it to just show the header one time at the top of the list of players... I had it working at one time and don't remember what I changed that caused it to do this, does anyone know how to fix it?
Here is my code:
<?php
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
The problem with your code was the table is getting created in the loop, leading to the multiple headers. Instead remove the creation of the table from the while loop and then try to update all the result set you get from the query you fired in a loop. Try this.
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<?php
while($row = mysql_fetch_array( $result )) {
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

PHP Array doesn't show all the data from database

I have a very basic and simple script that should display records from my database. The problem: it doesn't show all the records. I've tried it even with the most simple mysql
($sql="SELECT * FROM $tbl_name";) but still some records are missing (mostly the first of the list that isn't shown).
So here is my code (it's all on 1 page):
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$tbl_name="***";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE rowNameOne >= 0.01 AND rowNameTwo='2013'";
if ($_GET['sort'] == 'one')
{
$sql .= " ORDER BY one ASC";
}
elseif ($_GET['sort'] == 'two')
{
$sql .= " ORDER BY two ASC";
}
elseif ($_GET['sort'] == 'three')
{
$sql .= " ORDER BY three ASC";
}
elseif($_GET['sort'] == 'four')
{
$sql .= " ORDER BY four ASC";
}
elseif($_GET['sort'] == 'five')
{
$sql .= " ORDER BY five ASC";
}
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body onload="parent.alertsize(document.body.scrollHeight);">
<br />
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" colspan="5">
<font>Titel</font>
</td>
<tr>
<td>Titel one</td>
<td>Titel two</td>
<td>Titel three</td>
<td>Titel four</td>
<td>Titel five</td>
</tr>
<tr>
<td colspan="5" class="noBorder">
<?php
while($rows=mysql_fetch_array($result)){
?>
<a href="pageName.php?id=<? echo $rows['id']; ?>" >
<table width="100%">
<tr>
<td><? echo $rows['rowNameOne']; ?></td>
<td><? echo $rows['rowNameTwo']; ?></td>
<td><? echo $rows['rowNameThree']; ?></td>
<td><? echo $rows['rowNameFour']; ?></td>
<td><? echo $rows['rowNameFive']; ?></td>
</tr>
</table>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
<?php
}
?>
</a>
</td>
</tr>
</table>
It's a very basic code, easy as can be I would say, but still it's missing records, not displaying everything that's in the database. What am I doing wrong?
Thanks for the help!
Before you start the loop, you do this:
$rows=mysql_fetch_array($result);
Then the loop condition is:
while($rows=mysql_fetch_array($result)){
So the first result is never shown. I would advice to remove the first statement, since you're not using its results between that statement and the loop.
On a related note, please consider moving to PDO or mysqli.