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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:36:55+00:00 2026-05-23T15:36:55+00:00

I am trying to rewrite this messy code, so that I only make one

  • 0

I am trying to rewrite this messy code, so that I only make one database query and eliminate the for loop. I am hoping the code will be faster with one query instead of two.

The loop exists for one reason: The date attribute, which is either “Today” or “Tomorrow” followed by the formatted date.

The main point here is that I want to keep the data structure (dayReport) the same. I want to know what date the result set belongs to (either “Today” or “Tomorrow”).

It seems silly to have a loop for that reason alone.

So here is the code. It’s in PHP, but really this is a language agnostic question:

for ($a=1; $a<=2; $a++)
{
    $b = $a - 1;
    $result = mysql_query("SELECT
                            name,
                            time,
                            date_format(time,'%M %d %Y %h:%i %p') as ftime,
                            date_format(time,'%l:%i %p') as ttime,
                            fee
                            FROM `foo_bar`
                            WHERE `cityId` = $cityId
                            AND time_utc > utc_timestamp()
                            AND time >= DATE_ADD(curdate(),INTERVAL $b day)
                            AND time < DATE_ADD(curdate(),INTERVAL $a day)
                            ORDER BY time ASC
                            " ) or die(mysql_error());


  if ($result && mysql_num_rows($result) > 0)
  {
    $day = new Day();
    $day->date = $a == 1 ? 'Today' . date(' - l, F d') : 'Tomorrow' . date(' - l, F d',strtotime('+'.$b.' day'));
    $dayStuff = array();
    while ($row = mysql_fetch_object($result))
    {
        $dayStuff[] = $row;
    }
    $day->foo = $dayStuff;
    $dayReport[] = $day;
  }
}
  • 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-23T15:36:56+00:00Added an answer on May 23, 2026 at 3:36 pm

    You could rewrite your query in this way, plain SQL no php code

    SELECT
        name, time,
        date_format(time,'%M %d %Y %h:%i %p') as ftime,
        date_format(time,'%l:%i %p') as ttime,
        fee,
        `time` = CURDATE() AS is_today -- Note this flag
        FROM `foo_bar`
        WHERE `cityId` = $cityId
        AND time_utc > utc_timestamp()
        AND time BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 1 day) -- rewritten clause for clarity
        ORDER BY time ASC
    

    Then you could completely remove the outer php for statement and use the is_today flag to distinguish between today and tomorrow records.

    For what regards the Day structure you can build two of them before the inner while and fill id according to the is_today flag, something like

    while ($row = mysql_fetch_object($result))
    {
        if ($row['is_today']) {
            $todayStuff[] = $row;   
        } else {
            $tomorrowStuff[] = $row;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to rewrite some query strings I have in my URL like this
I am trying to write this rewrite code and it just is not working
I'm trying to rewrite this url: http://www.example.com/user.php?user=username into http://example.com/username I'm using this code in
i am trying to rewrite any URL that match this pattern: ~/Ahmed ~/Name to
I am trying to rewrite this code without the or operator. if((/^\s*field\s+{\s*$/)||(/^\s*field\s+{\s*\/\/.*$/) { #
I'm trying to rewrite this as a for loop; my attempt below failed. Any
I'm trying to rewrite a url of this: http://www.foo.com/bar/baz to index.php?q=$1&d=baz Where bar is
I am trying to rewrite an url with GET-data from a form. This works
I am trying to exclude a directory with ISAPI-Rewrite (note: this is a windows/iis
Ok, this is driving me nuts... I'm trying to rewrite my urls like this:

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.