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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:23:59+00:00 2026-05-27T08:23:59+00:00

We have a list of dates that is drawn from a MySQL database. That

  • 0

We have a list of dates that is drawn from a MySQL database. That part is pretty straight forward and works just fine, do a $query, step through the array, find those that meet conditions, echo some html code and the date itself.

The issue we are hoping to remedy is that this list appears in three separate places on the webpage. Each time it appears, the php code is run, database query made and results echo’d.

What I am wondering is if there is a way to create a php variable at the beginning and then simply echo that variable in all three spots, rather than running the code three times.

I have tried the noob ideas of simply using:

$date = ( ...all the php query / echo code... );
and
$date = { ...all the php query / echo code... };

As well as permutations there of. But to no avail.

  • 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-27T08:23:59+00:00Added an answer on May 27, 2026 at 8:23 am

    Yes, you can store it in a variable at the top of your php file:

    // query ...
    // but don't echo, store in a string
    $date = 'this contains what is supposed to be echoed later on.';
    

    Then later on:

    echo $date;
    

    And again elsewhere:

    echo $date;
    

    Regarding your comment:

    $date = ''; // this will hold all dates
    while($tourtime = fetch_result_from_db(...)){ // 'the loop', whatever yours look like
        // instead of echoing, append to the string $date
        $date .= "<span class=\"dates\">". date ("j F Y", $tourtime) . "</span><br />";
    }
    

    .= appends (concatenates) strings.


    Bonus: If you have lots of data, it might be better w.r.t. efficiency to collect all rows first and then join them into a string in a single operation. You can do this like this:

    $rows = array();
    while($tourtime = fetch_result_from_db(...)){
        $rows[] = "<span class=\"dates\">". date ("j F Y", $tourtime) . "</span><br />";
    }
    $date = implode($rows);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of dates in a table in a MySQL database (the
I have a list of dates that a machine has worked on, but it
I have created a list view that displays the names and dates of items
I have a list of dates stored in MySQL using PHP. These were stored
I have a long list of dates that i want to parse, they are
I have a list of Dates ( or DateMidnight from joda time) i rappresent
I have an ordered (i.e. sorted) list that contains dates sorted (as datetime objects)
Have a list of dates in excel in the format (this comes originally from
I have a list of dd/mm/yyyy dates stored in a hash that I need
I have a list of scheduled dates that we toggle the visibility of. They

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.