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 3665562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:46:33+00:00 2026-05-19T01:46:33+00:00

I want some advice on structuring some (basic) php code. I need to display

  • 0

I want some advice on structuring some (basic) php code.

I need to display data from a table in different places. The rows that need to be displayed in each section can be identified by a flag.

I’m not sure if the best way to do this is to go to the database once and seperate the data into 2 variables as I loop through it or if I should go to the database twice, using sql to call only the data I need each time.

For those who want to see it in code:

Method A:

// Create and execute a MySQL query
function tasks_not_done(){
        // Open a PDO dtabase connection
        $link = new PDO(DB_INFO, DB_USER, DB_PASS);

        $sql = " SELECT title, created_date
                FROM todos
                WHERE list_id = ? AND checked = '0'
                ORDER BY created_date DESC";
        $stmt = $link->prepare($sql);
        $stmt->execute(array($_REQUEST['list_id']));

        // loop throught all the rows
        while($row = $stmt->fetch()) {
            $date = strtotime($row['created_date']);
            $date = date('d/m/y' , $date);
            echo '<div class="task">' . "\n";
            echo '<span class="taskcdtate">' .$date . '</span>'. '<span class="tasktitle"> ' . $row['title'] . ' </span>' . "\n";
            echo '</div>';

            }
       $stmt->closeCursor();
}

Method B

function tasks_all(){
       // Open a PDO dtabase connection
       $link = new PDO(DB_INFO, DB_USER, DB_PASS);

       $sql = " SELECT title, created_date, checked
                FROM todos
                WHERE list_id = ?
                ORDER BY created_date DESC";
        $stmt = $link->prepare($sql);
        $stmt->execute(array($_REQUEST['list_id']));

        // loop throught all the rows
        $tasks['not_done'] = "";
        $tasks['done'] = "";

        while($row = $stmt->fetch("FETCH_ASSOC")) {
            $date = strtotime($row['created_date']);
            $date = date('d/m/y' , $date);

            if($row['checked'] =='0'){
                $tasks['not_done'] .= '<div class="task">' . "\n";
                $tasks['not_done'] .='<span class="taskcdtate">' .$date . '</span>'. '<span class="tasktitle"> ' . $row['title'] . ' </span>' . "\n";
                $tasks['not_done'] .='</div>';
                } elseif ($row['checked'] =='1') {
                $tasks['done'] .= '<div class="task">' . "\n" .
                $tasks['done'] .='<span class="taskcdtate">' .$date . '</span>'. '<span class="tasktitle"> ' . $row['title'] . ' </span>' . "\n";
                $tasks['done'] .='</div>';
                }

            }
       $stmt->closeCursor();
       return $tasks;
}

thanks

  • 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-05-19T01:46:34+00:00Added an answer on May 19, 2026 at 1:46 am

    There is no right answer to this kind of question, but I can give you some pointers.

    My general rule is: get the data you need when you need it. But there are a lot of things that could affect this.

    1. Will you always use data from both queries ? if yes, you should get all in one query. If Never, meaning they exclude each other, the answer is no.
    2. If you always need query 1, and sometimes query, you have to make a few more considerations. Does query 1 take much more time if you include query 2? How often do you need query 2, almost always or almost never.
    3. What happens if someone else updates data at the same time. Let’s say you get both at once, and someone else updates data that affects query 2. This means you’re using outdated data. If you fetch the data when you need it, you might get correct updated data.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice. I have a web page and want to extend it's
please I need some advice about design approaches in iPhone programming. I want to
Need some advice from you experts out there. I've just started with Android programming
need some advice on this one as im struggling abit and cannot figure it
I need some advice please on the best way to achieve a particular outcome...
I've recently been writing some basic command-line programs (I want to keep my skills
All, We could really do with some advice from SVG gurus. WHAT WE HAVE:
I'm a Jr. Engineer hoping to seek some advice from all of the experienced
I'm in the process of porting some code from Linux to Mac OS X.
I want some slightly 'dynamic' XML source files. I want some of the element

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.