Currently I'm learning Server Sent Events. Everything is fine if I make a simple HTML application. But when I turn into AngularJS, the problem appears. Server Sent Events is not continually called in Firefox. It works only once. This problem doesn't happen in Chrome and Opera.
Is this some issues regarding AngularJS performance in Firefox? Or is there anything wrong with my code? Any answer would be very appreciated! Thanks.. :)
Index.php
<body ng-app='' ng-controller="myCtrl">
<table class="table table-bordered table-hover table-responsive table-striped">
<thead>
<tr>
<th>No.</th>
<th>Id</th>
<th>Name</th>
<th>Level</th>
<th>Hapus</th>
</tr>
</thead>
<tr ng-repeat="x in admin">
<td>
{{$index + 1}}.
</td>
<td>
<input type="text" value="{{x.Id}}" class="init" readonly>
</td>
<td>
<textarea ng-keyup="myFunc($event, x.Id, 'Name')" class="init" ng-focus="edit($event)" ng-blur="noedit($event, x.Id, 'Name')">{{x.Name}}</textarea>
</td>
<td>
<textarea ng-keyup="myFunc($event, x.Id, 'Level')" class="init" ng-focus="edit($event)" ng-blur="noedit($event, x.Id, 'Level')">{{x.Level}}</textarea>
</td>
<td>
<button ng-click="sendDelete(x.Id)" class="btn btn-warning">Hapus</button>
</td>
</tr>
</table>
<div id="result"></div>
<script src="js/jquery-1.9.1.js" type="text/javascript"></script>
<script src="js/angular-1.2.25.min.js" type="text/javascript"></script>
<script type="text/javascript">
function myCtrl($scope, $http, $window) {
if (typeof (EventSource) !== "undefined") {
// Yes! Server-sent events support!
var source = new EventSource("sse.php");
source.onmessage = function(event) {
msg = JSON.parse(event.data);
$scope.admin = msg;
$scope.$apply();
document.getElementById("result").innerHTML += msg.Id + ". " + msg.Name + ". " + msg.Level + "<br>";
console.log($scope.admin);
};
} else {
// Sorry! No server-sent events support..
alert('SSE not supported by browser.');
}
};
</script>
</body>
sse.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
require_once 'koneksi.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$query = "SELECT * FROM `admin` WHERE id='" . $id . "'";
} else {
$query = "SELECT * FROM `admin`";
}
$stmnt = $dbh->prepare($query);
$stmnt->execute();
$output = "[";
while ($row = $stmnt->fetch()) {
if ($output != "[") {
$output .= ",";
}
$output .= '{"Id":"' . $row["id"] . '",';
$output .= '"Name":"' . $row["name"] . '",';
$output .= '"Level":"' . $row["level"] . '"}';
}
$output .= "]";
echo 'data: ' . ($output) . "\n\n";
ob_flush();
flush();
sleep(1);
?>
Related
Can someone help me on pagination syntax for php webpage. I have a set a pagination code to display 20 records per page. The records are displaying based on 'start date' and 'end date'.
The issue is, it works on page 1 but when I click on page 2 in pagination then it won't work. I assume the values of $_POST['startdate'] and $_POST['enddate] are not forwarding on page2.
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="multitab/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<style>
.inline{
display: inline-block;
float: right;
margin: 20px 0px;
}
input, button{
height: 34px;
}
</style>
</head>
<body>
<form id="form1" name="form1" action="" method="post">
<table>
<tr>
<td><b>Start date:</b></td>
<td><input type="date" id="startdate" name="startdate" size="10"> </td>
<td><b>End date:</b></td>
<td><input type="date" id="enddate" name="enddate" size="10" max=<?php echo date('Y-m-d');?>></td>
<td><input type="submit" value="Search" name="Search" onClick="return check()"></td>
</tr>
</table>
</form>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("abc_db",$con);
$limit = 20;
if (isset($_GET["page"])) {
$pn = $_GET["page"];
}
else {
$pn=1;
};
if(isset($_POST['Search'])){
$startdate1 = $_POST['startdate'];
echo "Start date : ".$startdate1", ";
$enddate1 = $_POST['enddate'];
echo "End date : ".$enddate1;
}
$start_from = ($pn-1) * $limit;
$serial = (($pn-1) * $limit) + 1;
$today1= date("Y/m/d");
$days14_ago = date('Y/m/d', mktime(0, 0, 0, date("m") , date("d") - 14, date("Y")));
if ($startdate1 !=Null) {
$sql = "SELECT * FROM hd where datearrival='$startdate1' LIMIT $start_from, $limit";
} else if ($enddate1 !=Null) {
$sql = "SELECT * FROM hd where datearrival='$enddate1' LIMIT $start_from, $limit";
} else {
$sql = "SELECT * FROM hd where datearrival between '$days14_ago' and '$today1' LIMIT $start_from, $limit";
}
if (($startdate !=Null) && ($enddate !=Null)) {
$sql = "SELECT * FROM hd where datearrival between '$startdate1' and '$enddate1' LIMIT $start_from, $limit";
}
$rs_result = mysql_query ($sql);
?>
<div class="container">
<button style="height:25px;width:70px;" onclick="window.location.reload(true);">Refresh</button>
<br>
<div>
<font face='cambria'><p><span style="display:inline-block; margin-left: 650px; ">
</span></p> </font>
<font face="Cambria" size="2"> <table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th width="5%" valign="top">Sr#</th>
<th width="10%" valign="top">Date arrival</th>
<th width="10%" valign="top">Pno</th>
<th width="10%" valign="top">First name</th>
<th valign="top"></th>
<th valign="top"></th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($rs_result, MYSQL_ASSOC)) {
$pno1=$row['pno'];
$datearrival1=$row['datearrival'];
$sr_num = $serial++;
?>
<tr>
<td><?php echo $sr_num; ?></td>
<td><?php echo date("d/m/Y", strtotime($row['datearrival'])); ?></td>
<td><?php echo $row["pno"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><p align="center">edit</font></td>
<td><p align="center">delete</font></td>
</tr>
<?php
};
?>
</tbody>
</table>
</font>
<div>
<ul class="pagination">
<?php
$sql = "SELECT COUNT(*) FROM hd where datearrival between '$startdate1' and '$enddate1'";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$k = (($pn+4>$total_pages)?$total_pages-4:(($pn-4<1)?5:$pn));
$pagLink = "";
if($pn>=2){
echo "<li><a href='qm.php?page=1'> << </a></li>";
echo "<li><a href='qm.php?page=".($pn-1)."'> < </a></li>";
}
for ($i=-4; $i<=4; $i++) {
if($k+$i==$pn)
$pagLink .= "<li class='active'><a href='qm.php?page=".($k+$i)."'>".($k+$i)."</a></li>";
else
$pagLink .= "<li><a href='qm.php?page=".($k+$i)."'>".($k+$i)."</a></li>";
};
echo $pagLink;
if($pn<$total_pages){
echo "<li><a href='qm.php?page=".($pn+1)."'> > </a></li>";
echo "<li><a href='qm.php?page=".$total_pages."'> >> </a></li>";
}
?>
</ul>
<div class="inline">
<input id="pn" type="number" min="1" max="<?php echo $total_pages?>"
placeholder="<?php echo $pn."/".$total_pages; ?>" required>
<button onclick="go2Page();">Go</button>
</div>
</div>
</div>
</div>
<script>
function go2Page()
{
var pn = document.getElementById("pn").value;
pn = ((pn><?php echo $total_pages; ?>)?<?php echo $total_pages; ?>:((pn<1)?1:pn));
window.location.href = 'qm.php?page='+pn;
}
/* start and end date- validation checks */
function check(){
var startdatea=document.getElementById("startdate").value;
var enddatea=document.getElementById("enddate").value;
if(Date.parse(startdatea)>Date.parse(enddatea)){
alert("Please select a different End Date.");
return false;
}
}
</script>
</body>
</html>
$_POST superglobal variable is populated by PHP when a POST HTTP request is processed. This is the case on the request for page 1.
However, your go2page() function is mutating location.href, which generates a GET HTTP request, and so does pagination links.
You should append your startdate and enddate params to the pagination URLs, to forward your params to the next/previous requests :
qm.php?page=<YOUR-PAGE-NUMBER>&startdate=<YOUR-START-DATE>&enddate=<YOUR-END-DATE>';
And use $_GET['startdate'] and $_GET['enddate] to retrieve those when processing GET requests.
More about $_POST https://www.php.net/manual/en/reserved.variables.post.php
More about GET AND POST HTTP requests : https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
There's an error in your isset($_POST['Search'])
your code:
if(isset($_POST['Search'])){
$startdate1 = $_POST['startdate'];
echo "Start date : ".$startdate1", ";
$enddate1 = $_POST['enddate'];
echo "End date : ".$enddate1;
}
possible working solution:
if(isset($_POST['Search'])){
$startdate1 = $_POST['startdate'];
echo "Start date : ".$startdate1.", ";
$enddate1 = $_POST['enddate'];
echo "End date : ".$enddate1;
}
Though I didn't try it yet, but I assume this is the cause, pagination in page2 will not work since the code is not executed after the error which is the pagination for page2.
EDIT: The . fixes things.
echo "Start date : ".$startdate1", ";
is different than
echo "Start date : ".$startdate1.", ";
Add values of your $_POST inside links and use them as $_GET so you would use both.
$startdate1=$enddate1=null; //to hide notice
if(isset($_POST['Search'])){
$startdate1 = $_POST['startdate'];
echo "Start date : ".$startdate1.", ";
$enddate1 = $_POST['enddate'];
echo "End date : ".$enddate1;
} else if (!empty($_GET['startdate']) && !empty($_GET['enddate'])){
$startdate1 = $_GET['startdate'];
echo "Start date : ".$startdate1.", ";
$enddate1 = $_GET['enddate'];
echo "End date : ".$enddate1;
}
$pagLink .= "<li><a href='qm.php?page=".($k+$i)."&startdate=".startdate1."&enddate=".$enddate1."'>".($k+$i)."</a></li>";
For your need:
$pagLink = "";
if($pn>=2){
echo "<li><a href='qm.php?page=1&startdate=".startdate1."&enddate=".$enddate1."'> << </a></li>";
echo "<li><a href='qm.php?page=".($pn-1)."&startdate=".startdate1."&enddate=".$enddate1."'> < </a></li>";
}
for ($i=-4; $i<=4; $i++) {
if($k+$i==$pn)
$pagLink .= "<li class='active'><a href='qm.php?page=".($k+$i)."&startdate=".startdate1."&enddate=".$enddate1."'>".($k+$i)."</a></li>";
else
$pagLink .= "<li><a href='qm.php?page=".($k+$i)."&startdate=".startdate1."&enddate=".$enddate1."'>".($k+$i)."</a></li>";
};
echo $pagLink;
if($pn<$total_pages){
echo "<li><a href='qm.php?page=".($pn+1)."&startdate=".startdate1."&enddate=".$enddate1."'> > </a></li>";
echo "<li><a href='qm.php?page=".$total_pages."&startdate=".startdate1."&enddate=".$enddate1."'> >> </a></li>";
}
That would help you to move dates to next page.
You need to set it to all paging links. Also you can make extra if statement to not pass empty GET
EDIT:
As I already told: You need to set it to all paging links. Also you can make extra if statement to not pass empty GET (that includes javascript)
function go2Page()
{
var pn = document.getElementById("pn").value;
pn = ((pn><?php echo $total_pages; ?>)?<?php echo $total_pages; ?>:((pn<1)?1:pn));
window.location.href = 'teztz.php?page='+pn+'&startdate=<?=startdate1;?>&enddate=<?=$enddate1;?>';
}
I'm new to both php and jquery, and i'm trying to create a webpage which shows players and it's status based on data from my Mysql server.
The base code works, although the code scraped together from multiple youtube tutorials :)
I would like to change the opacity of a player's row when it's status is, or becomes 0 (dead).
This is the code i have so far :
pagetest.php:
<!DOCTYPE html>
<html>
<head>
<title>pagetest</title>
</head>
<body>
<table id="show">
<tr>
<td>player</td>
<td>status</td>
</tr>
</table>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
$('#show').load('test.php')
}, 3000);
});
</script>
</body>
</html>
test.php :
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
$conn = mysqli_connect("myserver", "myuser", "mypass", "mydb");
if ($conn-> connect_error) {
die("Connection failed:". $conn->connect_error);
}
$sql = "SELECT player, status FROM mytable";
$result = $conn->query($sql);
if ($result-> num_rows > 0) {
while ($row = $result->fetch_assoc()){
echo "<tr><td>" . $row["player"] . "</td><td>" . $row["status"] . "</td></tr>";
}
echo "</table>";
}
else {
echo "No game data available";
}
?>
</body>
</html>
I'd really appreciate your help :)
Is there any reason for using jQuery with it? If no, then you can use this code:
<body>
<?php
$conn = mysqli_connect("host", "user", "password", "database");
if ($conn-> connect_error) {
die("Connection failed:". $conn->connect_error);
}
$sql = "SELECT player, status FROM mytable";
$result = $conn->query($sql);
if ($result-> num_rows > 0) {
echo '<table id="show">
<tr>
<td>player</td>
<td>status</td>
</tr>';
while ($row = $result->fetch_assoc()){
$status = $row["status"];
$player = $row["player"];
echo "<tr style='" . ($status == 0 ? 'opacity: 0;' : '') . "'><td>" . $player . "</td><td>" . $status . "</td></tr>";
}
echo '</table>';
}
else {
echo "No game data available";
}
?>
</body>
You can do something like that:
echo '<tr class="player-status-'.$row["status"].'"><td>'. $row["player"] .'</td><td>' . $row["status"] . '</td></tr>';
Add a class to your line tr based in the $row["status"] so rendered you'll have something like this: <tr class="player-status-0"><td>cristiano ronaldo</td><td>0</td></tr>
this way you can create a style to this class with css
.player-status-0 {
opacity:
}
By the way, have thought about using ajax and return a json with the data?
in the code i used csv file in which i want to edit the rows of 3rd coulmn as want to make them a http link by using tag but i cant do that. right now the code chowing only the link but i want to make them hyperlink
<html>
<head>
</head><body>
<table width="10%" cellpadding="1" cellspacing="0" border="1" ;">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<?php
$f = fopen("http://localhost/csv/cr.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
$data = count($line);
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
?>
</tr>
</thead>
</table>
</body>
</head>
try:
<?php
$f = fopen("http://localhost/csv/cr.csv", "r");
while (($line = fgetcsv($f)) !== false) {
if (!isset($line[2])) {
continue;
}
echo "<tr><td><a href='url'>" . htmlspecialchars($line[2]) . "</a></td></tr>";
}
fclose($f);
?>
I'm writing 2 queries and the first query is working fine but the 2nd query is not working. Please, Guide me.Thanks.
<table class="table table-bordered table-hover">
<form role="form" method="post" action="">
<div class="tablenav top">
<div class="alignleft actions bulkactions">
<label for="bulk-action-selector-top" class="screen-reader-text">Comment Action</label><select name="comment_status" id="bulk-action-selector-top">
<option value="" name="">Select Option</option>
<option value="Approve" name="Approve">Approve</option>
<option value="unapprove" name="unapprove" class="hide-if-no-js">Unapprove</option>
</select>
<input type="submit" name="submit" id="doaction" class="button action" value="Apply">
</div>
<br class="clear">
</div>
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Author</th>
<th>Comments</th>
<th>Email</th>
<th>Author Url</th>
<th>Status</th>
<th>Date</th>
<th>Post Name</th>
<th>Edit</th>
<th>Delete</th>
<th>Reply</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM comments";
global $connection;
$select_comments = mysqli_query($connection, $query) or die('Could not look up user information; ' . mysqli_error($connection));
while ($row = mysqli_fetch_array($select_comments)) {
$comment_id = $row['comment_id'];
$comment_post_id = $row['comment_post_id'];
$comment_author = $row['comment_author'];
$comment_date = $row['comment_date'];
$comment_email = $row['comment_email'];
$comment_author_url = $row['comment_author_url'];
$comment_content = $row['comment_content'];
$comment_status = $row['comment_status'];
echo "<tr>
<td><input type='checkbox' name='check_list[]' value='$comment_id'></td>
<td>$comment_id</td>
<td>$comment_author</td>
<td>$comment_content</td>
<td>$comment_email</td>
<td>$comment_author_url</td>
<td>$comment_status</td>
<td>$comment_date</td>
</tr>";
}
if (isset($_POST['submit'])) {
global $connection;
global $errors;
$comment_status = $_POST['comment_status'];
$check_box = isset($_POST['check_list']) ? $_POST['check_list'] : '';
// error messages
$missingcheckbox = "<p><stong>Recored box not checked.Please check the checkbox.</strong></p>";
// for name feild
if (!$check_box) {
$errors .= $missingcheckbox;
}
if ($errors) {
$resultMessage = '<div class="alert alert-danger">' . $errors . '</div>';
echo $resultMessage;
} else {
for ($i = 0; $i < count($_POST['check_list']); $i++) {
$id = $_POST['check_list'][$i];
if ($comment_status == 'Approve') {
$query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $id";
} elseif ($comment_status == 'Unapprove') {
$query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id = $id";
}
if ($approve_comments = mysqli_multi_query($connection, $query)) {
// header ("location: comments.php");
// exit;
$resultMessage = '<div class="alert alert-success">Data has been successfully Updated.<img src="../../assets/img/refresh.png" alt="Edit Client" title="Refresh" style="width:30px; height:30px; border:0;"></div>';
echo $resultMessage;
} else {
$resultMessage = '<div class="alert alert-warning">ERROR: Unable to excecute:' . $query . ' . ' . mysqli_error($connection) . '</div>';
echo $resultMessage;
}
}
}
}
?>
</tbody>
</table>
The first query is working fine but 2nd query is not working.I have problem in this code below:-
if($comment_status =='Approve'){
$query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $id";
}elseif($comment_status =='Unapprove'){
$query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id = $id";
}
Case matters when comparing two strings to be the same
<option value="unapprove"
^^^
and
elseif($comment_status =='Unapprove'){
^^^
$id its a variable and you are putting there as string. Example:
<?php
$a = "Hello ";
$b = $a . "World!"; // "Hello World!"
$a = "Hello ";
$a .= "World!"; // "Hello World!"
?>
You must do the same.
am trying to read data from an sql table using Angular,can't seem to find my problem cause my debugger isn't recognizing it,Attached is the html and php file am using,any here will be greatly appreciated.
here is the code:
HTML+JS
<div ng-app="" ng-controller="customersController" class="container">
<p><input type="text" ng-model="name" class="form-control"></p>
<table class="table table-bordered">
<tr>
<th>Full Name</th>
<th>Email Address</th>
<th>Password</th>
</tr>
<tr ng-repeat="x in names | filter:name | orderBy:'Fullname'">
<td>{{ x.Fullname }}</td>
<td>{{ x.Email }}</td>
<td>{{ x.Password }}</td>
</tr>
</table>
</div>
<script src="js/angular.min.js" type="text/javascript"></script>
<script>
function customersController($scope,$http) {
var page = "sql.php";
$http.get(page)
.success(function(response) {$scope.names = response;});
}
</script>
</body>
sql.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: text/html; charset=UTF-8");
$conn = new mysqli("localhost", "", "", "") or die(mysqli_error());
$result = $conn->query("SELECT * FROM `user` ORDER BY `id` ASC");
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"Full Name":"' . $rs["fullname"] . '",';
$outp .= '"Email":"' . $rs["emailAddress"] . '",';
$outp .= '"Password":"'. $rs["user_pass"] . '"}';
}
$outp .="]";
$conn->close();
echo($outp);
?>
You are sending a content-type header of text/html, and then constructing your own json string. Kill that header, and do as follows:
$outp = array();
while( $rs = $result->fetch_array(MYSQLI_ASSOC) ) $outp[] = $rs;
$conn->close();
echo json_encode($outp);
exit();