Show / hide php mysql results with loader - php

I have MySQL database results.
I want to show 3 rows and then hide rest.
When the user clicks to load more data then to appear all rows.
The problem is when I click show more, then only one more row appears.
<?php
$query_brands = mysql_query("SELECT distinct pd_filter1 from tbl_brands2 WHERE pd_code in (select pd_code from tbl_product where cat_id='2')") or die(mysql_error());
$count_brands = mysql_num_rows($query_brands);
if($count_brands > 0) {
while($fetch_brands = mysql_fetch_array($query_brands)) {
$record_brands[] = $fetch_brands;
}
}
$i_brands=0;
foreach($record_brands as $records_brands) {
?>
<table border="1" width="215" style="border-collapse: collapse; border-spacing: 0;" bgcolor="#eeeff0">
<tr>
<td>
<?php
$i_brands = $i_brands + 1;
if ($i_brands > 3)
{
?>
<div id="myDIV_Filter1_1" style="display:none";>
<?php
}
}
?>
<div id="myDIV_Filter1_2">
<span class="class22">
show more...
</span>
</div>
<div id="myDIV_Filter1_3" style="display:none";>
<span class="class22">
show less...
</span>
</div>
</td>
</tr>
</table>
JavaScript
function myFunction() {
var x_filter1_1 = document.getElementById("myDIV_Filter1_1");
var x_filter1_2 = document.getElementById("myDIV_Filter1_2");
var x_filter1_3 = document.getElementById("myDIV_Filter1_3");
if (x_filter1_1.style.display === "none") {
x_filter1_1.style.display = "block";
x_filter1_2.style.display = "none";
x_filter1_3.style.display = "block";
} else {
x_filter1_1.style.display = "none";
x_filter1_2.style.display = "block";
x_filter1_3.style.display = "none";
}
}

You have some errors on your code:
1) Try not to use return the way you are using right now in code lines like this one:
show more...
You can use it better like it's explained here
2) You have all your divs of the code example in display none, the user will never see the information in those divs because you don't have any code to start showing some of them. In the same line you put a ";" after the style but it must be inside the style. This line has an error:
<div id="myDIV_Filter1_3" style="display:none";>
It must be this way:
<div id="myDIV_Filter1_3" style="display:none;">
The "show-hide" logic on your javascript function myFunction has an error because your div id="myDIV_Filter1_1" contains the other 2 div you have on your code example so you can't hide this particular div because you will lose the "show" or "hide" of the other 2 divs. That way it'll never show you the other 3 rows you wanted to see. I fixed all the errors you can check the code on my snippet here:
function myFunction() {
var x_filter1_1 = document.getElementById("myDIV_Filter1_1");
var x_filter1_2 = document.getElementById("myDIV_Filter1_2");
var x_filter1_3 = document.getElementById("myDIV_Filter1_3");
if (x_filter1_3.style.display === "none") {
x_filter1_1.style.display = "block";
x_filter1_2.style.display = "none";
x_filter1_3.style.display = "block";
} else {
x_filter1_1.style.display = "block";
x_filter1_2.style.display = "block";
x_filter1_3.style.display = "none";
}
}
<?php
$query_brands = mysql_query("SELECT distinct pd_filter1 from tbl_brands2 WHERE pd_code in (select pd_code from tbl_product where cat_id='2')") or die(mysql_error());
$count_brands = mysql_num_rows($query_brands);
if($count_brands > 0) {
while($fetch_brands = mysql_fetch_array($query_brands)) {
$record_brands[] = $fetch_brands;
}
}
$i_brands=0;
foreach($record_brands as $records_brands) {
?>
<table border="1" width="215" style="border-collapse: collapse; border-spacing: 0;" bgcolor="#eeeff0">
<tr>
<td>
<?php
$i_brands = $i_brands + 1;
if ($i_brands > 3)
{
?>
<div id="myDIV_Filter1_1">
<?php
}
}
?>
<div id="myDIV_Filter1_2" >
<span class="class22">
show more...
</span>
</div>
<div id="myDIV_Filter1_3" style="display:none">
<span class="class22">
show less...
</span>
</div>
</td>
</tr>
</table>
Hope it helps!

Related

Modal inside a loop

