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

  • SEARCH
  • Home
  • 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 6041953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:44:11+00:00 2026-05-23T06:44:11+00:00

I’m working on a PHP-written html calendar that writes each day of the month

  • 0

I’m working on a PHP-written html calendar that writes each day of the month inside a while loop.

Each day of the month is inside a pair of tags and certain days need a title attribute in the tag. Those days will be obtained from the MySQL db and stored in an array like this: $array($day_number=>$quantity).

An example array might be: (3=>5, 12=>4, 15=>6, 22=>10, 27=>2, 31=>4). The array would only contain keys for days where the quantity is not zero.

I can think of two ways to do this. I’m wondering which one is more efficient, or if there is another, best way.

Option 1.) Check the array at each iteration of the html-creating loop to see if it contains the corresponding day. If so, add the appropriate title attribute to the tag. So I would be using something like:

$day = 1;
while($day < 31) {
if(array_key_exists($day,$array)) {
echo "<td title=\"$array['$day'] spots available\">$day</td>";
}
else echo "<td title\"No spots available for this date\">$day</td>";
$day++;
}

2.) Use loop to put entire calendar into a string variable, then use string functions to insert special title attributes.

$day = 1;
while($day < 31) {
$month .= "<td>$day</td>";
$day++;
}
foreach($array as $day_number=>$quantity) {
$month = str_replace("<td>$day_number</td>","<td title=\"$quantity spots available\">$day_number</td>",$month)
}

In this application, either method will be done very quickly but my question is more general. Is it better to use string functions after the loop that creates the basic html, or to checking/processing at each iteration? Or is there a third, best way?

I have learned PHP entirely by myself using internet resources so I have no idea how bad my coding may be. Any help is appreciated.

Thank you.

  • 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-23T06:44:11+00:00Added an answer on May 23, 2026 at 6:44 am

    Here’s another solution:

    $day = 1;
    $strings = array();
    while($day < 31) {
        if (array_key_exists($day, $array)) {
            $strings[$day] = $array['$day'] . " spots available";
        } else {
            $strings[$day] = "No spots available for this date";
        }
        $day += 1;
    }
    foreach ($strings as $day => $string) {
        echo '<td title="' . $string . '">' . $day . '</td>';
    }
    

    What is so different about it? Well, fist of all, it’s likely going to be slower than your solution since I’m looping 31 times, twice! But this example is so trivial that performance is not your main concern.
    Your main concern is writing code that is so simple anyone can understand it.

    PHP code gets very complicated if you have lots of HTML printing going on. Generally what you want to do is make some sort of model (adhoc or systematic) that collects all your data, and only prints it to HTML as the very last step.

    Learn to separate the data-collection and model generation step from the HTML printing step. This may seem like overkill and a waste of CPU power and memory and what not, but what is more expensive: your time and sanity or some server in a hot room somewhere? 😉


    Your choice to have a sparse array of only the days that have spots available is already an optimization. And premature optimization is the mother of all evil. Think about the easiest way to solve your problem, build that, it’s likely going to be fast enough anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.