php - (logical) Breadcrumbs for posts. I have the category_id. Is it enough to show the category name? -


for posts pages have lot of things database post id, titles, contents , category id. try create breadcrumb of posts pages , cannot show category name acheave this:

home -> world -> happened somewhere in world.

"home" easy. can print post title $post['title'] don't have clue how put in breadcrumb category name. if have category_id there "magical" way category name. category names in table. or have store category names in records of posts.

thank you!

my tables:

categories

category_id                   int(11)             category_name                 varchar(90)                 category_description          text 

posts

post_id                    int(11)            post_title                 varchar(255)                           post_published_date        datetime current_timestamp         post_content               text                       category_id                int(11) 

you don't have store category name each post; can category name in categories table. i'm little confused @ question seems rather easy...

say post information in $post:

$dbh = new pdo(...); $sql = $dbh->prepare( 'select category_name categories category_id = ?' ); $sql->execute( array($post['id']) ); $result = $sql->fetch(pdo::fetch_assoc); if($result && isset($result['category_name'])) {      $category_name = $result['category_name']; } else { /* handle error... */ } 

if don't have many categories , don't change much, consider storing category ids , corresponding names elsewhere, such don't have query database each time serve post.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -

jquery - Keeping Kendo Datepicker in min/max range -