I'm new to php and I have this problem.
First I did display the inputed message using select and while loop in table. Then I want a functionality that can reply by getting the name or id of the selected row by clicking a button that shows a modal.
The problem is only the first row are showing the modal. The rest of the rows aren't displayed. I've already searched for the answers and found out that modal is using the same id to display.
I'm still a student and sorry for the messy codes.
`
INBOX
while (($row = mysqli_fetch_array($query))) {
$idd = $row['id'];
$content = $row['content'];
$content_msg = $row['content_msg'];
$to_msg = $row['to_msg'];
$date_created = $row['date_created'];
$from_msg = $row['from_msg'];
$query1 = mysqli_query($db, "SELECT profile_pic FROM users WHERE
first_name = '$from_msg' ")
or die (mysqli_error($db));
while ($rows = mysqli_fetch_array($query1)) {
$pic = $rows['profile_pic'];
echo "
<table id = 'tbl_inbox' border = '0'>
<tr>
<td rowspan = '9' width = '90px'>
<center> <img src = '$pic' width = '80px' height = '80px' id =
'inbox_pic'> </center>
</td>
<td class = 'td_name' colspan = '4'>
<b>$from_msg</b>
</td>
<td id = 'td_date'>
<b>$date_created</b>
</td>
</tr>
<tr>
<td rowspan = '3' colspan = '5' id = 'td_msg'>
$content_msg
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<!-- This is the looped button
<td colspan = '5' id = 'td_action'> <button id ='reply$idd' name =
'btn' value = 'reply$idd'> $idd </button> </td>
</tr>
</table>
";
//
?>
<div id="myModal<?php echo $idd; ?>" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>This is modal ID <?php echo $idd; ?></h2>
</div>
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
<script>
// Get the modal
var modal<?php echo $idd; ?> = document.getElementById('myModal<?
php echo $idd; ?>');
// Get the button that opens the modal
var btn<?php echo $idd; ?> = document.getElementById('reply<?php
echo $idd; ?>');
// When the user clicks the button, open the modal
btn<?php echo $idd; ?>.onclick = function() {
modal<?php echo $idd; ?>.style.display = "block";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal<?php echo $idd; ?>) {
modal<?php echo $idd; ?>.style.display = "none";
}
}
</script>
<?php
}}
?>
<script>
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
`
First things first.. Why are you creating a <table> everytime it runs the while loop?
But I think that the problem that your having is because of the way you try to toggle a Modal..
Every item is having one single Modal Toggle function but it's better to make a generic function like:
function toggleModal(id) {
document.queryselector(`#${id}`).classList.toggle('modal-active');
}
Then make a button like:
<button onclick="() => toggleModal(testModal1)">TEST</button>
To explain what this exactly does:
The only thing you're actually doing is toggling a class with display: unset so that it get's showed. So if it's already inside it's classes it will get removed from it, otherwise it gets added to the classes.
If this is not the case, try looking inside your console (F12) and watch for errors when pressing one of the not working buttons

jQuery PHP Chat user is typing feature

Im use jquery for display someone is typeing.
Its ok everything work but i want everyone can see that and i cant think about something any help ?
var textarea = $('#input');
var typingStatus = $('#typing_on');
var lastTypedTime = new Date(0); // it's 01/01/1970
var typingDelayMillis = 1500; // how long user can "think about his spelling" before we show "No one is typing -blank space." message
function refreshTypingStatus() {
if (!textarea.is(':focus') || textarea.val() == '' || new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
typingStatus.html('');
} else {
typingStatus.html('<div id="new"><div class="comment" style="display:inline;"><div class="user_name"> <b><font color="white"><?php echo $pernome?> <br> <center> <b style="font-size:9px;"> <?php echo $datenow?> </b> </center></font></b></div></div> <div class="comment" style="display:inline; position:relative; font-size:14px; bottom:6px; color:#ffffff; margin-bottom:2px;">מקליד...</div></div>');
}
}
function updateLastTypedTime() {
lastTypedTime = new Date();
}
setInterval(refreshTypingStatus, 100);
textarea.keypress(updateLastTypedTime);
textarea.blur(refreshTypingStatus);
Thanks!!

hide and show content in loop php jquery

