Jump to content

Showing more/less text in loop


graham23s

Recommended Posts

Hi Guys,

 

in my code i while loop out users blog posts, and have added a show/hide button for the text, but when im looping the blogs from mysql only the original show/hide button is working

 

code:

 

<script type="text/javascript">
function toggleBlock(id, linkid) {
if (document.getElementById(id).style.display == 'none') {
document.getElementById(id).style.display = 'block';
document.getElementById(linkid).innerHTML = '[HIDE BLOG]'
} else {
document.getElementById(id).style.display = 'none';
document.getElementById(linkid).innerHTML = '[sHOW BLOG]'
}
}
</script>
<?php
#################################################
# viewblog.php
#################################################
  ## id
  $usersid = $_GET['id'];
  
  $query = "SELECT * FROM `usersblog` WHERE `bloggerid`='$usersid'";
  $result = mysql_query($query);
  $blogcount = mysql_num_rows($result);
  
  ## no videos to display
  if($blogcount == 0) {
     ## echo a back link
     echo ("<div id=\"\" align=\"left\">[<a href=\"profile.php?id=$usersid\" class=\"foot_links\">Back To Users Profile</a>]</div>");
     stderr("Error","This user has no blogs started yet.");
     include("includes/footer.php");
     exit;   
  }
  
  ## display the blogs
  while($row = mysql_fetch_array($result)) {
  
  ## vars
  $blogsubject = $row['blogsubject'];
  $blogtext = $row['blogtext'];
  $blogdate = $row['date'];
  
  echo ('<table class="sendmsgbox" width="500" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0">');
  echo ('<tr>');
  echo ('<td colspan="2" class="header_boxes" align="left"><span class="colhead">Posted On: '.$blogdate.'</span> <a class="al" href="javascript:void(0)" onClick="toggleBlock(\'below\', \'linkid\');" id="linkid">[sHOW]</a></td>');
  echo ('</tr>');
  echo ('<tr>');
  echo ('<td colspan="2" align="center"><div id="below" style="display:none; ">'.$blogtext.'</div></td>');
  echo ('</tr>');
  echo ('</table><br />');
  
  } // end while loop
?>

 

any ideas on what could be wrong?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/81805-showing-moreless-text-in-loop/
Share on other sites

Yeah, ids are meant to be unique. All of yours are "below". You could easy fix that by adding an integer to then end of the below string like this

 

$int = 0;
while($row = mysql_fetch_array($result)) {
$id = 'below:' . $int++;
... onClick="toggleBlock(\''. $id .'\'

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.