How to increment div id in a php loop

I have a page where Fire Safety Training Components are dynamically generated from a php foreach loop. I also give the option to the user to comment on the training component, and administrator can respond to the comment. In essence creating a discussion for a particular Fire Safety training component. I am still working on localhost to develop the site. What I would like to accomplish is insert the conversation in a jquery accordion underneath the training component so the user has the choice to see the discussion or not.

the problem that I am having is I can only make the first result work. I figured out this was because the div id needs to be unique for each loop. I was wondering if someone can help me increment the javascript function and div result?

here is the code I use to produce the data:

$(function() {
	
    $( "#Accordion2" ).accordion({
        active: false,
        collapsible: true,
    });
});
</script>
</head>
<body>

  <div class="gridContainer clearfix">
    <nav id="nav"> <?php include ("includes/nav_employee.php");?> </nav>
    <div id="main">
        <div class="pageTitle">
           <?php 
	 include ("includes/title.php");
	  ?>
         </div pageTitle>
         <hr>
         <?php 
        if(Session::exists('FS')){
           echo '<div class="message"><span class="flashT">MESSAGE:</span> <br /><span class="flash">' . Session::flash('FS') . '</span></div><br />';
         }
		 if(Session::exists('FSnoAss')){
           echo '<div class="message"><span class="flashT">MESSAGE:</span> <br /><span class="flash">' . Session::flash('FSnoAss') . '</span></div><br />';
         }
		 if(Session::exists('response')){
           echo '<div class="message"><span class="flashT">MESSAGE:</span> <br /><span class="flash">' . Session::flash('response') . '</span></div><br />';
         }
		if(Session::exists('home')){
		   echo '<div class="message"><span class="flashT">MESSAGE:</span> <br /><span class="flash">' . Session::flash('home') . '</span></div><br />';
		 }
		if(Session::exists('CommentDeleted')){
		   echo '<div class="message"><span class="flashT">MESSAGE:</span> <br /><span class="flash">' . Session::flash('CommentDeleted') . '</span></div><br />';
		 }
		 
          ?>
         <div class="stretch">
         <h3 class="output">
         The following is a collection of Fire Safety Training Component that were written by the Fire/Safety Director of a particular Site Location.  Every Associates working at this particular site should make themselves familiar with the content of this page.
         </h3><br /> Each Site requires a Fire/Safety Director.  Please send us your request if you would like to receive  "Administrator priviledges." Administrator can produce fire safety reports and conduct fire drills and write site specific fire safety training component. Send an email here with your request to receive <a href="contact.php">Admin priviledges.</a>  <br /><br /></h3>
         </div>
     
	 
	 <?php  
		//pulls out all current component in a list
		if(isset($_GET['id'])){
			echo'';
		}else{
			
		echo '<div class="output"><h3>Already existing Fire Safety Training Components.</h3><p style="text:small;">Click to navigate to the desired Fire Safety Component</p>';
		 $currentComponents = DB::getInstance()->query("SELECT * FROM fscomponents WHERE siteId=$siteId ORDER BY ComponentTitle ASC");
		 if($currentComponents->count()){
			 echo'<ul>';
			 foreach($currentComponents->results() as $cc){
				 echo '<li class="li"><a href="#'.$cc->componentTitle.'">'.$cc->componentTitle.'</a></li>';
			 }
				 echo '</ul>';
		  }
			 echo '</div>';//outputHeading
		}
	?>    
	<?php  
	if(isset($_GET['id'])){
		$id=$_GET['id'];
//this check to see if the request was made to lookup one component and returns the one component that was selected in a nother page
		$FSComponents = DB::getInstance()->get('fscomponents', array( 'id', '=', $id));
	}else{
//this return all components
      $FSComponents = DB::getInstance()->query("SELECT * FROM fscomponents WHERE siteId=$siteId ORDER BY ComponentTitle ASC");
	}
      foreach($FSComponents->results() as $rows){
         
              echo '<br /><div><a name="'.$rows->componentTitle.'"><span class="outputHeading">'.$rows->componentTitle.'</span>&nbsp;</a></div>';
              echo '<div class="wrapper">'; ///////////
			  echo '<div class="titleNav">
			  			<ul>';
                          
						   	if($user->data()->groupId <2){
								echo'';	
							}else{
							echo'<li class="li"> <a href="firesafetycomponents_update.php?id=' . escape($rows->id) . '">Update Component</a></li>';
							}
                            echo '<li class="li"><a  href="firesafetycomponent_comment.php?id=' . escape($rows->id) . '">Add a Comment</a></li>';
                            echo '<li class="li"><a  href="#top">Top of the page</a></li>
							
                        </ul>
                   </div>';
				  
              echo '<div class="stretchColorC">'. $rows->FSComponent. '</div><br />'; 
                echo'<div id="Accordion2">';
					  echo '<h3>Discussion</h3>';
					  echo '<div>';
//comment start          	
              $comments = DB::getInstance()->query("SELECT * FROM fscomponentcomments WHERE FSComponentId = $rows->id");
              if($comments->count())
			 
                    foreach($comments->results() as $comment){
						
						$date= date ("F d, Y",strtotime($comment->FSCCCreatedDate));
                        
						echo '<div class="compoComment"><div class="componentTitle">';
                        
                        $createdBy= DB::getInstance()->query("SELECT id,firstname, lastname FROM associates WHERE id=$comment->FSCCCreatedBy");
                        if($createdBy->count()){
							
                            foreach($createdBy->results() as $name){
                                
								echo '<a href="associate_details.php?user=' .escape($name->id).'">'.$name->firstname .'&nbsp;' . $name->lastname.'</a> Commented on '.$date;
								if(!$user->isLoggedIn() || $user->data()->groupId <2){
									echo '</div>';
								}else{
								echo '&nbsp;<a  href="firesafetycomponent_comment_response.php?id=' . escape($comment->id) . '">Respond inside this tread</a>&nbsp;' ;	
								echo '&nbsp;<a  href="firesafetycomponent_comment_delete.php?id=' . escape($comment->id) . '">Delete this comment</a><br /></div><br />' ;	
								}
								//menu starts here for response
								 
			  				
							echo '<div>';//div to wrap comment and question together
                                echo '<div class="componentComment">'.$comment->FSCComment.'</div></div>';
//response start///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

								$responses = DB::getInstance()->query("SELECT * FROM fsccresponses WHERE FSCCommentId = $comment->id");
								if($responses->count()){
									foreach($responses->results() as $response){
										$resDate = date ("F d, Y",strtotime($response->FSCCRDate));
										
										echo '<div><div class="componentResponse">';
										
										$resCreatedBy = DB::getInstance()->query("SELECT id,firstname, lastname FROM associates WHERE id=$response->createdBy");
										if($resCreatedBy->count()){
											 foreach($resCreatedBy->results() as $resName){
                                				echo '<a href="associate_details.php?user=' .escape($resName->id).'">'.$resName->firstname .'&nbsp;' . $resName->lastname.'</a> Responded on '.$resDate.'<br /></div><br />' ;
												echo '<div class="componentResComment">'.$response->FSCCResponse.'</div></div>';

											 }//foreach $resCreatedBy
										}//if resCreatedBy

                                		
									}//foreach $responses
								}//if $responses
														

								echo'</div>';//div to wrap comment and response together
								 

                            }
							
                        }
               		 }
					 
             echo '</div><br />';//wrapper
			 echo '</div>';//accordion
						echo '</div>';//accordion
       }
	   
        ?>