Hi guys i need some help i have these code i need to show content by id
after clik to show content i need to show content by the same id
<script type="text/javascript">
function toggleAndChangeText() {
$('#divToToggle').toggle();
if ($('#divToToggle').css('display') == 'none') {
$('#aTag').html('Collapsed text mode &#9658');
}
else {
$('#aTag').html('Expanded text mode &#9660');
}
}
</script>
<style>
#divToToggle{display:none;}
</style>
and this code php/html
<?php
$stmt = $DB_con->prepare("SELECT * FROM `topic` ORDER BY id");
$stmt->execute();
foreach ($stmt->fetchAll() as $row) {
echo"
<div class='Post'>
<div class='rgt Pimg'><a href='post.php?id=".$row['id']."'><img src='".$row['e_title']."' class='Pimg'/></a></div>
<div>
<a id='aTag' href='javascript:toggleAndChangeText()'>
Show Content
</a>
<div id='divToToggle'>".$row['e_content']."</div>
</div>
";
}
?>
You cannot use same ID's for multiple elements. Use class instead:
Change your script to :
$(document).ready(function(){
$("a.aTag").on("click", function(){
var toggleElement = $(this).closest("div").find(".divToToggle");
toggleElement.toggle();
if (toggleElement.css('display') == 'none') {
$(this).html('Collapsed text mode &#9658');
}
else {
$(this).html('Expanded text mode &#9660');
}
});
});
And your PHP code to this:
<?php
$stmt = $DB_con->prepare("SELECT * FROM `topic` ORDER BY id");
$stmt->execute();
foreach ($stmt->fetchAll() as $row) {
echo"
<div class='Post'>
<div class='rgt Pimg'><a href='post.php?id=".$row['id']."'><img src='".$row['e_title']."' class='Pimg'/></a></div>
<div>
<a class='aTag' href='javascript:toggleAndChangeText()'>
Show Content
</a>
<div class='divToToggle'>".$row['e_content']."</div>
</div>
";
}
?>

Fetch new MySQL entries and append to existing div's dynamically

I have a PHP script to selected records for comments from a database, and then print them onto the page. What I'd like to happen is that if a user is on a page, and another user comments on an item on said page, it automatically appends the new comment to the bottom. The problem I'm having is that I don't know how to differentiate between all of the statuses.
My code for generating the comments on a status is:
<?php
$rt = ("SELECT * FROM (SELECT comment as comment, byuid as byuid, comuid as comuid, likes as likes, dislikes as dislikes, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_comments WHERE onuid = '$sid' AND type = 'status' ORDER BY timestamp DESC LIMIT 2) mingle_comments ORDER BY timestamp ASC"); //query
$result = mysql_query($rt) or die (mysql_error());
if(mysql_num_rows($result) >= 2) {
?>
<div id="sa" style="background:#E0E0E0; padding:5px 5px 5px 5px;">
View all comments...
</div>
<?php
}
while($st = mysql_fetch_assoc($result)) {
$comment = nl2br($st['comment']);
$by = $st['byuid'];
$comuid = $st['comuid'];
$time = $st['timestamp'];
$l = $st['likes'];
$d = $st['dislikes'];
$bq = "SELECT * FROM users WHERE uid = '$by' LIMIT 1";
$bqq = mysql_query($bq) or die (mysql_error());
while($bqr = mysql_fetch_assoc($bqq)) {
$dp = $bqr['dp'];
$fbq = $bqr['fname'];
$sbq = $bqr['sname'];
}
?>
<div id="commentbox" class="<?php echo $comuid; ?>" style="padding:5px 5px 5px 5px;">
<div id="cbi" style=" display:inline; position:relative; ">
<img src="<?php if ($dp == null) { echo 'img/unknown_user.jpg'; } else { echo "pf/" . $by . "/" . $dp; } ?>" width="36px" style=" display:inline; position:relative;"/>
</div>
<div id="cbt" style="position:relative; margin-left:32px; margin-top:-35px;">
<?php echo $fbq . " " . $sbq; ?>
<p class="<?php echo $comuid; ?>" style="position:relative; margin-left:5px;"><?php echo $comment; ?></p>
</div>
<div id="clb" style="background:#E0E0E0; padding-left:5px;">
Like<?php echo time_since($time); ?>
</div>
</div>
<?php
}
?>
TL:DR; How can I automatically fetch new comments and append them to the comments generated in the above script without page refresh.
JQUERY
$(document).ready(function(){
var lastcheck='';
setInterval(function() {
$.ajax({
url: 'URL_TO_PHP_SCRIPT',
type: 'POST',
data: {'lastcheck':lastcheck},
success: function(result){
if (result!='nothing_new'){
lastcheck=result.lastcheck /*Update lastcheck*/
var data=result.data;
$.each(data, function(i,val){
//Foreach comment you do what you need, like append, prepend, etc. data[i]."key" to get the value.
});
}
}
});
}, 15000 /* This will check each 15 seconds, you can change it */);
});
PHP SIDE
$lastcheck=$_POST['lastcheck'];
/*
You should add a date field to each new created comment,then filter by "orderby date > $lastcheck" so you get comments since your last check
You get the new comments here
...
...
..
*/
if (!empty($arrayofnewcomments)){
/*The output*/
echo json_encode(array('lastcheck'=>date('Y-m-d H:i:s'),'data'=>$arrayofnewcomments));
}else{/*No new comments*/
echo 'nothing_new';
}
This is some code I came up with, it's a general Idea but it works. It will check each 15 seconds for new comments.

