Jump to content

selecting highest id of an array


dachshund

Recommended Posts

hi,

 

i'm trying to select the highest id in an array, so that if it's the highest one a reply button is displayed.

 

my code so far is

 


<?php
$query = mysql_query("SELECT MAX(`id`) FROM `comments` WHERE `replyto` LIKE '$commentid' AND `moderate` LIKE '0'");		
$maxRow= mysql_fetch_array($query);
$max = $maxRow['id'];		
if($id == $max)	{
?>

<span class="comment_reply">
<a id="<?php echo $commentrows['id']; ?>" />
Reply
</a>
</span>

<?php		
}
?>

 

any help would be great!

Link to comment
Share on other sites

thanks jesirose. i tried those things but it has made no difference.

 

when i echo out $max it is blank.

 

Are you getting errors? The only ways I can suspect you are getting an empty value without getting errors is:

  1) The id value in all the matching records is empty

  2) there are no matching records.

 

I suspect #2 is your problem. Always echo your queries out to inspect them before running them. And when encountering such issues just add some debugging logic to output information to help you determine the problem.

$query = "SELECT MAX(`id`)
          FROM `comments`
          WHERE `replyto` = '$commentid'
            AND `moderate` = '0'";
$result = mysql_query($query);
if(!$result)
{
    echo "QUERY FAILED:<br>Query: $query<br>Error: " . mysql_error();
}
else
{
    $max = mysql_result($result, 0);
    //Begin Debugging logic
    $query2 = "SELECT COUNT(`id`)
              FROM `comments`
              WHERE `replyto` = '$commentid'
                AND `moderate` = '0'"
     $result = mysql_query($query2) or die(mysql_error());
     $count = mysql_result($result, 0);
     echo "$query<br>The above query matched $count records";
     //End debugging logic
}

if($id == $max)
{
    echo "<span class='comment_reply'>\n";
    echo "<a id='{$commentrows['id']}'' />Reply</a>\n";
    echo "</span>\n";
}

Link to comment
Share on other sites

well, it's able to find that there is one matching query. it's just not realising that it's the maximum id in the query.

 

the only other parts of the code that are relevant are '$commentid' and $commentrows['id'], both of which work and echo out perfecty.

 

thanks

Link to comment
Share on other sites

here we go:

 

