dachshund Posted July 13, 2012 Share Posted July 13, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/ Share on other sites More sharing options...
ManiacDan Posted July 13, 2012 Share Posted July 13, 2012 ...and what's the problem here? Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361293 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 the maximum id is not being selected Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361296 Share on other sites More sharing options...
Jessica Posted July 13, 2012 Share Posted July 13, 2012 1. Use =, not LIKE here. 2. Echo the query first to make sure it looks the way you expect. 3. Check for mysql errors. mysql_query has plenty of examples. 4. Switch to mysqli Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361300 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 thanks jesirose. i tried those things but it has made no difference. when i echo out $max it is blank. Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361303 Share on other sites More sharing options...
Psycho Posted July 13, 2012 Share Posted July 13, 2012 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361313 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 thanks psycho, that's useful. i tried that code and it said "the above record matches one record", but still wouldn't echo the reply button. Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361317 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2012 Share Posted July 13, 2012 There's obviously more code that's relevant to this problem. You should post it also. Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361320 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361321 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2012 Share Posted July 13, 2012 So you want people to blindly guess what the rest of the code does and what control structures, etc. may be affecting the outcome? Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361322 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361323 Share on other sites More sharing options...
dachshund Posted July 13, 2012 Author Share Posted July 13, 2012 worked it out it is : $query = mysql_query("SELECT MAX(`id`) AS `id` FROM `comments` WHERE `replyto` LIKE '$commentid' AND `moderate` LIKE '0' ORDER BY id ASC"); Quote Link to comment https://forums.phpfreaks.com/topic/265618-selecting-highest-id-of-an-array/#findComment-1361335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.