sql - MySQL if pagename then run statment -
hi wondering if help me out mysql. i'm building blog , have 2 tables articles , categories , i'm joining these tables, want display categories if click categories link. i'm passing categories_id in link can't figure out how display categories if page link blog.php?=category_id. i'll have categories_id number in url cant figure out how display category, know sql statement display categories can't figure out how run statement if url contains the ?=category_id , not run original sql statement displaying articles date. tried if , else conditions depending on page name wasn't working. hope makes sense , help much appreciated, in advance,
mitchell layzell
heres code code
$url = $_server['script_name']; $pos = strrpos($url,"/"); $pagename = substr($url,$pos+1); if($pagename == ("blog.php")) { $sql = "select article_id, title, body, date, categories.category, author articles left bring together categories on articles.category = categories.category_id order article_id desc limit 4"; } elseif($pagename == ("blog.php?=1")) { $sql = "select article_id, title, body, date, categories.category, author articles left bring together categories on articles.category = categories.category_id category_id = 1"; } $result = query($sql); if($result===false) { echo "query failed"; } else { while( $data = mysqli_fetch_array($result)) { ?> <article> <h3 class="title-medium"><?php echo $data['title']; ?></h3> <p class="caption-medium"><?php echo $data['author']; ?> <?php echo $data['date']; ?> <?php echo $data['category']; ?></p> <img src="img/blog-post-1.jpg" alt=""> <p><?php echo substr($data['body'],0,450)." ..." ?></p> <a href="blog-post.php?id=<?php echo $data['article_id']; ?>"><p class="caption-medium highlight">read more</p></a> <hr> </article> <?php } } ?>
code
as you've been told in comments, have utilize parameter, not whole request.
connect_to_db(); $where = ''; if (isset($_get['cat_id'])) { $where = "where category_id = ".intval($_get['cat_id']); } $sql = "select article_id, title, body, date, categories.category, author articles left bring together categories on articles.category = categories.category_id $where order article_id desc limit 4"; $result = query($sql);
mysql sql if-statement mysqli
No comments:
Post a Comment