<?php
$commentsql="SELECT * FROM comments WHERE articleid LIKE $id AND moderate LIKE 0 AND replyto LIKE 0 ORDER BY id ASC";
$commentresult=mysql_query($commentsql) or die (mysql_error());
while($commentrows=mysql_fetch_array($commentresult)){

$date = date('d F y', strtotime($commentrows['datetime']));
$sql="SELECT * FROM content WHERE id='$id'";
$result = mysql_query($sql) or die (mysql_error());
$rows=mysql_fetch_array($result);
$articleid=$rows['id'];
$replyto = $commentrows['id'];

if (strlen($http) > 3) {
$http = substr($http, 0, 7);
}			
?>

<script type="text/javascript">


$(function(){
$('#<?php echo $commentrows['id']; ?>').click(function(){ 
	$("#randomdiv<?php echo $commentrows['id']; ?>").load('http://www.website.co.uk/reply_comment.php', {'id' : '<?php echo $commentrows['id'];?>'})
});
});

$(function(){
$('#reply<?php echo $commentrows['id']; ?>').click(function(){ 
	$("#randomdiv<?php echo $commentrows['id']; ?>").load('http://www.website.co.uk/reply_comment.php', {'id' : '<?php echo $commentrows['id'];?>'})
});
});

</script>

<div id="posted_comments">

<ul>
    
    	<li class="comment_text">
        
        <? echo $commentrows['comment']; ?>
        
        </li>
            
    	<li class="username">

        <?php echo $commentrows['name']; ?> 
        
        <span class="comment_date">
        <?php

	echo 'Posted ';
	$dateNew = strtotime($commentrows['datetime']);
	if (timePassed($dateNew) == '')	{
	echo 'just now';
	}else	{
	echo timePassed($dateNew);
	}
	if (timePassed($dateNew) == 'yesterday' OR timePassed($dateNew) == '')	{
	}else	{
	echo ' ago';
	}

	echo ' ';
	?>
        </span>
	     
        <span class="comment_reply">
        <a id="<?php echo $commentrows['id']; ?>" />
        Reply
        </a>
        </span>
        
        <div class="clear"></div>
        
	</li>
    
    </ul>

</div>

<?php
$commentid = $commentrows['id'];
$replycommentsql="SELECT * FROM comments WHERE replyto LIKE $commentid AND moderate LIKE 0 ORDER BY id ASC";
$replycommentresult=mysql_query($replycommentsql) or die (mysql_error());
while($replycommentrows=mysql_fetch_array($replycommentresult)){
?>

<div id="reply_comments_wrap">

<div id="posted_reply_comments">

<ul>
    
    	<li class="comment_text">
        
        <? echo $replycommentrows['comment']; ?>
        
        </li>
            
    	<li class="username">

        <?php echo $replycommentrows['name']; ?> 
        
        <span class="comment_date">
        <?php

	echo 'Posted ';
	$dateNew = strtotime($replycommentrows['datetime']);
	if (timePassed($dateNew) == '')	{
	echo 'just now';
	}else	{
	echo timePassed($dateNew);
	}
	if (timePassed($dateNew) == 'yesterday' OR timePassed($dateNew) == '')	{
	}else	{
	echo ' ago';
	}

	echo ' ';
	?>
        </span>
        
        <?php
        $query = mysql_query("SELECT MAX[`id`] FROM `comments` WHERE `replyto` LIKE '$commentid' AND `moderate` LIKE '0' ORDER BY id ASC");
	$maxRow = mysql_fetch_array($query);
	$max = $maxRow['id'];
	if($replycommentrows['id'] == $max)	{
	?>

	<span class="comment_reply">
        <a id="reply<?php echo $commentrows['id']; ?>" />
        Reply
        </a>
	</span>

	<?php		
	}
	?>
        
        <div class="clear"></div>
        
	</li>
    
    </ul>

</div>

<div class="clear"></div>

</div>

<?php
}
?>

<div id="randomdiv<?php echo $commentrows['id']; ?>"></div>

<?php

}

mysql_close();

?>

<script type="text/javascript">
    
function validateForm()
{
var x=document.forms["comment"]["n"].value
if (x==null || x=="" || x=="Your name...")
{
$('#comment_wrap').prepend($('<div class="comment_wrong">Please fill in all fields.</div>').fadeIn('slow').delay(1000).fadeOut('slow'));
return false;
}
$('#comment_wrap').prepend($('<div class="comment_answer">Thank you! Your comment will be approved shortly.</div>').fadeIn('slow').delay(1000).fadeOut('slow'));
$('#comment_container input[type=text]').attr('value', 'Your name...');
$('#comment_container textarea').attr('value', 'Your comment...');
}

// wait for the DOM to be loaded 
$(document).ready(function() {
// bind 'commenter' and provide a simple callback function 
    $('#commenter').ajaxForm( { beforeSubmit: validateForm } );
});
        
</script>


<div id="comment_wrap">

<div id="comment_container">

<ul>

        <form name="comment" method="post" id="commenter" action="http://www.website.co.uk/comment_posted.php?id=<?php echo $id; ?>">
        
        <li>
        
        <input type="text" placeholder="Your name..." class="text_name_email" name="n">
        
        </li>
        
        <li>
        
        <textarea class="text_area" name="c" placeholder="Your thoughts..."></textarea>
        
        </li>
        
        <li>
        
        <input type="submit" value="Post" class="post_comment">
        
        </li>
        
        </form>
        
        </li>
    
    
   	</ul>

</div>

</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.