So! Basically I have a database with a load of blog posts, these are all sorted by a UNIX timestamp, and what I need is a way to make this code spit out headers when appropriate, so that it will output something like this:
2008
November
Title 1 - Date Goes Here
Title 2 - Date Goes Here
December
Title 3 - Date Goes Here
2009
January
Title 4 - Date Goes Here
etcetera
Here's my code so far, it works until the comparison of the year, and I still need to come up with a good way of how to make it compare months in a sensible fashion, so that January indeed comes after December, and not some ludicrous 13th month.
[code]
<?php
if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
$stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$current_year = date("Y", $row[1]);
$current_month = date("m", $row[1]);
if ($current_year > $last_year) {
echo "<h1>" . $current_year . "</h1>";
$last_year = $current_year;
}
echo "<tr>";
echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
echo "</tr>";
}
}
} else {
die($sqliteerror);
}
?>
[/code]
With unix timestamps you could do something like (pseudo code obviously)
prev_month = null
prev_year = null
foreach results as r
new_month = date('F', r[timestamp]);
new_year = date('Y', r[timestamp]);
if(prev_month != new_month)
//display month
/if
if(prev_year != new_year)
//display year
/if
// display other info
prev_month = new_month
prev_year = new_year
/foreach
<?php
if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
$stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$current_year = date("Y", $row[1]);
$current_month = date("n", $row[1]); # n instead of m
if ($current_year > $last_year) {
echo "<h1>" . $current_year . "</h1>";
echo "<h2>" . $current_month . "</h2>";
$last_year = $current_year;
$last_month = $current_month;
}
elseif ($current_month > $last_month) {
echo "<h2>" . $current_month . "</h2>";
$last_month = $current_month;
}
echo "<tr>";
echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
echo "</tr>";
}
}
} else {
die($sqliteerror);
}
?>
I did not test it.
Idea:
If the year changes, also the month changes.
The check for the next month happens only if there is not a change of the year, which is the only case where lastmonth might be bigger than currentmonth.
I would be tempted to do this in two steps, separating the database fetching from the display/html logic, for example:
<?php
//snip
//make big array of archive
$archive = array();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$year = date("Y", $row[1]);
$month = date("m", $row[1]);
if (!isset($archive[$year])) {
$archive[$year] = array();
}
if (!isset($archive[$year][$month])) {
$archive[$year][$month] = array();
}
$archive[$year][$month][] = $row;
}
//snip
//loop over array and display items
?>
<?php foreach ($achive as $year => $months): ?>
<h1><?php echo $year; ?></h1>
<?php foreach ($months as $month => $posts): ?>
<h2><?php echo $month; ?></h2>
<ul>
<?php foreach ($posts as $post): ?>
<li><?php echo $post[0]; ?> etc...</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
You should get the years and the months in reverse order as per your SQL query. I haven't tested this, but it should be vaguely correct.
Related
I'm creating a calendar with Sunday being the start of each week and I want to present the week number of the year before each row of dates.
This is my calendar
my calendar
How can I add week numbers like this:
This is what I need
My code:
<?php
function generate_calendar($month, $year) {
$calendar = array();
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_day_of_month = date('w', strtotime("$year-$month-01"));
$week_number = 1;
$week = array();
// Determine the week number offset
$days_from_january_1st = strtotime("$year-01-01");
$days_to_first_day_of_month = strtotime("$year-$month-01");
$week_number_offset = floor(($days_to_first_day_of_month - $days_from_january_1st) / 604800);
// Check if the last week of the previous month is full (7 days)
$last_week_of_previous_month = $week_number_offset;
if ($month > 1) {
$previous_month = $month - 1;
$previous_month_year = $year;
$previous_month_year = date("Y", strtotime("-1 month" , strtotime("$year-$month-01")));
$previous_month = date("n", strtotime("-1 month" , strtotime("$year-$month-01")));
if ($previous_month == 0) {
$previous_month = 12;
$previous_month_year = $year - 1;
}
$days_in_previous_month = cal_days_in_month(CAL_GREGORIAN, $previous_month, $previous_month_year);
$last_day_of_previous_month = date('w', strtotime("$previous_month_year-$previous_month-$days_in_previous_month"));
if ($last_day_of_previous_month == 6) {
$last_week_of_previous_month=$last_week_of_previous_month + 1;
} else {
// Check if the last week of the previous month only has a few days
$last_week_of_previous_month = $last_week_of_previous_month;
}
}
// Generate the previous month's days
for ($i = 1; $i <= $first_day_of_month; $i++) {
array_unshift($week, "");
}
// Generate the current month's days
for ($i = 1; $i <= $days_in_month; $i++) {
$week[] = $i;
if (count($week) == 7) {
$calendar[$week_number + $last_week_of_previous_month] = $week;
$week_number++;
$week = array();
}
}
// Generate the next month's days
$remaining_days = 7 - count($week);
for ($i = 1; $i <= $remaining_days; $i++) {
$week[] = '';
}
if (!empty($week)) {
$calendar[$week_number + $last_week_of_previous_month] = $week;
}
return $calendar;
}
$year = 2022;
echo "<table>\n";
echo " <tr>\n";
echo " <th colspan='3'>$year</th>\n";
echo " </tr>\n";
echo " <tr>\n";
for($i=1; $i<=12; $i++){
$month_name = date('F Y', strtotime("$year-$i-01"));
$calendar = generate_calendar($i, $year);
echo "<td>\n";
echo "<table>\n";
echo " <tr>\n";
echo " <th colspan='8'>$month_name</th>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <th>Week</th>\n";
echo " <th>Sun</th>\n";
echo " <th>Mon</th>\n";
echo " <th>Tue</th>\n";
echo " <th>Wed</th>\n";
echo " <th>Thu</th>\n";
echo " <th>Fri</th>\n";
echo " <th>Sat</th>\n";
echo " </tr>\n";
foreach ($calendar as $week_number => $week) {
echo " <tr>\n";
echo " <td>$week_number</td>\n";
foreach ($week as $day) {
if (empty($day)) {
echo " <td></td>\n";
} else {
echo " <td>$day</td>\n";
}
}
echo " </tr>\n";
}
echo "</table>\n";
echo "</td>\n";
if($i==3 || $i==6 || $i==9){
echo " </tr>\n";
echo " <tr>\n";
}
}
echo " </tr>\n";
echo "</table>\n";
?>
Trying to add week number which counts from first week of January (if last week of previous month is not full 7 days then first week of current month start from last week of previous month), but my code only count from first week of current month.
I managed to get the right result (as far as I tested) with IntlCalendar and its FIELD_WEEK_OF_YEAR constant instead of counting Sundays.
I did some refactoring of your function, but ran out of time to do a full reduction of the script (there may still be lines that can be simplified/condensed).
Code: (Demo)
function generate_calendar(int $month, int $year) {
$calendar = [];
$week_number = (IntlCalendar::fromDateTime ("$year-$month-01"))->get(IntlCalendar::FIELD_WEEK_OF_YEAR);
$month_start = new DateTime("$year-$month-01");
// Generate the previous month's days
if ($first_day = $month_start->format('w')) {
$week = array_fill(1, $first_day, '');
}
// Generate the current month's days
$days_in_month = $month_start->format('t');
for ($i = 1; $i <= $days_in_month; $i++) {
$week[] = $i;
if (count($week) == 7) {
$calendar[$week_number] = $week;
$week_number++;
$week = [];
}
}
if ($week) {
// Generate empty days for the next month
$calendar[$week_number] = array_pad($week, 7, '');
}
return $calendar;
}
I don't believe there is any way to set the start day of the week in the standard library of PHP without manual calculation, but you can do it easily with the intl library (make sure the intl PHP extension is enabled):
$cal = IntlGregorianCalendar::createInstance();
// Set start of week to Sunday
$cal->setFirstDayOfWeek(IntlGregorianCalendar::DOW_SUNDAY);
// Get week number of the year for the current date
$weekOfYear = intval(IntlDateFormatter::formatObject($cal, 'w'));
// Advance to the next week
$cal->add(IntlGregorianCalendar::FIELD_WEEK_OF_YEAR, 1);
// ect...
I have a table with headers; Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. I have gathered user "shifts" from a database...
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<?php $sql4 = "SELECT * FROM shifts WHERE date = '" . $sunday . "' or date = '" . $monday . "'
or date = '" . $tuesday . "' or date = '" . $wednesday . "'
or date = '" . $thursday . "' or date = '" . $friday . "'
or date = '" . $saturday . "'";
$result1 = $database->query($sql4);
echo"<tr>";
while ($row4 = mysqli_fetch_array($result1)){
echo "<td>";
echo $row4['name'];
echo "</td>";
}
echo "</tr>"
So there can be any amount of shifts per day, but dependant on what day the shift is on i need to place the shift under the correct table heading.
The shifts table has: shift_id, name, date, day
$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
$weekDay = $row4['day'];
$resultsArray[$weekDay][] = $row4['name'];
}
for ($x = 0; $x <= count($resultsArray); $x++) {
$sundayData = (!empty($resultsArray['Sunday'][$x]) ) ? $resultsArray['Sunday'][$x] : "";
$mondayData = (!empty($resultsArray['Monday'][$x]) ) ? $resultsArray['Monday'][$x] : "";
$tuesdayData = (!empty($resultsArray['Tuesday'][$x]) ) ? $resultsArray['Tuesday'][$x] : "";
$wednesdayData = (!empty($resultsArray['Wednesday'][$x]) ) ? $resultsArray['Wednesday'][$x] : "";
$thursdayData = (!empty($resultsArray['Thursday'][$x]) ) ? $resultsArray['Thursday'][$x] : "";
$fridayData = (!empty($resultsArray['Friday'][$x]) ) ? $resultsArray['Friday'][$x] : "";
$saturdayData = (!empty($resultsArray['Saturday'][$x]) ) ? $resultsArray['Saturday'][$x] : "";
echo "<tr>";
echo "<td>".$sundayData."</td>";
echo "<td>".$mondayData."</td>";
echo "<td>".$tuesdayData."</td>";
echo "<td>".$wednesdayData."</td>";
echo "<td>".$thursdayData."</td>";
echo "<td>".$fridayData."</td>";
echo "<td>".$saturdayData."</td>";
echo "</tr>";
}
?>
It would be easier to answer your question if I could see the exact structure of your data, but I'll give this a whirl anyway and hopefully you can see where i'm going with it.
What I would do is create an array and fill in the information as you go through the results. Once you have the new resulting array, build your table.
$resultsArray = array();
while ($row4 = mysqli_fetch_array($result1)){
$weekDay = $row4['day'];
$resultsArray[$weekDay][] = $row4['shift'];
}
Now you have an array that contains all the shifts organized by day. Looping through this array will allow you to create your table the way you want.
This is untested so may have some mistakes in it, but here is an example...
for ($x = 0; $x <= 10; $x++) {
echo "<tr>";
echo "<td>".$resultsArray['Sunday'][$x]."</td>";
echo "<td>".$resultsArray['Monday'][$x]."</td>";
echo "<td>".$resultsArray['Tuesday'][$x]."</td>";
echo "<td>".$resultsArray['Wednesday'][$x]."</td>";
echo "<td>".$resultsArray['Thursday'][$x]."</td>";
echo "<td>".$resultsArray['Friday'][$x]."</td>";
echo "<td>".$resultsArray['Saturday'][$x]."</td>";
echo "</tr>";
}
Note : Keep in mind you'll have to change this portion $x <= 10 to fit your actual array size.
Basically I am building a calendar/event booking system on my local PC for personal use, I have the functionality working to get the days from my database that have a booking attached to them, and you recolour the dates in the array accordingly, but I'm trying to work out if I could customise the result of the 1st day in each range, and the following days being the same.
Here is how it looks(if it makes it any easier)
And here is the code I am currently using on the page..
<?php
function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
?>
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="background-color: #aaaaaa;">
<?php
$cmonth = date('F');
$cyear = date('Y');
$res = mysql_query("SELECT * FROM trips WHERE year(start_date) = '$cyear' ORDER BY start_date ASC");
$array_days = array();
while ($rows = mysql_fetch_array($res)) {
$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
foreach($selected_dates as $selected_date){
$array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
$array_days[$selected_date]['colour'] = "". $rows['colour'];
$array_days[$selected_date]['booked'] = "". $rows['booked'];
$array_days[$selected_date]['capacity'] = "". $rows['capacity'];
}
}
?>
<tr>
<td> </td>
<?php for($i=1;$i<=31;$i++){?>
<td class="trip-date-cell"><?php echo $i;?></td>
<?php }?>
</tr>
<?php
$current_month = date("m");
$next_6_month = date("m", strtotime("+5 month", strtotime(date("F") . "1")));
for($i=$current_month;$i<=$next_6_month;$i++){ // 12 months in year
?>
<tr>
<td class="trip-month-cell"><?php echo date('M', mktime(0, 0, 0, $i,10, $cyear)); ?></td>
<?php
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$i,$cyear);
foreach (range(1, $days_in_month) as $days) {
$key = date('Y-m-d', mktime(0, 0, 0, $i, $days, $cyear));
if(array_key_exists($key,$array_days)){
$color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'> </td>";
} else {
echo "<td class='trip-day-blank'> </td>";
}
}
?>
</tr>
<?php } ?>
</table>
Any suggestions or advice would be appreciated.
You could store the first date information in your array when reading in the date ranges:
while ($rows = mysql_fetch_array($res)) {
$selected_dates = date_range(date("Y-m-d",strtotime($rows['start_date'])), date("Y-m-d",strtotime($rows['end_date'])));
// default to true: first date is first day in range
$firstDate = true;
foreach($selected_dates as $selected_date) {
// store first day info in array
$array_days[$selected_date]['first_day'] = $firstDate;
// set to false for all following days
if ($firstDate) $firstDate = false;
$array_days[$selected_date]['start_end'] = "". $rows['trip_start'] . " to " . $rows['trip_end'];
$array_days[$selected_date]['colour'] = "". $rows['colour'];
$array_days[$selected_date]['booked'] = "". $rows['booked'];
$array_days[$selected_date]['capacity'] = "". $rows['capacity'];
}
}
and then while rendering access the information to achieve your desired result:
$color = $array_days[$key]['capacity'] - $array_days[$key]['booked'] == 0 ? "#303030" : $array_days[$key]['colour'];
// set content of cell according to first day info
$content = $array_days[$key]['first_day'] ? '<strong>1</strong>' : ' ';
echo "<td class='trip-day-book' style='background-color: ".$color."' alt='".$array_days[$key]['start_end']."' title='".$array_days[$key]['start_end']."'>$content</td>";
I'm trying to build a year\month archive.
It refuses to output what i want so i'm hoping one of you might push me in the right direction.
I want to sort by year, and show the months for each year ( if exists) and output it like this:
2012
- June
- August
2011
- July
my code is this :
$query = "SELECT * FROM article WHERE full_name ='$safe_name' group by year, month";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
echo "<ul class='year'><li><a>{$year}</a>";
echo "<ul class='months'><li><a>{$month}</a></li></ul>
</li></ul>";
}
But it outputs :
2012
- june
2012
- august
2011
- july
When i group only by year, it will output:
2012
- june
2011
- july
i have a "date_posted" row which is datetime (yyyy-mm-dd 00:00:00).
On top of that i have 1 row for month and 1 for year ( i know that's dumb, but i couldnt figure out how to do this by just using the "date_posted" row.
Ive read up on some posts on this topic, but its not doing the trick for me.
Any help is greatly appreciated!
This should work. When fetching results, sometimes it's handy to restructure it to be easily usable in iterating a view.
$query = "SELECT * FROM article WHERE full_name ='$safe_name' group by year, month";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
// Get data
$year = $row['year'];
$month = $row['month']
// Structure it
$archive[$year][] = $month;
}
// Display data
foreach($archive as $year => $months)
{
// Show year
echo "<ul class='year'><li><a>{$year}</a>";
// Month container
echo "<ul class='months'>"
// Get months
foreach($months as $month)
{
echo("<li><a>{$month}</a></li>";
}
// Close Month/Year containers
echo("</ul></li></ul>");
}
You need to use a simple 'state' machine to detect when a year changes:
$previous_year = null;
$frist = true;
echo "<ul>"
while($row = mysql_fetch_assoc($result)) {
if ($row['year'] != $previous_year) {
// got a new year
if (!$frist) {
echo "</ul></li>"; // only close a previous list if there IS a previous list
$frist = false;
}
echo "<li class="year">$row[year]\n<ul>";
$previous_year = $row['year'];
}
echo "<li class="month">$row['month']</li>";
}
$temp_year = 0;
while($row = mysql_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
if( $temp_year != $year )
{
if( $temp_year > 0 )
echo "</li></ul>";
$temp_year = $year;
echo "<ul class='year'><li><a>{$year}</a>";
}
echo "<ul class='months'><li><a>{$month}</a></li></ul>";
}
Give a shot
$storage_array = array();
while($row = mysql_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
$storage_array[$year][] = $month;
}
foreach ($storage_array as $year => $month_array){
echo "<ul class='year'><li><a>{$year}</a>";
foreach ($month_array as $month){
echo "<ul class='months'><li><a>{$month}</a></li></ul>";
}
echo "</li></ul>";
}
Not sure how to name correctly this question so feel free to edit if is needed.
I have 12 boxes one per month, and each box display:
1 - A message like "Sold" or "Available" depending on the info coming from the database.
2 - The name of the month and the year, if the month is older than the current month the year will be increased by one.
The info coming from the database have a few values separated by a | symbol, the pertinent one to check is the las one, an example value of $row['custom'] is: 93 | Supper | New Port | August - 2012
My problem is that I need to update each box with their "status" and with the current script only the las entry on the database is used to update the boxes, so In cases where i know there is two or more boxes that should display the message "Sold", only the most recent is the updated.
How can I modify the following script to update one by one box ?
The problem is the way I'm querying the database or something else ?
Thanks in advance for any help or advice.
Code only for 2 of 12 month, to make things short
<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year
// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";
if ($month > $january) {
$jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}
if ($month > $february) {
$feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}
//Get info from Database
$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
}
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';} else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';} else { $feb_status = 'Available';}
//Output the months and their status
?>
<div class="month">
<div class="mname"><?php echo $jan;?></div>
<div class="<?php echo $jan_status;?>">
<?php echo $jan_status;?>
</div>
<?php if($jan_status == 'Available') {
echo 'This month is ' . $jan_status . '.';
} else {
echo 'This month has been ' . $jan_status . '.';} ?>
</div>
<div class="month">
<div class="mname"><?php echo $feb;?></div>
<div class="<?php echo $feb_status;?>">
<?php echo $feb_status;?>
</div>
<?php if($feb_status == 'Available') {
echo 'This month is ' . $feb_status . '.';
} else {
echo 'This month has been ' . $feb_status . '.';} ?>
</div>
You misplaced the closing bracket of while and move the `$jan_status='Available' from inside while loop to just above it; Here is the modified code
<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year
// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";
if ($month > $january) {
$jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}
if ($month > $february) {
$feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}
//Get info from Database
$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'";
$result = mysql_query($query) or die(mysql_error());
$jan_status = 'Available';
$feb_status = 'Available';
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';}
if ($month_sold == $feb) { $feb_status = 'Sold';}
}//th closing bracket should be here;
//Output the months and their status
?>
<div class="month">
<div class="mname"><?php echo $jan;?></div>
<div class="<?php echo $jan_status;?>">
<?php echo $jan_status;?>
</div>
<?php if($jan_status == 'Available') {
echo 'This month is ' . $jan_status . '.';
} else {
echo 'This month has been ' . $jan_status . '.';} ?>
</div>
<div class="month">
<div class="mname"><?php echo $feb;?></div>
<div class="<?php echo $feb_status;?>">
<?php echo $feb_status;?>
</div>
<?php if($feb_status == 'Available') {
echo 'This month is ' . $feb_status . '.';
} else {
echo 'This month has been ' . $feb_status . '.';} ?>
</div>
Every time you run $row = mysql_fetch_array(), it replaces the contents of $row with the next row from the database. Look at the while loop: it assigns values to $check_custom and $month_sold, but doesn't do anything with them before overwriting them with new values. You need to move all your code for parsing the database output and generating the info for a given month inside the loop. Then output or save the information you want to display for that month before it goes on to the next one.
There's a lot you could do to make your code simpler and easier to maintain. For example, I would make an array for the months and iterate over that, rather than creating a separate set of variables and separate output code for each month. Also, what you're doing with your custom column right now is using a field in your database table to store another tiny table. This creates a lot of problems -- if you broke that data out into multiple columns, you could just do a query for the appropriate month and year.
Move your status checks into the loop so that it operates on every row from the database. Otherwise you only wind up referring to the row returned by the last call of mysql_fetch_array. I would recommend using a switch statement for readability, though.
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';}
else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';}
else { $feb_status = 'Available';}
}