Welcome Guest, Not a member yet? Register   Sign In
HTML malformed
#1

Hello again  Smile ,

I am using jsTree plugin to show some data, the code is working but HTML is malformed.

Can someone help me to correct this code?
The array looks like this:
Code:
$users = array(
     array(
       'department' => 'Business',
       'fullname' => 'Leo Rodriguez'
     ),
     array(
       'department' => 'Computer Science',
       'fullname' => 'Harry Jones'        
     ),
     array(
       'department' => 'Computer Science',
       'fullname' => 'Sabi'        
     ),
     array(
       'department' => 'IT',
       'fullname' => 'Tim Ben'        
     ),  
     array(
       'department' => 'Marketing',
       'fullname' => 'John Doe'        
     ),        
   );

Code:
<div class="container  well" id="tree">
   <?php
     echo '<ul>';
       $offset = '';
       foreach($users as $user) :
         if($user['department'] != $offset) {
           echo '<li>' . $user['department'];
           $offset = $user['department'];
         }
         echo '<ul><li>' . $user['fullname'] . '</li>';
         echo '</ul>';
       endforeach;
     echo '</ul>';
   ?>
</div>
Quote: <ul>
 <li>Business
   
<ul>
     <li>Leo Rodriguez</li>
   </ul>
   <li>Computer Science
     
<ul>
       <li>Harry Jones</li>
     </ul> // this should technically not be here
     
<ul>  // this should technically not be here
       
<li>Sabi</li>
     </ul>
     <li>IT
       
<ul>
         <li>Tim Ben</li>
       </ul>
    // I am missing a closing li here
       
<li>Marketing
         
<ul>
           <li>John Doe</li>
         </ul>
   // I am missing a closing li here
</ul>

I visualize the problem but I don't know where to put the missing parts in the code abow. (Tried some things but then the nodes with more children don't show up) 
Any help is very appreciated!
Reply
#2

Your last echo in the foreach should be like this:

PHP Code:
<div class="container  well" id="tree">
 
  <?php 
     
echo '<ul>';
 
      $offset '';
 
      foreach($users as $user) : 
 
        if($user['department'] != $offset) {
 
          echo '<li>' $user['department']; 
 
          $offset $user['department'];
 
        }
 
        echo '<ul><li>' $user['fullname'] . '</li>';
 
        echo '</ul>';
 
        echo '</li>';
 
      endforeach;
 
    echo '</ul>';
 
  ?>
</div> 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks for the answer, 
I have tried that but then the second node name ('Sabi') of the CS department does not show up because the HTML looks like this:
Quote:<ul>
    <li>Business
        <ul>
            <li>Leo Rodriguez</li>
        </ul>
    </li>
    <li>Computer Science
        <ul>
            <li>Harry Jones</li>
        </ul>
    </li>
    <ul>
        <li>Sabi</li>
    </ul>
    </li>
    <li>IT
        <ul>
            <li>Tim Ben</li>
        </ul>
    </li>
    <li>Marketing
        <ul>
            <li>John Doe</li>
        </ul>
    </li>
</ul>
when in fact it should be like this: 
Quote:<ul>
    <li>Business
        <ul>
            <li>Leo Rodriguez</li>
        </ul>
    </li>
    <li>Computer Science
        <ul>
            <li>Harry Jones</li>
            <li>Sabi</li>
        </ul>
    </li>
    </li>
    <li>IT
        <ul>
            <li>Tim Ben</li>
        </ul>
    </li>
    <li>Marketing
        <ul>
            <li>John Doe</li>
        </ul>
    </li>
</ul>
Reply
#4

(This post was last modified: 12-31-2017, 04:54 AM by Wouter60.)

Code for your view:

PHP Code:
<html>
<
body>

<
ul>
<?
php $offset '';?>

<?php for($x=0;$x<count($users);$x++) : ?>
    
    <?php
    
if ($offset != $users[$x]['department']) {
        if (
$x 0) echo '</ul></li>';
        echo 
'<li>' $users[$x]['department'];
        echo 
'<ul>';
        
$offset $users[$x]['department'];
    }
    
?>
    <li><?= $users[$x]['fullname'];?></li>    
    
<?php endfor; ?>
    </ul></li>
</ul>


</body>
</html> 
Reply
#5

Another solution :

PHP Code:
<div class="container well" id="tree">
    <
ul>
        <?
php foreach ($users as $key => $user) : ?>
        <?php if ($key == || $users[$key]['department'] != $users[$key 1]['department']) : ?>
            <li>
                <?php echo $user['department']; ?>
                <ul>
        <?php endif; ?>
        <li><?php echo $user['fullname']; ?></li>
        <?php if (count($users) - == $key || $users[$key]['department'] != $users[$key 1]['department']) : ?>
                </ul>
            </li>
        <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</div> 
Reply
#6
Thumbs Up 

Great, thank you very much Wouter60 and fenzy. 

Cheers!
Reply
#7

You can use this code.


<ul>
  <?php foreach ($users as $key => $value) { ?>
      <?php if($users[$key]['department'] != $users[$key-1]['department']) { ?>
           <li><?php echo $users[$key]['department']; ?></li>
           <ul>
             <li><?php echo $users[$key]['fullname']; ?></li>
           </ul>
      <?php } else { ?>
 
    <ul>  
       <li>Sabi</li>
     </ul>
  <?php } ?>
  <?php } ?>
</ul>
Reply
#8

(12-31-2017, 11:45 PM)XtreemDeveloper Wrote: You can use this code.


<ul>
  <?php foreach ($users as $key => $value) { ?>
      <?php if($users[$key]['department'] != $users[$key-1]['department']) { ?>
           <li><?php echo $users[$key]['department']; ?></li>
           <ul>
             <li><?php echo $users[$key]['fullname']; ?></li>
           </ul>
      <?php } else { ?>
 
    <ul>  
       <li>Sabi</li>
     </ul>
  <?php } ?>
  <?php } ?>
</ul>
Reply
#9

(12-31-2017, 11:45 PM)XtreemDeveloper Wrote:   <?php foreach ($users as $key => $value) { ?>
      <?php if($users[$key]['department'] != $users[$key-1]['department']) { ?>
 (...)
  <?php } ?>
  <?php } ?>
</ul>

No, you can't. The $key-1 will throw an error at the first iteration of the foreach loop, because $key-1 doesn't exist at that point.
Besides that, in your code, every department with more than one user, will have one or more users named "Sabi".
Reply




Theme © iAndrew 2016 - Forum software by © MyBB