Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:19:51+00:00 2026-06-08T18:19:51+00:00

Is there a simpler way for me to code this and perform the same

  • 0

Is there a simpler way for me to code this and perform the same thing without having to have so many queries? Im trying to add pagination (not included in code here) and it works on my ALL query, but the AND / OR queries give funny results and is becoming a headache. Other than that the filtering of results works perfect just need pagination.

$filter = isset($_POST['filter']) ? $_POST['filter'] : null;
$status = isset($_POST['status']) ? $_POST['status'] : null;

if(empty($filter) && empty($status)) {

    //echo 'ALL query';
    $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id';
    $sth = $link->prepare($sql);
    $sth->execute(array());
    $result = $sth->fetchall();

} else {
    // display result if filter AND status are selected
    if(!empty($filter) && !empty($status)) {

        //echo 'AND query';
        $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id WHERE module_type = :filter AND product_status = :status';
        $sth = $link->prepare($sql);
        $sth->execute(array(':filter' => $filter, ':status' => $status));
        $result = $sth->fetchall();

    } else {
        // display result if filter OR status are selected
        if(!empty($filter) || !empty($status)) {

            //echo 'OR query';
            $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id WHERE module_type = :filter OR product_status = :status';
            $sth = $link->prepare($sql);
            $sth->execute(array(':filter' => $filter, ':status' => $status));
            $result = $sth->fetchall();

        }
    }
}

//test sql
echo $sql.'<br />';

$bgcolor = '';
foreach($result as $key => $value) {

    if(($bgcolor=='#ffffff') ? $bgcolor='#f1f1f1' : $bgcolor='#ffffff') {

    echo '<tr bgcolor="'.$bgcolor.'">';
    echo '<td>'.$value['product_id'].'</td>';
    echo '<td>'.$value['product_name'].'</td>';
    echo '<td>'.$value['product_type'].'</td>';
    echo '<td>'.$value['module_name'].'</td>';
    echo '<td>'.$value['product_price'].'</td>';
    echo '<td>'.$value['product_status'].'</td>';
    echo '</tr>';
    }
}
    echo '</table>';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:19:52+00:00Added an answer on June 8, 2026 at 6:19 pm

    And My Second thought:

    $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id WHERE';
    if(!empty($filter) || !empty($status)){
        $sql .= ' module_type = :filter '.(!empty($filter) && !empty($status) ? 'AND' : 'OR').' product_status = :status';
        $sth = $link->prepare($sql);
        $sth->bindValue(':filter', $filter);
        $sth->bindValue(':status', $status);
    }else{ $sth = $link->prepare($sql); }
    $result = $sth->execute();
    

    It needs testing and as truth said you might not need the OR but that is upto exactly how your form and filtering works tbh, but I am gonna leave it at that it should give you pointers to move to the next stage.

    Edit again:

    Couldn’t help myself, here is a third possibility:

    $params = array();
    $where = array();
    
    if(!empty($filter)){
        $params[':filter'] = $filter;
        $where[] = 'module_type = :filter';
    }
    if(!empty($status)){
        $params[':status'] = $status;
        $where[] = 'product_status = :status';
    }
    
    $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id'.(sizeof($where) > 0 ? ' WHERE '.implode(' AND ', $where) : '');
    
    $sth = $link->prepare($sql);
    foreach($params as $k=>$v) $sth->bindValue($k, $v);
    $result = $sth->execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simpler way of implement this? Or a implemented method in JDK
Is there a simple way of putting source code into a repository and then
Is there a simpler way to rewrite the following condition in JavaScript? if ((x
Is there a simpler way to get a string of a parent directory than
Given two lists of equal length, is there a simpler or preferred way to
Is there really no simple way to perform two-way data binding on properties of
Similar to this question , I am trying to perform simple authentication to a
I'm parallelizing some back-end code and trying not to break interfaces. We have several
I have Java-related question: I want to know is there a way to create
Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.