1

This is for a timetable and what it does is displays the previous day's timetable and who has booked each slot (this is for a radio station.)

At the moment it displays who has booked each slot in chronological order however I would like it to say the time next to each result. The query outputs 24 rows (which is from Midnight to 23:00) and next to each slot I would like it to say (00:00, 01:00, 02:00, 03:00... 21:00, 22:00 so on so forth.)

This is my current code:

<?php 
include("../config.php");

#// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);

echo "<table width=\"580px\" class=\"board\" border=\>";

$order = "SELECT * FROM timetable WHERE day = '$yesterdow'";
$result = mysql_query($order);

// Error checking
if (!$result) {
  // output error, take other action
}
else {
  while ($row=mysql_fetch_array($result)){
     // Append all results onto an array
     $rowset[] = $row;
  }
}
    foreach ($rowset as $row) {
  echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}



?> 

Can you help?

8
  • Also, you should store days of the week as numbers, not names. Then you can sort them naturally and they're not dependent on English. Commented Apr 11, 2012 at 18:43
  • 1
    @Corbin - except his actual question doesn't seem to say anything about sorting, rather displaying the time. Commented Apr 11, 2012 at 18:43
  • 1
    How are the timeslots stored in your db? Commented Apr 11, 2012 at 18:43
  • @Cfreak Ah, you're right. Oops. Thought he currently had the times next to them but them to be sorted. Seems I should have read the question closer. Commented Apr 11, 2012 at 18:44
  • @Corbin - I was confused at first too. I edited the title to reflect his actual question. Commented Apr 11, 2012 at 18:46

2 Answers 2

1

I think we're all looking too hard at a VERY simple problem. You are already using SELECT * in your query, so you're already fetching all three columns from your table. So now, all you need to do is add another cell to each row of your table.

echo "<tr><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></tr>";

And to make sure you are fetching your rows in the correct order, you should add an ORDER BY to your query:

SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time

If you don't specify an ORDER BY clause, you have no guarantee that you will get the results in any particular order.

And one last thing, you are looping through the rows twice, unnecessarily. Get rid of the foreach loop and put the echo directly inside the while loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. Thank you. Is there a way I could now translate the outputted information into a RSS feed?
Sure, you can. But that's outside the scope of this question. You may want to follow some tutorials before asking questions here. W3Schools has some good tutorials for beginners. Stack Overflow is more for asking for a solution to a specific problem, not a place to ask for tutorials.
0

try this:

foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . htmlspecialchars($row['time'])"</td></tr>";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.