0

I'm building a blog and have two tables articles and categories and I'm joining these tables, but what I want to do is display just the categories if I click a categories link. I'm passing the categories_id in the link but I can't figure out how to display just the categories if the page link is like blog.php?=category_id. I'll have the categories_id number in the URL I just can't figure out how to display just that category, I know the SQL statement to just display the categories but what I can't figure out is how to run that statement if the URL contains the the?=category_id and not run my original SQL statement that was displaying all the articles by date. I tried to do if and else conditions depending on the page name but wasn't working.

<?php
    connect_to_db();
    
    $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 FROM articles
            LEFT JOIN categories ON articles.category = categories.category_id ORDER BY article_id DESC LIMIT 4";
    }
    elseif($pagename == ("blog.php?=1")) {
    $sql = "SELECT article_id, title, body, date, categories.category, author FROM articles
            LEFT JOIN categories ON articles.category = categories.category_id WHERE 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
    }
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
i092173
  • 53
  • 9
  • valid URL for a desired category can end with `blog.php?cart_id=1` and you can access `Category_ID` using `$_GET['cat_id']` or `$_REQUEST['cat_id']`. Use that in `if-else` block of PHP. – जलजनक Feb 10 '13 at 06:46
  • 1
    Please read what to do [when you get answers](http://stackoverflow.com/faq#howtoask). – जलजनक Feb 10 '13 at 06:59
  • Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Jul 24 '20 at 22:18

2 Answers2

0

Using cat_id in query string like :

blog.php?cart_id=1

You can extract it from $_GET[] or $_REQUEST[]

if (isset($_GET['cat_id']))
    $category_id = intval($_GET['cat_id']);
else
    $category_id = 0;

if($category_id > 0) { //Non-Zero +ve value
    $sql = "SELECT article_id, title, body, date, categories.category, author
            FROM articles LEFT JOIN categories ON articles.category = categories.category_id
            WHERE category_id = $category_id";
} else { //Zero
    $sql = "SELECT article_id, title, body, date, categories.category, author
            FROM articles LEFT JOIN categories ON articles.category = categories.category_id
            ORDER BY article_id DESC LIMIT 4";
}
जलजनक
  • 3,072
  • 2
  • 24
  • 30
0

As you've been told in the comments, you have to use only parameter, not the 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 
        FROM articles LEFT JOIN categories 
        ON articles.category = categories.category_id 
        $where ORDER BY article_id DESC LIMIT 4";

$result = query($sql);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thank you very much I appricate you taking the time to help me, that makes a lot of sense thanks again! – i092173 Feb 10 '13 at 19:07