Jquery delete function

I'm trying to make Jquery delete function, here is the code, that I wrote -
$("a.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
}
else {
return false;
}
});
Okay now to problem, when I press, button a with class delete, it correctly, shows up the dialog (and the id is correct), but after I press yes, it will delete it with php, without jquery fadeOut effect.
My php code -
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
Not sure where is the problem but I think it's inside $(target+"ul"), since I'm not sure, if it will add the target value and ul together.
In additional, how can I prevent the delete, if I press no? Currently, if I press no, it will still delete the category.
If you need any other information - ask.
Full code -
public function showCategories() {
if(isset($_SESSION['logged_in']) && isset($_SESSION['user_level']) && $_SESSION['user_level'] == 'admin') {
$sql = "SELECT * FROM `categories`";
$q = mysql_query($sql);
if(mysql_num_rows($q) > 0) {
?>
<div class="recentorders" style="display: none;" id="categoryBox">
<h5 class="colr">Categories</h5>
<div class="account_table" style="width: 688px;">
<ul class="headtable" style="width: 680px;">
<li class="order">ID</li>
<li class="action" style="width: 430px;">Category Name</li>
<li class="action nobordr">Options</li>
</ul>
<?php
while($row = mysql_fetch_array($q)) {
?>
<ul class="contable" style="width: 680px;" id="cat<?php echo $row['id']; ?>ul">
<li class="order"><?php echo $row['id']; ?></li>
<li class="action" style="width: 430px;"><?php echo $row['name']; ?></li>
<li class="action nobordr">Edit<a class="delete" id="cat<?php echo $row['id']; ?>" href="?action=delete&id=<?php echo $row['id']; ?>#">Delete</a></li>
</ul>
<?php
}
?>
</div>
</div>
<?php
}
else {
// No categories created, please create one first.
}
}
else {
header('location: account.php');
}
}
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
If you're searching by ID you need to prefix your selector with a "#":
100% working sample on jsFiddle:
http://jsfiddle.net/E7QfY/
Change
$(target+"ul").delay(3000).fadeOut(200);
to
$('#' + target+"ul").delay(3000).fadeOut(200);
OR
$(this).closest('ul').delay(3000).fadeOut(200);
I would also modify the code to this:
$("a.delete").click(function(event){
if(confirm('Do you really want to delete this category?') == true){
$(this).closest('ul').delay(3000).fadeOut(200);
window.location.href = $(this).attr('href'); //OR use AJAX and do an event.preventDefault();
}
else
event.preventDefault();
});
It's because you are using an anchor tag that is causing a postback to the server which makes your page refresh before you see the animation. Make sure your anchor tag has an href defined as "#" to stop the postback from happening.
You should use something else than A tag e.g. span or div. And then after clicking this element make your fadeout and after it redirect to the PHP script that deletes your object:
$("SPAN,DIV.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
window.location.href = URL TO PHP FILE
}
else {
return false;
}
});

Categories