I am working on a simple Chat right now, its already working, but its not very efficient.
Whenever I run my script it refreshes the whole Chat and loads the content over and over again.
Im trying to find a way that the script only loads new content from my chat database, the old messages shouldnt load over and over again.
This is the part from my index.php:
<div id="chat" class="full rund">
<table border="1" class="full chattable">
<?php
$abfrage = "SELECT * FROM chat";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)) {?>
<tr class="chattr">
<td style="width:200px;"><h3 class="text">ƬψƬ ☢ <?php echo $row->name;?>:</h3></td>
<td style="width:440px;"><p><h3 class="text"><?php echo $row->text;?></h3></p></td>
<td style="width:160px;"><h3 class="text"><?php echo $row->timestamp;?></h3></td>
</tr>
<?php
}
?>
</table>
</div>
This is my chat.js:
setInterval( "updateShouts()", 1000 );
function fx(form,target){
var _target=target;
var url=form.action;
var data=$(form)[(form.method.match(/^post$/i))?'serializeArray':'serialize']();
$(_target).load(url,data,function(){setTimeout(function(){
$(_target).empty();
},0);});
return false;}
function updateShouts(){
$('#chat').load('php/latestMsg.php');
}
My latestMsg.php (same as index):
<div id="chat" class="full rund">
<table border="1" class="full chattable">
<?php
$abfrage = "SELECT * FROM chat";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)) {?>
<tr class="chattr">
<td style="width:200px;"><h3 class="text">ƬψƬ ☢ <?php echo $row->name;?>:</h3></td>
<td style="width:440px;"><p><h3 class="text"><?php echo $row->text;?></h3></p></td>
<td style="width:160px;"><h3 class="text"><?php echo $row->timestamp;?></h3></td>
</tr>
<?php
}
?>
</table>
</div>
I hope anyone can help me, ive been searching for a long time now and I couldnt find anything that solves my problem!
Thanks :)
2 Solutions.
You will need a way to mark your messages as read, per user of course.
You can ignore that and only read back the last X messages.
in your chat table, you must add 'unread' field or something, to identify the message status (read/unread) , same like #geggleto solution
add filter to your query to something like this :
$abfrage = "SELECT * FROM chat WHERE timestamp between '$from' AND '$to' ";
to make sure you have a role to limit message by time
Related
I started learning webdeveloping and i tried to send "id" of one of the rows generated from database to another page. The rows are clickable thanks to the javascript code, so i can choose whichever row i want. The problem is, that even though the POST method seems right:
<form id="send" method="POST" action=<?php echo "secondpage.php?id=". $row['id']; ?> ></form>
// In inspect of the main page it gets the value.
However
second page always receive id value of 1. Doesn't matter if i click on the row with id=18 or any other. It will always recieve value of 1...
I heard that this could be a problem with javascript code which i put under PHP code.
Here is a code with PHP:
<div id="content">
<table id="usersTable" class="table table-bordered table-hover table-sm ">
<form action=http://localhost/dodawanie.php>
<input class="btn btn-primary" type="submit" value="Dodawanie">
</form>
<?php if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {?>
<tr>
<td><?php echo "id: ". $row['id']; ?> </td>
<td><?php echo "Name: ". $row["first_name"]; ?> </td>
<td><?php echo "Last: ". $row["last_name"];?> </td>
<form id="send" method="POST" action=<?php echo "secondpage.php?id=". $row['id']; ?> >
</form>
</tr>
<?php }
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</div>
Here is javascript:
<script type = "text/javascript">
$(document).ready(function(){
$('#usersTable').find('tr').click( function(){
// alert('You clicked row ' + ($(this).index()+1) );
$('#send').submit();
});
});
</script>
I would gladly accept any help to find an error here.
Change the <form id="send" id value as unique
or use a class some thing like below:
<form class="form_send" then in your javascript search for the form_class inside the clicked tr:
$(document).ready(function(){
$('#usersTable').find('tr').click( function(){
$(this).find('form.form_send').submit();
});
});
Ids have to be unique. $('#send').submit() only finds and submits the first form with that id.
You could add your row id to the form's id attribute to make them unique for example.
As shown in the code below, I have a dropdown list, populated from table "tabela". Below it I have a table which I need it to be populated depending on the option select from the dropdown?
How can I do this?
<div class="box">
<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname; charset=utf8', 'user', 'pass');
$sql = "SELECT id, pref, nome FROM tabela GROUP BY pref,nome ORDER BY nome";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll();
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['pref']; ?> - <?= $user['nome']; ?></option>
<?php endforeach; ?>
</select>
<table class="gradienttable">
<thead>
<tr>
<th colspan="7" class="tabelas">tipo1</th>
</tr>
<tr>
<th>Tipo1</th>
<th>Modelo</th>
<th>Qtde</th>
<th>Tipos</th>
<th>Pessoas</th>
<th>Vazios</th>
<th>Porcent</th>
</tr>
</thead>
<tbody>
<?php
foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM tabela WHERE prefixo=8510") as $row) {
printf(
"<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td style='padding:8px;'>%s - %s</td>
<td style='padding:8px;'>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>",
$row->pref, $row->nome, $row->model, $row->qtde, $row->tipos, $row->pessoas, $row->vazios, $ocupacao1 = round($row ->ocupacao1 * 100)/100 . '%');
}
?>
</tbody>
</table>
</div>
I think first of all you should change your foreach() statement into:
<?php foreach($users as $user) { ?>
<option value="<? echo $user['id']; ?>"><? echo $user['pref']; ?> - <?echo $user['nome']; ?></option>
<?php }; ?>
I find this a bit more clean and readable :)
Then you will need to enable the page to "filter" the rows by select - this can be done either by posting the select contents each time you change the select value (so basically you create a form that will submit itself every time you change what is in select - not very modern solution) or use AJAX to send the contents and retrieve the results.
If you decide to go second option (which I would highly recommend) you should take a look at some tutorials on changing page content basing on AJAX response. I would recommend to go to jQuery for that - since you should find quite some functions there that would help you out...
Using the way you are gathering the data you need to echo the results into the html.
<?php foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM infografico WHERE prefixo=8510") as $row) { ?>
<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td><?php echo $row->pref; ?></td>
...
</tr>
<?php } ?>
It would most likely be better to gather the data you need in the table first and form your array as required, then loop through that array and echo into the table.
Like in the title I would like to use a variable inside the hyperlink to use it into the modal window.
I am not using bootstrap, it is a custom code but it works until I try to put some kind of <a href='#openModal?id=".$VARIABLE."'>
Is it possible to do that?
Regards
Update:
<?php
$query = "SELECT * FROM USER";
$result = mysqli_query ($connection,$query)
or die ("You couldn’t execute query");
echo "<div class='admin-table'>
<table cellspacing='15'>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC))
{
extract ($row);
echo "<tr>\n
<td>$USER_LASTNAME</td>\n
<td>$USER_FIRTSNAME</td>\n
<td>$USER_PHONE</td>\n
<td>$USER_ADDRESS</td>\n
<td>$USER_POSTCODE</td>\n
<td>$USER_DOB</td>\n
<td>$USER_EMAIL</td>\n
<td>$USER_PASSWORD</td>\n
<td>$USER_ROLE</td>\n
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>\n
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>\n
</tr>\n";
echo "<tr><td colspan ='15'><hr></td></tr>\n";
}
echo "</table></div>\n";
?>
<div id="openModal?id=<?php echo $USER_ID; ?>" class="modalDialog">
<div>X
<h2>$USER_ID</h2>
</div>
</div>
It is working the modal but its just taking the last id, I have to think another solution to pass the variable.
Many thanks for your help
Update 2:
Thank you very much! Now its working,
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>
<div id='openModal?id=".$USER_ID."' class='modalDialog'>
<div><a href='#close' title='Close' class='close'>X</a>
<h2>".$USER_ID."</h2>
<p>You can have additional details here.</p>
</div>
</div>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
?>
Try this:
echo '<a href="#openModal?id='.$VARIABLE.'">';
Update:
echo '<td>Edit</td>\n';
echo '<td>Delete</td>\n';
any reason you can't use onclick ?
<a href="#" onclick="myJsFunc();">
<script>
function myJsFunc() {
//code to open modal
}
</script>
UPDATE
If i have understood you correct you are trying to make a table showing all users and then when the admin clicks at element a modal pop ups providing additional info for the particular user.
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal".$USER_ID."'>Edit</a></td>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
//Fetch again the rows to make the modals with the edit info
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo '
<div id="openModal'.$USER_ID.'" class="modalDialog">
<div>X
<h2>'.$USER_ID.'</h2>
<p>You can have additional details here.</p>
</div>
</div>';
}
?>
Old answer before the comments
It seems that single quotes and/or double quotes aren't escaped properly.
Also remember that in PHP string concatenation is made using " . " (dot) and in JavaScript using " + " (plus).
Update 1
I think that if you use the following you will be ok.
echo '<td>Delete</td>';
echo '<td>Edit</td>';
Update 2
Don't forget to add $variable at div too so <a> and div id are the same the same. E.g.
echo '<div id="openModal'.$variable.'" class="modalDialog"></div>';
So far I'm having limited success with AJAX. I have a couple of textboxes I want to fill with values from the database, only nothing is being inserted. Something is happening though which I guess is a good sign, but they're specific to 2 values and they're errors. Nothing seems to be out of the ordinary about these values either. The error report says: Parse error: syntax error, unexpected '}' in getemp.php, but everything lines up correctly and it never happens anywhere else in the database but these values that are back to back. Also since I'm using a table with listboxes no matter which box I choose from it only affects one entry into the table, however I thought the loop would take care of that.
So here's the code I'm trying to use AJAX on:
<? require_once("connect_to_DB.php"); // inserts contents of this file here ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New Order Form/Edit Order Form</title>
<link rel="stylesheet" href="hw2.css"/>
<?
connectDB();
session_start();?>
<script src="validation.js"></script>
<script src="jquery.js"></script>
<script type="text/javascript">
function showUser(str){
if(str==""){
$("#txtHint").html("");
return;
}else{
$("#txtHint").load("getemp.php?q="+str);
};
}
</script>
</head>
<body>
<?
include ('navbar_func.php');
echo navbar();
// Establish a connection with the data source, and define the SQL
$strSQL = "SELECT product_name FROM product";
$rs = mysqli_query($db, $strSQL) or die("Error in SQL statement: " . mysqli_error());
$row = mysqli_fetch_array($rs);
// Establish a connection with the data source, and define the SQL for the orders
$newID = 101;
$rndSQL = "Select order_id FROM salesorder WHERE order_id = ".$newID;
while(mysqli_num_rows(mysqli_query($db, $rndSQL)) > 0){
echo "<script>console.log($newID)</script>";
$newID++;
$rndSQL = "Select order_id FROM salesorder WHERE order_id =". $newID;
}
?>
<?$today = date("F j, Y");?>
<form name="orderform" method="post" action="new_order_result.php" onsubmit="return validate_order()">
<table>
<tr>
<td>Order Number:</td>
<td><label id="order" type="text" name="ordernumber" value="<?=$newID?>"><?=$newID?></label>
</td>
<td>Order Date:</td>
<td><label type="text" name="orderdate" value="<?=$today?>"/><?=$today?></label></td>
</tr>
<tr>
<td> Customer:</td>
<td><input id="customer"type="text" name="customer" value=""/></td>
</tr>
<tr>
<td>Sale Agent:</td>
<td><input id="salesagent" type="text" name="salesagent" value=""/></td>
<td>Order Status:</td>
<td><input id="orderstatus" type="text" name="orderstatus" value=""/></td>
</tr>
</table>
<table border = "1">
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<?$rs = mysqli_query($db, $strSQL) or die("Error in SQL statement: " . mysqli_error());?>
<?for($x=0; $x <= 19; $x++)
//just needs to post quantity not which
{?>
<tr>
<td>
<select name="P<?=$x?>" onchange="showUser(this.value)">
<option value="">Choose the product you'd like to purchase:</option>
<?while($row = mysqli_fetch_array($rs)){?>
<?print '<option value="'.$row[0].'">' . $row[0] . '</option>' . "\n";}//This is uses the datebase values?>
</select>
</td>
<td>
<div id="txtHint"><input type="text" name="M<?=$x?>" value="0"></input></div>
</td>
<td><select name="Q<?=$x?>" value="$row[1]">
<?for($i = 0; $i < 10; $i++){
print "<option value=$i>$i</option>";}//This uses the datebase values?>
</select></td>
</tr>
<? $rs = mysqli_query($db, $strSQL); //resets pointer in database.?>
<?}?>
</table>
<center>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</center>
</form>
</body>
</html>
Here's getemp.php which is being used in the script.
<? require_once("connect_to_DB.php"); // connect to furniture database
// ###################### retrieve data from database #################
connectDB();
$sql = "SELECT * FROM product WHERE product_name = " . $_GET["q"];
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
// ###############################################################
while($row = mysqli_fetch_array($result))
{
print $row['product_cost']
}
mysqli_close($db);
?>
As of right now the only way I can test for a reaction is by having "div" tags surrounding the textbox.
Thank you so much for the help or tips. I've spent so many hours on just this small thing and I can't make much more progress more than this. I've tried everything and looked everywhere for possible solutions, but none seemed to work. Thanks again!
In getemp.php:
print $row['product_cost'] is missing a semicolon.
Should be:
print $row['product_cost'];
Which is why you're seeing the error Parse error: syntax error, unexpected '}' in getemp.php
I have a html table that is populated using PHP:
<table class="flat-table flat-table-1" width="100%">
<tr style="background-color:#FFF;">
<td class="table-head">Name</td>
<td class="table-head">Review</td>
<td class="table-head">Rating</td>
</tr>
<?php
//run a query to find all the fields in the review table that belong to the specific hall, using the id in the url ($current_id)
if ($r = $db->prepare("SELECT * FROM reviews WHERE hall_id = :current_id")) {
//bind the parameters used in the above query using the 'current_id' variable
$r->bindParam(':current_id', $current_id);
//Execute the prepared query
$r->execute();
//search review table for all fields and save them in $review
$reviewtemp = $r->fetchAll();
foreach( $reviewtemp as $review) {
//loop and output the reviews
?>
<tr>
<td><? echo $review['name'];?></td>
<td><? echo $review['review']; ?></td>
<td><? echo $review['overall']; }}?></td>
</tr>
</table>
The - <td><? echo $review['review']; ?></td>' - contains longs pieces of text that need to be shorted using a show more/less button, if the piece of text is over 400 characters.
I have tried using pieces of code I have found on the internet but have had no luck. I am hoping that maybe somebody on here can point me in the right direction? I am happy to use any solution I can.
The following piece of code is the kind of thing I need to use, however I am unsure how to implement it into my code: http://jsfiddle.net/Wpn94/326/
(--I have previously asked a similar question before but had no responses. I have now changed the question and have posted it again--)
In the following code, I updated the HTML to contain more than one review and use tables.
http://jsfiddle.net/3VTb7/
The structure that is impotant for the javascript to work is this:
<table>
<tr><td>Name</td></tr>
<tr><td><div class="content hidecontent">insanely long review here</div><div class="show-more">Show More</div><td></tr>
</table>
Note: You must have the hidecontent class. The show more div should be directly after the content you want to hide and include the show-more class.
Without looking at the code you tried to mix together, I cannot determine what else can be wrong.
This might be an error: <td><? echo $review['overall']; }}?></td>
Since you're looping the table rows, } should go after </tr>
<tr>
<td><? echo $review['name'];?></td>
<td><? echo $review['review']; ?></td>
<td><? echo $review['overall']; ?></td>
</tr>
<?php }} ?>
To answer question, maybe something like:
First, if you have review_id use that otherwise add $id = 0; before if ($r = $db->prepare("...
Split review into two..
<td><?php
$review = $review['review'];
$short_length = 200;
$short = substr($review, 0, $short_length);
$long = substr($review, $short_length);
$id++;
echo $short . '<span class="content hidecontent" id="review'.$id.'">'.$long.'</span>
Show More';
?></td>
Then use jQuery to toggle
$("#hidecontent").click(function(){
var reviewid = "#review"+$(this).data("id");
$(reviewid).slideToggle(500, function(){
var moreless = $(reviewid).is(":visible") ? 'Less' : 'More';
$(this).html("Show "+moreless)
});
});