I have a div (inbox-chat) which contains data retrieved from database using ajax. the content is refreched every 5 seconds.
the div have a scroll bar. the problem here is when the div is refreshed to load new data, the scroll bar goes to top, and then the user cannot read what is written on the bottom of the div content.
the content is loaded here
messages.php
<div id="inbox">
<img src="assets/images/loaders/preload.gif" class="loading_inbox" >
</div>
here is an example of HTML code
ajax/inbox.php (EIDTED)
<?php
ob_start();
session_start();
require_once '../../config/connect.php';
require_once '../../functions/func.php';
$uid = $_GET['sender_id'];
$uid = get_username_id($_GET['sender_id']);
$stmt = $PDO->prepare("SELECT * FROM `forum_inbox`
WHERE
(sender_id=? AND receiver_id=?)
OR
(sender_id=? AND receiver_id=?)
ORDER BY dateMsg DESC");
$stmt->execute(array($uid, $_SESSION['id_userf'], $_SESSION['id_userf'], $uid));
$messages = $stmt->fetchAll(PDO::FETCH_OBJ);
?>
<?php if($stmt->rowCount() != 0): // STARTS CHECK IF ANY RECORDS EXISTS IN INBOX TABLE ?>
<div id="inbox-chat">
<section class="chat-container">
<ol class="chat-box">
<div class="new-msg"></div>
<?php
foreach($messages AS $msg):
if(get_username($msg->sender_id) == $_SESSION['usernamef']){
$direction = 'another';
}else{
$direction = 'me';
}
?>
<li class="<?php echo $direction ?>">
<div class="avatar-icon">
<a title="Voir profil de <?php echo get_username($msg->sender_id); ?>" href="profil.php?user=<?php echo get_username($msg->sender_id) ?>&pane=profile">
<img src="assets/images/profiles/<?php echo get_user_profile_pic($msg->sender_id)?>">
</a>
</div>
<div class="messages">
<p><?php echo $msg->message ?></p>
<time datetime="<?php echo convert_date_format_full(convert_timestamp($msg->dateMsg));?>">
<?php echo time_stamp($msg->dateMsg);?>
<i class="fa fa-clock-o"></i> <?php echo convert_date_format_full(convert_timestamp($msg->dateMsg)); ?>
</time>
</div>
</li>
<?php endforeach; ?>
</ol>
</section>
</div>
<?php else: ?>
<div class="panel-body" style="width:50%; margin:5px auto;text-align:center;">
<div class="media v-middle">
<h2>Pas de message</h2>
<img src="assets/images/no-msg.png">
</div>
</div>
<?php endif; // ENDS CHECK IF ANY RECORDS EXISTS IN INBOX TABLE ?>
js file (inbox.js)
$(document).ready(function(){
var sender_id = $(".u").val();
var $container = $("#inbox");
$.ajax({
url : 'ajax/inbox.php',
data : 'sender_id='+sender_id,
before : function(){
$(".loading_inbox").show();
},
success : function(data){
$(".loading_inbox").hide();
$("#inbox").html(data);
}
});
var refreshId = setInterval(function()
{
$container.load('ajax/inbox.php?sender_id='+sender_id);
}, 10000);
});
Im using this css for the inbox-chat class
#inbox-chat .chat-container {
width: 100%;
margin: 10px auto;
height: 450px;
background-color: #FAFCFB;
}
#inbox-chat .chat-box {
list-style: none;
background: #fdfdfd;
margin: 0;
padding: 0 0 50px 0;
height: 100%;
overflow-y: auto;
}
#inbox-chat .chat-box li {
padding: 0.5rem;
overflow: hidden;
display: flex;
}
#inbox-chat .chat-box .avatar-icon {
width: 50px;
position: relative;
}
#inbox-chat .chat-box .avatar-icon img {
display: block;
width: 100%;
background-color:#fff;
padding: 3px;
border-radius:50%;
}
#inbox-chat .me {
justify-content: flex-end;
align-items: flex-end;
}
#inbox-chat .me .messages {
order: 1;
border-bottom-right-radius: 0;
}
#inbox-chat .me .avatar-icon {
order: 2;
}
#inbox-chat .messages {
background: white;
padding: 10px;
border-radius: 2px;
width: 80%;
border: 1px solid #c9cccd;
color:#424242;
}
#inbox-chat .messages p {
font-size: 13px;
margin: 0 0 0.2rem 0;
}
#inbox-chat .messages time {
font-size: 0.7rem;
color: #ccc;
}
Take a look at the div from here
http://jsfiddle.net/devstar/rgjfqsb2/
That's because you're replacing the entire content of your inbox element with $("#inbox").html(data);.
Before replacing, save the scroll position with var scroll=$("#inbox")[0].scrollTop; and after replacing, restore it with $("#inbox")[0].scrollTop=scroll;.
I don't know if you're ajaxing the entire message history or only maybe the 10 newest and if newer messages are at the end or at the beginning - but you may need to calculate heights of the new elements and add/substract it to/from the scroll value.
Edit:
Btw, why are you ajaxing all the messages every 5 seconds, and not just the new ones and append or prepend them next to the existing messages? This would eliminate the need for storing and restoring the scroll position.
Add message id to each of your messages and a class to select them with:
<li class="message me" data-messageid="65">
<div class="avatar-icon">
<img src="http://simpleicon.com/wp-content/uploads/user1.png">
</div>
<div class="messages">
<p>Hi buddy !</p>
</div>
</li>
Then select the newest message and send that to your php handler:
var sender_id = $(".u").val();
var newest_msg=$(".message:last").data("messageid");
$.ajax({
url : 'ajax/inbox.php',
data : {
sender_id: sender_id,
newest_msg: newest_msg
},
success : function(data){
$("#inbox").append(data); //append it, not replace
}
});
And in your PHP:
$uid = $_GET['sender_id'];
$uid = get_username_id($_GET['sender_id']);
$newest_msg = $_GET['newest_msg'];
$stmt = $PDO->prepare("SELECT * FROM `forum_inbox`
WHERE
(sender_id=? AND receiver_id=?)
OR
(sender_id=? AND receiver_id=?)
AND
message_id > {$newest_msg}
ORDER BY dateMsg DESC
");
Notice I select only messages with id's greater than the newest message.
something like this?
var refreshId = setInterval(function()
{
$container.load('ajax/inbox.php?sender_id='+sender_id);
$(".chat-box").css("height", "auto");
var height = $(".chat-box").height();
$(".chat-box").css("height", 450);
$(".chat-box").scrollTop(height);
}, 10000);
replace this function.
Related
I am building a page where various images, retrieved from a database, are displayed on two columns.
What I want, is there to appear some details about each image (e.g., the country and the year it was taken in), extracted from the same database, whenever I hover over it, as seen here.
This is my code:
HTML:
<!-- Photo Library -->
<div class="content">
<div class="row">
<div class="column">
<?php
include("getimg.php");
?>
</div>
</div>
</div>
PHP:
<?php
$query = "SELECT * FROM photos ORDER BY id DESC";
$result = mysqli_query($mysqli, $query);
while($photo = mysqli_fetch_assoc($result)) {
echo '<img id="myimg" src="'. $photo['photopath'] .'"
alt="'.$photo['photoname'].'"height:"500" width="640">'; }
?>
CSS:
.content {
background-color: #e8d9d9;
}
.row {
display: -ms-flexbox; /* IE 10 */
display: flex;
-ms-flex-wrap: wrap; /* IE 10 */
flex-wrap: wrap;
padding: 0 18px;
}
.column {
-ms-flex: 50%; /* IE 10 */
flex: 50%;
padding: 4 4px;
}
.column img {
margin-top: 8px;
margin-left: 8px;
vertical-align: middle;
}
.column img:hover {
opacity: 0.5;
}
I tried to mimic excatly what it says here https://jsfiddle.net/govdqd8y/, like this:
<div class="column">
<?php
include("getimg.php");
?>
<div class="img__description_layer">
<p class="img__description"> (whatever text) </p>
</div>
</div>
but nothing happened.
What could I do?
Even though you css example doesn't work in the Safari browser (at least for me), this is one quick & dirty solution. Nevertheless, I would recommend you to look into the MVC pattern to separate HTML + PHP code.
<?php
$query = "SELECT * FROM photos ORDER BY id DESC";
$result = mysqli_query($mysqli, $query);
while($photo = mysqli_fetch_assoc($result)) {
echo '<div class="img__wrap">';
echo '<img id="myimg" class="img__img" src="'. $photo["photopath"] .'" alt="'.$photo["photoname"].'" height="500" width="640">';
echo '<div class="img__description_layer">';
echo '<p class="img__description">'.$photo["photoname"].'</p>';
echo '</div>';
echo '</div>';
}
?>
See I have kept your HTML elements in order and edited some css
<div class="column">
<img class="img__img" src="http://placehold.it/257x200.jpg" />
<div class="img__description_layer">
<p class="img__description"> (whatever text) </p>
</div>
</div>
</div>
CSS
.row {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
padding: 0 18px;
}
.column {
position: relative;
}
.column .img__img {
display: block;
}
.img__description_layer {
position: absolute;
top: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
background: #f0f8ff91;
opacity: 0;
transition:all .5s;
}
.column:hover .img__description_layer {
opacity: 1;
}
What's up guys!
I'm joking a little with Laravel and I'm setting up a news area, which when clicking the div of the news the complete news overlaps. And I was able to make the css and div complete, but the overlapping news is just id = 1.
How can I correct to get the full story according to her id? As an example of what is happening:
https://screenshots.firefoxusercontent.com/images/be917d35-3aa5-408c-87d1-839fea32a4f6.png
https://screenshots.firefoxusercontent.com/images/fbc99ce8-c76c-4a54-9ece-d77eb0d750d9.png
I do not know if it was understandable, but the news I bring in the foreach for the "noticia" div gets correct but when cliaring and bring the "notice-full" it after only the complete news of the one that owns the id = "1" instead id = "2" ...
HTML:
<div class="noticiario">
<?php foreach ($noticias as $noticia) { ?>
<div class="noticia" data-noticia-id="{{ $noticia->id }}">
<div class="imagem-noticia" style="background-image: url('{{ $noticia->img }}')"></div>
<div class="descricao-noticia">
<h2 class="titulo-noticia">{{ $noticia->titulo }}</h2>
<span class="sobre-noticia">{{ $noticia->resumo }}</span>
</div>
<div class="noticia-inteira">
<span class="noticia-titulo"><?php echo $noticia->titulo; ?></span>
<?php echo $noticia->texto; ?>
<a class="fechar-noticia"><i class="fa fa-times-circle" aria-hidden="true"></i></a>
</div>
</div>
<?php } ?>
CSS:
.noticiario .noticia {
cursor: pointer;
display: inline-block;
margin-top: 25px;
width: 360px;
.noticiario .noticia-inteira {
background: #e8ecef;
border: 1px solid #dadada;
border-radius: 6px;
box-sizing: border-box;
display: none;
height: 100%;
left: 0;
padding: 20px 50px;
position: absolute;
top: 0;
width: 98%;
JS:
$(document).ready(function() {
$(".noticia-inteira").hide();
$(".noticia").bind("click",function(){
$(".noticia-inteira").slideToggle(300);
return false;
});
});
If anyone could help, I would be very grateful, right away!
Try this solution:
$(".noticia").bind("click",function(){
$(this).find(".noticia-inteira").slideToggle(300);
});
I have been creating a small map with different positions for a fabric. My goal is to make sure I can drag 1, 2, 3 and 4 (persons) into a position (e.g. Production Leader) and save the position of the DIV into the MySQL Database. I want a global map so each person which visit this page will see the same.
I created different DIV's for each person (1, 2, 3 and 4) which are already in the database.
I'm stuck right now.. can somebody help me?
Fiddle: https://jsfiddle.net/fj1zgw2o/
Database connection and showing persons from the database:
function choosePerson() {
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, image, position FROM Persons";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="boxPersons posLeft" id="'. $row['id'] .'">'. $row['id'] .'</div>';
}
} else {
echo "0 results";
}
}
// Move the box into the corresponding id.
// When you load your boxPersons have each
// person an id column that matches what position they will be in.
$(".boxPersons").each(function(e){
var target = $(this).attr("data-id");
$("#"+target).append($(this));
});
$(".boxPersons").draggable({
revert: 'invalid',
containment: '.box',
cursor: 'move'
});
$(".openSpots, .box-persons").droppable({
accept: ".boxPersons",
drop: function(event, ui) {
var droppable = $(this);
var draggable = ui.draggable;
// Move draggable into droppable
draggable.appendTo(droppable);
draggable.css({
top: '0px',
left: '0px'
});
}
});
.box-content {
display: flex;
}
.toPlan {
width: 10%;
}
.overviewPlanning {
flex: 1;
}
.box-persons {
overflow: hidden;
border-radius: 4px;
border: 1px solid #d8d8d8;
}
.boxPersons {
width: 60px;
height: 72px;
padding: 5px;
margin: 10px;
text-align: center;
border: 1px solid #d8d8d8;
border-radius: 4px;
z-index: 99;
float: left;
background: #888;
}
.posLeft {
float: left;
}
.openSpots {
width: 60px;
height: 72px;
padding: 5px;
margin: 10px;
text-align: center;
border: 0.5px dashed #000000;
border-radius: 4px;
z-index: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<header>
Header
</header>
<div class="box">
<div class="box-content">
<div class="toPlan">
<div class="productionLeader">
<strong>Production Leader</strong>
<div id="Leader" class="openSpots" id="openSpots">
</div>
<strong>Free</strong>
<div id="Free" class="openSpots positionFree">
</div>
<strong>Ill</strong>
<div id="Ill" class="openSpots positionIll">
</div>
<strong>Otherwise</strong>
<div id="Otherwise" class="openSpots positionOtherwise">
</div>
</div>
</div>
<div class="overviewPlanning">
Fabric map
</div>
</div>
<div class="box-persons">
Available collegues (to drag and drop into a spot)<br>When you load the data into this box change the id to match what was saved. You can use AJAX to keep everything synced.<br>
<div class="boxPersons" data-id='Free'>bob</div>
<div class="boxPersons" data-id='Ill'>marry</div>
<div class="boxPersons" data-id=''>mark</div>
</div>
</div>
<footer>
Footer
</footer>
I am working on a project and here I need to fetch data from a MySQL database.
And I need to fetch only that data which contain 38.
For example - I have a table which contain user_from and user_to and msg. Okay, now I have to fetch all data if user_from contain 38.
So I am able to fetch it but the problem is that is it fetching only one row from database nut I want all row which contain user_from 38.
So Please help me.
As Fast As you can
Codes----
<?php
include 'navigation.php';
include 'conn.php';
if(!$_SESSION["login"]){
header("location:index.php");
die;
}
if ($_SESSION['SESS_MEMBER_ID']) {
$id = $_SESSION['SESS_MEMBER_ID'];
} else {
echo "Sorry We are unable to get your id";
}
echo "<center><button type='button' class='btn btn-default' id='myBtn'> Send a message </button></center>";
?>
<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
#-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
#keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
select {
width: 100%;
}
</style>
</head>
<body>
<!-- Trigger/Open The Modal -->
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Send a Message</h2>
</div>
<div class="modal-body">
<label for="Friends">Select Friends:</label>
<select id="country" name="country">
<option value="Select Friends">Select Friends...</option>
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
<br> <br>
<textarea cols="149" rows="12"></textarea>
<button type='button' class='btn btn-default' id='myBtn' name="sendmsgnow"> Send Now </button>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
<?php
$aquery = "SELECT * FROM register WHERE id='$id'";
$aresult = mysqli_query($conn,$aquery);
while ($row = mysqli_fetch_assoc($aresult)) {
$user_id[] = $row["id"];
}
if (isset($_POST["sendmsgnow"])) {
$query = "INSERT INTO chats (user_from,user_to,msg_body,userdate,usertime) VALUES ('$id','1','$message','')";
$result = mysqli_query($conn,$query);
if ($result) {
echo "Your Message has been sent";
}
}
$b_query = "SELECT * FROM chats WHERE user_from='$id'";
$b_result = mysqli_query($conn,$b_query);
while ($data = mysqli_fetch_array($b_result)) {
$messages[] = $data["msg_body"];
$user_from = $data["user_from"];
if($user_from==38){
$d_query = "SELECT * FROM register WHERE id='$user_from'";
$d_result = mysqli_query($conn,$d_query);
while ($fdata = mysqli_fetch_assoc($d_result)) {
$frname = $fdata["firstname"];
echo "$frname sent you a message <br> $messages[]";
}
}
}
?>
As I told in comments you can use a nested while loop for thisUpdated
while ($data = mysqli_fetch_array($c_result)) {
$messages[] = $data["msg_body"];
$user_from = $data["user_from"];
if($user_from==38){
$d_query = "SELECT * FROM register WHERE id='$user_from'";
$d_result = mysqli_query($conn,$d_query);
while ($fdata = mysqli_fetch_assoc($d_result)) {
$frname = $fdata["firstname"];
}
} //This is end of if
echo "$frname sent you a message <br> $messages[]";
}//This END of First while loop
I have wrote what I have understood. Hope it helps you
How to display one by one fadein element from db when using loop for ?
.........................................................................................
When i load page index.php , it's will display 40 element from db,
but i want to apply code to show one by one element fadein , How can i do that ?
use in IE7
.
.
index.php
<?php
for($i=0;$i<40;$i++)
{
$strSQL = "SELECT * FROM products order by id desc Limit $i,1 ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mysql_fetch_array($objQuery);
$products_img_thumbnail_path_grid[$i] = $objResult["products_img_path"];
}
?>
<ul>
<?PHP
for($i=0;$i<40;$i++)
{
?>
<li class="thumbnail" style=" list-style: none; float: left; margin: 7px; width: 80px; ">
<div class="imgWrap" style=" float: left; height: 80px; " >
<a href="xxxxx.php">
<img border="0" src="<?PHP echo $products_img_thumbnail_path_grid[$i]; ?>" width="80" height="80" style=" background-color: #fff; border: 1px solid #aaa; "/>
</a>
</div>
</li>
<?PHP
}
?>
</ul>
Check this answer:
Using CSS for fade-in effect on page load
Basically you want to use css3 animations to apply a fade in effect to the images
var test = $('ul li');
current = 0;
test.hide();
Rotator();
function Rotator() {
$(test[current]).fadeIn('slow').delay(5000).fadeOut('slow');
$(test[current]).queue(function() {
current = current < test.length - 1 ? current + 1 : 0;
Rotator();
$(this).dequeue();
});
}
use above code after the ul section, hope it work for you
.