I'm trying to replace the content of comments div with the new comments by loading it again and replacing the old comments. When i change the content of the div, it replaces the first comment only and repeat the whole comment:
here is the comments div :
<?php
$comment_qry = mysqli_query($conn, "SELECT * FROM comments WHERE post_id='$post_row[id]' ORDER BY id DESC");
while($comment_row = mysqli_fetch_array($comment_qry)){
?>
<div id="comments_div">
<div style="background:#aaaaaa; margin:5px;">
<p onclick="report_var(<?php echo $post_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">report</p>
<p>user: <?php echo $comment_row['user_id']; ?></p>
<p>date: <?php echo date("Y-m-d", $comment_row['date']); ?></p>
<p>content: <?php echo $comment_row['content']; ?></p>
<?php if($post_row['user_id'] == $my_id or $comment_row['user_id'] == $my_id or $admin == 1){ ?>
<p><span onclick="comment_remove(<?php echo $comment_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">delete</span></p>
<?php } ?>
</div>
</div>
<?php } ?>
ajax code :
/////////////////////////comment function
function comment(post_id, poster_id)
{
loadXMLDoc("php/comment.php?post_id="+post_id+" content="+document.getElementById("content").value+"&poster_id="+poster_id,function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("comments_div").innerHTML = xmlhttp.responseText;
}
});
}
comment.php :
<?php
$comment_qry = mysqli_query($conn, "SELECT * FROM comments WHERE post_id='$_GET[post_id]' ORDER BY id DESC");
while($comment_row = mysqli_fetch_array($comment_qry)){
?>
<div style="background:#aaaaaa; margin:5px;" class="comment">
<p onclick="report_var(<?php echo $post_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">report</p>
<p>user: <?php echo $comment_row['user_id']; ?></p>
<p>date: <?php echo date("Y-m-d", $comment_row['date']); ?></p>
<p>content: <?php echo $comment_row['content']; ?></p>
<?php if($_GET['poster_id'] == $my_id or $comment_row['user_id'] == $my_id or $admin == 1){ ?>
<p><span onclick="comment_remove(<?php echo $comment_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">delete</span></p>
<?php } ?>
</div>
<?php } ?>
Your first code snippet is creating a separate comments_div for each comment. Then your JavaScript code snippet is just replacing the content of the first one.
You can fix it by putting the comments_div outside the the PHP while loop, and then using the same HTML output code inside that loop as you are in your third example (a div.comment per comment).
So, something like this in place of that first code snippet:
<div id="comments_div">
<?php
$comment_qry = mysqli_query($conn, "SELECT * FROM comments WHERE post_id='$post_row[id]' ORDER BY id DESC");
while($comment_row = mysqli_fetch_array($comment_qry)){
?>
<div style="background:#aaaaaa; margin:5px;" class="comment">
<p onclick="report_var(<?php echo $post_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">report</p>
<p>user: <?php echo $comment_row['user_id']; ?></p>
<p>date: <?php echo date("Y-m-d", $comment_row['date']); ?></p>
<p>content: <?php echo $comment_row['content']; ?></p>
<?php if($_GET['poster_id'] == $my_id or $comment_row['user_id'] == $my_id or $admin == 1){ ?>
<p><span onclick="comment_remove(<?php echo $comment_row['id']; ?>, <?php echo $comment_row['user_id']; ?>);">delete</span></p>
<?php } ?>
</div>
<?php } ?>
</div>
Related
I'm trying to give each ID in the loop a unique ID.
Like: check-1, check-2 etcera.
But it is not working .
Here is my code:
<?php
if( have_rows('activiteiten_knoppen') ): $count=0;?>
<?php while(have_rows('activiteiten_knoppen') ): the_row();
$meer_info = get_sub_field('meer_info');
$tijd = get_sub_field('reserveren');
?>
<p class="activiteitInfo"><a class="fancybox-inline" style="color: #f3f3f3 !important;">Meer info</a></p>
<p class="activiteitReserveer"><a style="color: #f3f3f3 !important;" href="<?php echo $tijd; ?>">Reserveren</a></p>
<div id="check-<?php echo $count ?>" style="display: none;"><?php echo $meer_info; ?></div>
<script>
$(document).ready(function () {
$(".activiteitInfo").click(function () {
$(".hoi").css("display", "block");
});
});
</script>
<?php $count++; ?>
<?php endwhile; ?>
<?php endif; ?>
hello here i have a problem with load more ajax all is okey but when there is no
data to show from database show me this error: http://i.stack.imgur.com/6158Y.png
index.php
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div>
</div>
main.js
// Load more post
function show_more_posts(post_id)
{
var ID = post_id;
$.ajax({
type: "POST",
url: "ajax/ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("#show_here").append(html);
$("#hide_here").remove();
}
});
return false;
}
ajax_more.php
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
</div>
You can use like this. then you will not get error.
<?php
if(isset($msg_id)) {
?>
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
</div>
<?php
}
?>
In your ajax_more.php file you have to set $msg_id to $lastmsg before the while:
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
$msg_id = $lastmsg;
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
so, even if your query doesn't return data (no new messages), the variable have the correct value.
If, instead, you expect that at some point, there aren't more posts to load, you have to use the solution provided also from Kaja Mydeen:
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<?php if(isset($msg_id)) { ?>
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
<?php } ?>
Also, your code have somethin strange: you continue to add a div with and id="show_here".
I think before fetch data through while loop you should have check for total affected rows like this
$totrows = mysql_num_rows($result);
if($totrows > 0){
//your while loop for fetch data
}else{
//return something else
}
you just need to do like this
<div id="show_here">
<?php
if(isset($post['id'])) {
?>
<div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div>
<?php
}else{
?>
<div id="hide_here">No More Posts to show !!!!</div>
<?php
}
?>
</div>
Try this, it may help you
I created a page named queries.php
<?php
require("connection.inc.php");
$query = "SELECT SUM(john), SUM(robert), COUNT(school) FROM election";
$result = mysql_db_query($db, $query, $connection) or
die("query error!");
$data = mysql_fetch_array($result);
$john = number_format($data[0], 0,',','.');
$robert = number_format($data[1], 0,',','.');
$school = $data[2];
$total = $john + $robert;
$remaining_school = 524 - $school;
$percentage_john = round(($john*100/$total),2);
$percentage_robert = round(($robert*100/$total),2);
?>
Then I display the data in display.php
The data displayed well and nothing error, I tried a couple of tricks to auto refresh the container using jquery but failed.
Here is my display.php page:
<?php
include("queries.php");
?>
<div id="totaljohn"> // <--- the div id taken from css layout
<h9>John: <?php echo $john; ?> <br> (<?php echo $percentage_john; ?> %)</h9>
</div>
<div id="totalrobert">
<h9>Robert: <?php echo $robert; ?><br> (<?php echo $percentage_robert; ?> %)</h9>
</div>
<div id="totalboth">
<h4>Total : <?php echo $total; ?> Suara</h4>
</div>
<div id="totalschool">
<h4>Total School attending : <?php echo $school; ?> Schools</h4>
</div>
<div id="remaining_schools">
<h4>Remaining Schools: <?php echo $remaining_school; ?> of 524 schools</h4>
</div>
Everything looks good. Is your database connected? Is your SQL correct? Are there records in the database returned for your query?
I've been trying to get JQuery to check a radio for me when I assign a value 'Z' to it.
My current code:
<?php if ($option['type'] == 'radio') { ?>
<div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
<label class="control-label"><?php echo $option['name']; ?></label>
<div id="input-option<?php echo $option['product_option_id']; ?>">
<?php if (isset($option_values)) foreach ($option_values as $option_value) { ?>
<div class="radio">
<label>
<input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" />
<?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
<?php if ($option_value['price_prefix'] == 'z') {?>
<script>
$(".radio").prop("checked", true)
</script>
<?php echo 'Im Active!!'; ?><?php }
else { ?> <?php echo ' ';?> (<?php echo $option_value['price_prefix'];?>
<?php echo $option_value['price']; ?>) <?PHP }?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
<?php } ?>
As for now, my option 'Z' only shows 'I'm Active!!' in the list. The radio is still unchecked though.
What am I doing wrong?
I'm planning on releasing a free OpenCart module where people can activate an option by standard by assigning the value Z in product properties.
Thanks!
I have 2 paragraphs coming from the database it work there is no issues with it. But I have added a button that when I click it will alert the ID of this current div which the button is present.
Now this is my table:
+--------+--------------------+
| id | paragraph |
|--------+--------------------+
| 1 | this is text 1 |
| 2 | another text |
+-----------------------------+
My PHP and HTML code:
<?php
$query = mysql_query(SELECT * FROM tests);
while($row = mysql_fetch_assoc($query)){
$post_id = $row['id'];
$text = $row['paragraph'];
?>
<div id='text' class='<?php echo "$post_id";?>'>
<p><?php echo "$text";?></p>
<input type='button' value='Show id' id='show'>
</div>
<?php } ?>
And now probably 2 paragraphs are generated but when I click at show button it will show only the 1 id even if I click at the second one:
This is my JQuery code:
$('#show').click(function(){
var ids = $("#text").attr('class');
alert(ids);
});
id="" attribute values are supposed to be unique. do the other way around:
<div id="<?php echo $post_id; ?>" class='text'>
<p><?php echo $text; ?></p>
<input type='button' value='Show id' class='button'>
</div>
jquery
$('.button').click(function(){
var ids = $(this).parent('div').attr('id');
alert(ids);
});
Note: Stop using mysql_ functions, instead use mysqli_* or PDO
Sample:
<?php
$con = new mysqli('localhost', 'username', 'password', 'database');
$result = mysqli_query($con, 'SELECT * FROM tests');
while($row = $result->fetch_assoc()) {
$post_id = $row['id'];
$text = $row['paragraph'];
?>
<div id="post_<?php echo $post_id; ?>">
<p><?php echo $text; ?></p>
<button type="button" class="button">Show</button>
</div>
<?php } ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$('.button').click(function(){
var ids = $(this).parent('div').attr('id');
alert(ids);
});
</script>
Very important point from Jeroen:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Reference
this is hard to read
<?php
$query = mysql_query(SELECT * FROM tests);
while($row = mysql_fetch_assoc($query)){
$post_id = $row['id'];
$text = $row['paragraph'];
?>
<div id='text' class='<?php echo "$post_id";?>'>
<p><?php echo "$text";?></p>
<input type='button' value='Show id' id='show'>
</div>
<?php } ?>
Try something like this
<?php
$query = mysql_query('SELECT * FROM tests');
while(false !== ( $row = mysql_fetch_assoc($query))):
?>
<div class='text' id='post-<?php echo $row['id']; ?>'>
<p><?php echo $row['paragraph']; ?></p>
<input type='button' value='Show id' id='show'>
</div>
<?php endwhile; ?>
I don't see how the query worked without quotes. Just to explain you can use the alternative syntax for the while, so instead of
<?php while(. .. ){ ?>
<html></html>
<?php } ?>
do
<?php while(... ): ?>
<html></html>
<?php endwhile; ?>
When you have a lot of html ( this is a small example ) you can easily find the diffrence between endif; endwhile; or endforeach; then } } or }
Why don't you just pass the ID as a parameter?
<?php
$query = mysql_query("SELECT * FROM tests");
while($row = mysql_fetch_assoc($query)){
$post_id = $row['id'];
$text = $row['paragraph'];
?>
<div id='text' class='<?php echo "$post_id";?>'>
<p><?php echo "$text";?></p>
<input type='button' value='Show id' id='show' onclick="showId(<?php echo $post_id; ?>);">
</div>
<?php } ?>
Then your script could look like this:
function showId(theId){
alert(theId);
}
where it says:
class='<?php echo "$post_id";?>'
when you're echoing a php variable, you dont put quotes around it:
class='<?php echo $post_id;?>'.