sub menu link is not working

This Code worked for me

while ( odrin_have_rows('read_the_book_chapters',  $post_id) ) : the_row(); if (odrin_get_sub_field('sub')){
$output .= '<li><ul>'; }
$output .= '<li><a href="#item'. $item_num = 1 . '">' . odrin_get_sub_field('title',  $post_id) .'</a></li>';if (odrin_get_sub_field('sub')){
$output .= '</ul></li>';}$item_num++; endwhile;

I was missing <li> of sub menu. Meaning that <ul> of sub menu should be within <li> of parent.

Here is the structure of sub menu

<ul>
    <li>Parent Item
        <ul><li>Sub Menu Item</li></ul>
    </li>
    <li>Another Parent Item</li>
</ul>