What do you mean by “first result”?

I’m going to guess it’s because you terminate div’s inside the loop without opening new ones. But do please inform us better what you mean by only getting the first result to work.

By first result I mean the script is working but only for the first result of the loop. the rest does not get inserted inside an accordion. if you look at the picture on top you can see an accordion being generated but the second record below simply shows the “Discussion” without it being inserted inside an accordion

you are referring to this block:

foreach($createdBy->results() as $name){

						echo '<a href="associate_details.php?user=' .escape($name->id).'">'.$name->firstname .'&nbsp;' . $name->lastname.'</a> Commented on '.$date;
						if(!$user->isLoggedIn() || $user->data()->groupId <2){
							echo '</div>';
						}else{
						echo '&nbsp;<a  href="firesafetycomponent_comment_response.php?id=' . escape($comment->id) . '">Respond inside this tread</a>&nbsp;' ;	
						echo '&nbsp;<a  href="firesafetycomponent_comment_delete.php?id=' . escape($comment->id) . '">Delete this comment</a><br /></div><br />' ;	
						}

the reason why I am closing this div tag is because if the user doesn’t have administrator privileges I close the div but if they do have admin proviledge I extend te div further with two ‘a’ tags that can respond to the comment left by a user, or delete the comment. everyone is allowed to comment but only admins have the privilege to respond or delete comments. this closing div does not have any effect on the loop result.

First impulse: You’re missing your open brace on the comments count, so that’s doing nothing.

Second impulse: Why are you closing the ‘accordion’ div twice according to your last 3 lines of code? I count too many </div>'s…

If the above dont do anything, run the script, copy and paste the source code of the result, and lets see what it’s doing wrong.

ok I have corrected pretty much all errors on the page. the only thing I am getting an error on is duplicating the ID of the accordion(which was my first question on this post.)

I know there is a way to increment the ID using JS but I unfortunately don’t have much experience in JS script.
Also, I am using the “name” in the “a” tag attribute in the result of the loop so I can navigate to the particular result from a list that is created on top of the page(navigate directly to a particular Fire Safety component) I see that I am also getting an error on this? HOw else can I identify the result? I can’t use an ID as suggested because I am going to end up with the same problem with multiple same IDs?

ok I figured it out. I replaced the ID= with a class=.

this made possible to echo accordions in each result from the loop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.