I need to check/uncheck a table checkbox based on a value stored in a database. I'm not an experienced programmer. I've been Google-ing this for a whole day with no luck. Hope someone's going to help me.
Here's the code :
echo "<table data-toggle='table' data-click-to-select='true' id='potable'>";
echo "<thead>";
echo "<tr>";
echo "<th data-field='state' data-checkbox='true'></th>";
echo "<th data-field='code'>Code</th>";
echo "<th data-field='description'>Description</th>";
echo "<th data-field='verb'>Verb</th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>"; ?>
<script>
var select = <?php echo "{$selected}"; ?>;
if (select == 1) {
// INSERT HERE JAVASCRIPT LINE THAT CHECKS THE CURRENT CHECKBOX
else {
//UNCHECK THE CURRENT CHECKBOX
}
</script>
<?php
echo "</td>";
echo "<td>{$code}</td>";
echo "<td>{$description}</td>";
echo "<td>{$verb}</td>";
echo "<a type='button' class='btn btn-primary left-margin' href='po-info.php?id={$id}'>Info</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Finally managed to make it work
Used
echo "<th data-field='selected' data-checkbox='true' data-formatter='stateFormatter'></th>";
then
<script>
function stateFormatter(value, row, index) {
if (value == 1) {
return {
checked: true
};
} else {
return {
checked: false
};
}
return value;
}
</script>
Try
$("th[data-checkbox='true'] input[type=checkbox]").attr('checked', <?=$selected?'true':'false'?>);
<table>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$checked_ids = array();
$checked_ids[] = $id;
$chk_str = '<input data-index="%s" name="chk" type="checkbox">';
echo "<tr>";
echo "<td>";
printf($chk_str, $id);
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="hidden" id="db_checked" value="<?php echo implode(",",$checked_ids); ?>" />
<script>
// need jquery.js
$(function () {
var $table = $('#potable'),
var string = $('#db_checked').val();
var array = string.split(',');
for (var ck_id in array) {
$table.bootstrapTable('check', parseInt(ck_id, 10));
// check or uncheck
}
}
</script>
maybe "data-index" is write at "tr" tags
Related
Does it overwrite the $line array? If yes, will renaming the array fix the problem? Furthermore, how can I call data from specific fields in this array through jquery?
I need to make an INTERACTIVE VISUAL to represent all the data being read from these files. How can I go about doing this?
This is the php code:
<?php
echo "<html><body><table>\n\n";
$f = fopen("LoanStats3a.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$g = fopen("LoanStats3b.csv", "r");
while (($line = fgetcsv($g)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($g);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$h = fopen("LoanStats3c.csv", "r");
while (($line = fgetcsv($h)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($h);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$i = fopen("RejectStatsA.csv", "r");
while (($line = fgetcsv($i)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($i);
echo "\n</table></body></html>";
?>
<?php
echo "<html><body><table>\n\n";
$j = fopen("RejectStatsB.csv", "r");
while (($line = fgetcsv($j)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($j);
echo "\n</table></body></html>";
?>
I'm trying to show div when time (importing from database) - current time will be 0. How to do that? Here's the code:
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{
echo "<script>";
echo "$(document).ready(function(){";
echo "$('#". $row['id'] . "').show();";
echo "$('#". $row['id'] . "').delay(15000);";
echo "$('#". $row['id'] . "').hide();";
echo "});";
echo "</script>";
}
}
I think you want to add a setTimeout() to your javascript code instead of if ($differenceInSeconds==0) {} PHP side.
Also you need to rewrite your jQuery from (effectively showing an element, delaying nothing and hiding it right away)
$("#element").show();
$("#element").delay(15000);
$("#element").hide();
to
$("#element").show().delay(15000).hide(0);
Full code below.
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
// Adding a setTimeout so your JS code actually gets written and executes when it should.
if ($differenceInSeconds >= 0) {
echo "<script>";
echo "$(document).ready(function(){";
echo "window.setTimeout(function() {";
echo "$('#". $row['id'] . "').show().delay(15000).hide(0);";
echo "}, " . $differenceInSeconds * 1000 . ");";
echo "});";
echo "</script>";
}
}
Try it :-
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div class='alert' id='" . $row['id'] . "' style='display:none'><br><br><center><h2>";
echo $row['title'];
echo "<br><br>";
echo $row['category'];
echo "</h2><br><br><hr><br><br></center>";
echo $row['description'];
echo "</div>";
$timeFirst = strtotime($row['date']);
$timeSecond = strtotime("now");
$differenceInSeconds = $timeFirst - $timeSecond;
if ($differenceInSeconds==0)
{ ?>
<script>
$(document).ready(function(){
$('#<?php echo $row['id'] ?>').show();
$('#<?php echo $row['id'] ?>').delay(15000);
$('#<?php echo $row['id'] ?>').hide();
});
</script>
<?php }
}
?>
I'm fairly noob when it comes to php and was just wondering how I can have my table split into pages as it has over 200,000 records in the database and also if it's possible to go to the next/previous page of the table without refreshing the webpage?
Here is my table, any advise is very much appreciated!!!
a<?php
$query = "SELECT * FROM aliases where client_id='$userid' ORDER BY time_edit DESC";
$result = mysql_query($query);
if ($result->num_rows > 0) {
echo "<table border=1>";
echo "<tr align=center>";
echo "<th width=25%>Alias</th>";
echo "<th width=25%>Times Used</th>";
echo "<th width=25%>First Used</th>";
echo "<th width=25%>Last Used</th>";
echo "</tr>";
echo "<tr><td>No Results</td></tr>";
}
echo "<table border=1>";
echo "<tr>";
echo "<th width=25%>Alias</th>";
echo "<th width=25%>Times Used</th>";
echo "<th width=25%>First Used</th>";
echo "<th width=25%>Last Used</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($result)) {
$timesused=$row['num_used'];
$alias=$row['alias'];
$alias=htmlentities($alias);
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
}
echo "</table>";
?>
add a class to your cells:
a<?php
$query = "SELECT * FROM aliases where client_id='$userid' ORDER BY time_edit DESC";
$result = mysql_query($query);
if ($result->num_rows > 0) {
echo "<table border=1>";
echo "<tr align=center>";
echo "<th width=25%>Alias</th>";
echo "<th width=25%>Times Used</th>";
echo "<th width=25%>First Used</th>";
echo "<th width=25%>Last Used</th>";
echo "</tr>";
echo "<tr><td>No Results</td></tr>";
}
echo "<table border=1>";
echo "<tr>";
echo "<th width=25%>Alias</th>";
echo "<th width=25%>Times Used</th>";
echo "<th width=25%>First Used</th>";
echo "<th width=25%>Last Used</th>";
echo "</tr>";
$countRow = 0;
$class = 1;
while ($row = mysql_fetch_assoc($result)) {
$countRow += 1;
$timesused=$row['num_used'];
$alias=$row['alias'];
$alias=htmlentities($alias);
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<tr align=center>";
echo "<td class='visible".$class.">$alias</td>";
echo "<td class='visible".$class.">$timesused</td>";
echo "<td class='visible".$class.">$firstused</td>";
echo "<td class='visible".$class.">$lastused</td>";
echo "</tr>";
if($countRow == 50){ $class += 1; $countRow = 0; }
}
echo "</table>";
?>
and on client side use javaScript, jQuery or what you prefer to hide or show them...
there are plugins that do it for you as commented before...
This is not the best answer but I hope it helps you.
I want to get simple fb graph data and load that into a table with php, but somehow i receive 4 same error messages why so ever and I dont know what they mean.
Notice: Undefined offset: 0 in index.php on line 52
Notice: Undefined offset: 0 in index.php on line 55
Notice: Undefined offset: 0 in index.php on line 59
Notice: Undefined offset: 0 in index.php on line 60
Here are the lines where the error / warnings appear ..
echo "<table class='table table-hover table-responsive table-bordered'>";
//count the number of events
$events_count = count($obj['data']);
for($x=0; $x<$events_count; $x++){
//fb events will be here
}
echo"</table>";
$start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));
// in my case, I had to subtract 9 hours to sync the time set in facebook
$start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);
$pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";
$eid = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
// place
$place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
$city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
$country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
$zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";
$location="";
if($place_name && $city && $country && $zip){
$location="{$place_name}, {$city}, {$country}, {$zip}";
}else{
$location="Location not set or event data is too old.";
}
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src='{$pic_big}' width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>{$name}</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>{$start_date} at {$start_time}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>{$location}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>{$description}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Facebook Link:</td>";
echo "<td>";
echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
echo "</td>";
echo "</tr>";
?>
Thanks for help and fast answer
Add your code inside loop.
Try
<?php
echo "<table class='table table-hover table-responsive table-bordered'>";
//count the number of events
$events_count = count($obj['data']);
for($x=0; $x<$events_count; $x++)
{
//fb events will be here
$start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));
// in my case, I had to subtract 9 hours to sync the time set in facebook
$start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);
$pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";
$eid = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
// place
$place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
$city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
$country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
$zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";
$location="";
if($place_name && $city && $country && $zip){
$location="{$place_name}, {$city}, {$country}, {$zip}";
}else{
$location="Location not set or event data is too old.";
}
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src='{$pic_big}' width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>{$name}</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>{$start_date} at {$start_time}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>{$location}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>{$description}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Facebook Link:</td>";
echo "<td>";
echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
echo "</td>";
echo "</tr>";
}
echo"</table>";
?>
You should close for correctly.
You can't use $x var out of for.
Your for loop is:
for($x=0; $x<$events_counts; $x++) {
// fb events will be here
}
// other code below
// $x is not available here!
So, basically you have } and you're trying to access $x which is declarated (unless it's global) in scope of for, but it doesn't exists below closing bracket. Put your code in for loop and it should works.
I guys, I need yo help for this. I have this code, wich shows me the products I have in my shopping cart:
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}
I need to sum 1 unity or rest 1 unity to $_SESSION['unidades'][$i] just to the selected product, when I click the two images respectively.
The thing is that when I click, it adds me 1 to all the products. Any easy way of doing this without using $_GET vars? I dont have high knowledge about JavasScript.
Thanks!!
You probably should remove your condition from the loop and put it outside of it:
if($_GET[action]=="suma")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] + 1;
}
elseif($_GET[action]=="resta")
{
$_SESSION['unidades'][$i] = $_SESSION['unidades'][$i] - 1;
}
for($i=0;$i<count($_SESSION['productos']);$i++)
{
$id = $_SESSION['productos'][$i];
$prods = mysql_query('SELECT * from productos where idprod='.$id.'');
$row = mysql_fetch_array($prods);
echo "<tr>";
echo "<td>" . $row['nombre']; echo "</td>";
echo "<td><input name=".$i." type='text' value=" . $_SESSION['unidades'][$i]; echo " size='5'/></td>";
echo "<td><a href='carro_detalle.php?action=suma'><img src='images/flecharriba.png' width='10x' height='10px'/></a></td>";
echo "<td><a href='carro_detalle.php?action=resta'><img src='images/flechabajo.png' width='10px' height='10px'/></a></td>";
echo "<td>" . $row['precio']; echo "</td>";
echo "<td>" . $row['precio'] * $_SESSION['unidades'][$i]; echo "</td>";
echo "</tr>";
}