php - Query inside query loop with both query outputs required -


on website, want make navigation bar this, categories (here 2012) , articles (here may - october). i've got mysql-database categories (infocat_id, name) , 1 articles (title, body, category, infopost_id).

i thought code php follows:

<ul>    <?php     $query1 = $db->prepare("select infocat_id, name info_category");     $query1->execute();     $query1->bind_result($infocat_id, $name);     while($query1->fetch()):    ?>    <li>       <?php echo $name; ?>       <ul>          <?php           $query2 = $db->prepare("select title, infopost_id info_posts category = $infocat_id");           $query2->execute();           $query2->bind_result($title, $infopost_id);           while($query2->fetch()):          ?>          <li><?php echo $title; ?></li>          <?php endwhile; ?>       </ul>    </li>    <?php endwhile; ?> </ul> 

sadly, wouldn't work got first category name , underneath "fatal error: call member function execute() on non-object in [the directory in php-file located] on line [the line says $query2->execute();].

i figured couldn't use join, because need output of first query.

i'm not super experienced php, if able me, appreciated.

you can use join, , check change in key decide whether output title again.

crudely knocked , not tested this:-

<ul>    <?php    $prev_infocat_id = 0;     $query1 = $db->prepare("select infocat_id, name, title, infopost_id                 info_category                 left outer join info_posts                 on info_category.infocat_id = info_posts.category");     $query1->execute();     $query1->bind_result($infocat_id, $name, $title, $infopost_id);     while($query1->fetch())     {         if ($prev_infocat_id == 0)         {             echo '<li>';         }         else         {             if ($prev_infocat_id != $infocat_id)             {                 echo '</li><li>';             }         }         $prev_infocat_id = $infocat_id;         echo '<li>'.$title.'</li>';     }      if ($prev_infocat_id != 0)     {         echo '</li>';     }    ?> </ul> 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -