php - on click change questions displayed -
i have page has list of items. on bottom of page "view more" button. when clicks button, page needs add more items. var $displayedquestions, , page coded right refresh when "view more" button clicked, we'd have live. how can done?
here code:
<?php include "db_connect.php"; db_connect(); function tags($tags) { $tagarray=explode(",",$tags); $i=0; $finished='false'; while($finished=='false') { if (empty($tagarray[$i])=='true') { $finished='true'; } else { $taglist = $taglist . '<a class="commontagnames" href="">' . $tagarray[$i] . '</a> '; $i++; } } return $taglist; } function formattime($timesince) { $strsince=number_format($timesince,0,'',''); $nodecimals=intval($strsince); if ($nodecimals<1){ return "less minute ago"; } elseif ($nodecimals>=1&&$nodecimals<60) { return $nodecimals . " min ago"; } elseif ($nodecimals>60&&$nodecimals<1440){ $hourssince=$nodecimals/60; $hoursnodecimals=number_format($hourssince,0); return $hoursnodecimals . " hours ago"; } elseif ($nodecimals>1440){ $dayssince=$nodecimals/1440; $daysnodecimals=number_format($dayssince,0); return $daysnodecimals . " days ago"; } } $submitbutton=$_request['viewmore']; $numquestions=intval($_request['questions']); if($numquestions!=0) { $displayedquestions=$numquestions; } else { $displayedquestions=10; } $sql="select * `questions` order `questions`.`id` desc limit 0, " . $displayedquestions; $questions=mysql_query($sql); while($row = mysql_fetch_array($questions)) { $id = $row['id']; $user = $row['userasking']; $question = $row['question']; $tags = $row['tags']; $timestamp = $row['timestamp']; $time=strtotime($timestamp); $secondssince=(date(u)-$time)/60; $timesince=formattime($secondssince); $responses=mysql_query("select * `answerstoquestions` `idofquestion`= '$id'"); $comments=mysql_num_rows($responses); $likes=mysql_query("select * `likesofquestions` `idofquestion`= '$id'"); $numlikes=mysql_num_rows($likes); $userprofileq = mysql_query("select `id`,`avatar` `users` `username` = '$user'"); $userprofileresult = mysql_fetch_row($userprofileq); $linktoprofile = $userprofileresult[0]; $avatar = $userprofileresult[1]; $taglist=tags($tags); echo "</li>"; echo '<li class="questionslist" onclick="showuser(' . $id . ')"> <div id="questionpadding"> <img class="askerimage" width=50 height=50 src="../images/userimages/' . $avatar . '.png"/> <div class="questionfirstrow"><h1 class="questiontitle">' . $question . '</h1></div> <span class="midrow"> <span class="askerspan"><a class="askername" href="">'. $user .'</a></span> </span> <span class="bottomrow"> <img src="../images/comment.png"/> <span class="comments">' . $comments . '</span> <img src="../images/likes.png"/> <span class="likes">' . $numlikes . '</span> ' . $timesince . ' </span> </div> </li>'; } ?> <center><a href="index.php?questions=<?php echo $displayedquestions+10; ?>"><img class="morequestions" src="../images/viewmorebar.png" alt="more" /></a></center>
without doing lot of work can add ajax this. use function:
first, (i assuming including code above file) create container around it. ex:
<div id='container'>...</div> second, add javascript page includes code have above:
<script type="text/javascript"> $(function(){ $("#container img.morequestions").parent().live('click', (function (e) { e.preventdefault(); $("#container").load($(this).attr("href")); }); }); }); </script> this load #container script have without refreshing rest of page.
note selector more link (slash button) in example $("#container img.morequestions").parent() because don't have class or id on it. should give class or id more link , use selector.
Comments
Post a Comment