Show div when variable (time) is 0 - javascript

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 }
}
?>

Related

Does repeated use of the fgetcsv function in this case overwrite the "$line" array?

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>";
?>

Undefined offset: 0

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.

Detect difference of form submit via submit button or javascript submit function in PHP

I am using a date picker in PHP, but I experience a small problem.
When selecting a month, I automatically submit the form to change the amount of days:
<script>
function change()
{
document.getElementById("datepickerform").submit();
}
</script>
But, when I press submit, I also want to execute some extra code (querying a database for some info with the selected date.
Is there a way to make a difference when the OK button is pressed or when the form is "submitted" via the function above?
Hereunder is the entire code (not finished, leap year also not included yet) of my datepicker.php:
<script>
function change(){
document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// http://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page
// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);
// Get result from FORM POST
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
$currentday = $submittedday;
$currentmonth = $submittedmonth;
$currentyear = $submittedyear;
}
else
{
$currentday = intval(date("d"));
$currentmonth = intval(date("m"));
$currentyear = intval(date("Y"));
}
//DEBUG ON
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
// DEBUG OFF
echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentday)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentmonth)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentyear)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>
Could anybody help?
Most JS libraries that preform AJAX calls pass an extra header with the request, typically X_REQUESTED_WITH with a value of XMLHttpRequest. If you were using something like jQuery you can check for this using PHP, or you could just use a hidden field that your JS creates and fills in when it is submitted that way.
function change()
{
var form = document.getElementById('datepickerform'),
el = document.createElement('input');
el.type = 'hidden';
el.name = 'js';
el.value = '1';
form.appendChild(el);
form.submit();
}
And then when handling in PHP, use
if (isset($_POST['js'])) {
// ...
}
You can Keep a hidden field in form and if the form is beig submitted using js then change the value of hidden field before submitting the form.
You could change it so that there is an Ajax call when the month changes, and use a button that can be used to invoke processing prior to submit.
Thanks for the above tips, this solved my problem. Also Change value of input then submit form in javascript helped solving my problem.
<script>
function change(){
document.getElementById("datepickerform").submitform.value = '1';
document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// https://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page
// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);
// Get result from FORM POST
$submitform = $_POST["submitform"];
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
$currentday = $submittedday;
$currentmonth = $submittedmonth;
$currentyear = $submittedyear;
}
else
{
$currentday = intval(date("d"));
$currentmonth = intval(date("m"));
$currentyear = intval(date("Y"));
}
//DEBUG ON
echo $submitform."<br>\r\n";
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
if ($submitform == '0')
{
echo 'Button pressed.<br>';
}
else
{
echo 'Javascript executed.<br>';
}
// DEBUG OFF
echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentday)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentmonth)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
echo '<option value="' . $i . '"';
if ($i == $currentyear)
{
echo ' selected';
}
echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="hidden" name="submitform" value="0">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>

Check bootstrap table row - Javascript

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

Pop up on button click

I am trying to make it so when a button is clicked it creates a pop up however I have this error "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\project\calendar_start.php on line 55"
Code:
<?php
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$showmonth= preg_replace('#[^0-9]#i', '', $showmonth);
$showyear= preg_replace('#[^0-9]#i', '', $showyear);
$day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear);
$pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6-(date('w', mktime(0,0,0, $showmonth, $day_count, $showyear))));
echo '<div id="calendar_wrap">';
echo '<div class="title_bar">';
echo '<div class="previous_month"><input name="button" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>';
echo '<div class="show_month">' . date('F', mktime(0, 0, 0, $showmonth)) . ' ' . $showyear . '</div>';
echo '<div class="next_month"><input name="button" type="submit" value="Next Month" onClick="javascript:next_month();"></div>';
echo '</div>';
echo '<div class="week_days">';
echo '<div class="days_of_the_week">Sun</div>';
echo '<div class="days_of_the_week">Mon</div>';
echo '<div class="days_of_the_week">Tues</div>';
echo '<div class="days_of_the_week">Wed</div>';
echo '<div class="days_of_the_week">Thur</div>';
echo '<div class="days_of_the_week">Fri</div>';
echo '<div class="days_of_the_week">Sat</div>';
echo '<div class="clear"></div>';
echo '</div>';
if ($pre_days != 0) {
for($i=1; $i<=$pre_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
$con=mysql_connect("localhost","root","");
mysql_select_db("familease", $con);
for ($i=1; $i<= $day_count; $i++) {
$date = $i.'/'.$showmonth.'/'.$showyear;
//echo $date;
$query = "Select id FROM events WHERE evDate = '$date'";
$num_rows = 0;
// check if the query returns anything
$result = mysql_query($query,$con) or die(mysql_error());
if ($result)
{
$num_rows = mysql_num_rows($result);
}
if($num_rows > 0) {
<input type="submit" value="Details"
onClick="window.open('events.php', 'View',width=300,height=200,menubar=yes,status=yes)"">
}
echo '<div class="cal_day">';
echo '<div class="day_heading">' . $i . '</div>';
if($num_rows > 0)
{
echo "<div class='openings'><br/>" . $event . "</div>";
}
echo '</div>';
} //end of for loop
if ($post_days !=0) {
for($i=1; $i<=$post_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
echo '</div>';
?>
With no more context all I can see is that you use only simple quotes (') inside your onclick statement. Use double quotes or escape simple quotes inside this statement such as :
onClick="window.open('events.php', 'View',width=300,height=200,menubar=yes,status=yes)"

Categories

Resources