Columns by day of week.

Hey guys. I’m new to this forum, PHP and programming itself. I’m looking for some guidance with a project I’ve been working on.

I have a DB of TV shows I watch. It includes the day of week the show is on. I’m able to figure out how to pull the show based on the day of week but I ended up writing 7 functions (1 for each day.) I know there’s a way to cover this in one function or class but I’m having trouble conceptualizing this.

I’m not asking for code itself but just some guidance on how to approach it. I essentially have a table-style layout with 7 columns (1 for each day.) I need something that will put each show in it’s corresponding column based on the dow field in that show’s record.

One idea I have is to somehow target a div with an id for each day of week but I’m not sure how to go about that.

Thanks!

PHP is one of the few things in the world for which the manual is helpful.

http://php.net/manual/en/function.date.php

[php]<?php
$today = date(‘D’);
echo $today;
?>[/php]
This will store the day of the week as a 3 letter string. I included the echo just so you can prove to yourself that the variable stored.

Now I’d need to know your table structure to help any further.

Thanks!

My table structure is pretty basic. I have a table for shows with id, show name, dow, air-date, and a few other misc. fields that aren’t significant to this issue.

I’m not having issues with the date or day of week showing up. The day of week is stored in the show table. I have 7 functions, all the same except for one variable: $day. Since those functions output the html with a while-loop, if I want to make a change to the code, I have to do it in 7 places. What I want is to figure out, conceptually, how to write either a single function or a class that will output the same code but only the show on that day.

I figure I can make 7 divs with IDs that correspond to the DOW. That’s easy. What I don’t know how to do is display the records with say, Sunday, as the DOW field in the div with an id of “sunday,” using a single function.

So I guess, conceptually, I want to include a variable that gets it’s value from the id of a div. Is that possible or is there a smarter way to do it?

I have not tested this, because I don’t have the tables to make it work.
[php]

<?php $server = ''; $username = ''; $password = ''; $database = ''; //fill in the appropriate information in between the single quotes // remember that $database is the database name, not the table name //The following connects to the server and the database if(!mysql_connect($server, $username, $password)) { exit('Error: could not establish database connection'); } if(!mysql_select_db($database)) { exit('Error: could not select the database'); } //Now we're connected, but first we'll establish the day of the week as a string variable and store it $today = date('D'); //note it's three characters long $sql = "SELECT show_title, show_time, show_main_character FROM shows WHERE show_day = '$today'"; /* Notes on above: you SELECT the different columns from the table that you will need to display somewhere FROM tells us the name of the table WHERE is a way of limiting the results, so we only get the values we selected for rows (shows) where the show day happens to be the present day. There is a catch. For this to work you must go into your table and re-enter the days of the week so that they are Sun, Mon, Tue, Thu, and so forth, three letters long and capitalized. */ $result = mysql_query($sql); if (!$result) { die('query failed.'); } if(mysql_num_rows($result) == 0) { die('query returned no results.'); } //All I did above was check to make sure you have some results //Now we're going to display your information echo '
'; while($row = mysql_fetch_assoc($result)) { echo ''; } //I think this is what you mean when you say you don't want to have seven different //functions, one for each day of the week. //let the day function decide the day for you, and run a query based on that. //Just make sure your table has values in it //that make this possible ?>

[/php]

Title Time Main character
'.$row["show_title"].' '.$row["show_time"].' '.$row["show_main_character"].'

Awesome! Thanks so much for the help. I really appreciate the time you took formulate an answer for me.

However, this isn’t exactly what I was asking for. My apologies for not explaining it properly.

On the site I’m working with, I’m displaying the shows for all 7 days of the week on one page. I’m trying to build a function that will pull all the records from the database, and display each show in it’s proper day of week.

I have this working with 7 functions. The only difference between each function is one variable that changes it’s day of week. Then, on the page, I call out each function as a left-floated ordered list. It works well but I always felt like I’m going about the functions the wrong way by writing 7 separate ones that are essentially the same.

I’ve tried a few different things, including using the day of week field to change the class and use CSS to position them but that doesn’t really work.

The next thing I’ll try is some kind of loop within a loop… but I’m not sure how to go about doing that.

Hey thanks again for your help. Although it wasn’t the answer I wanted, there’s some good stuff in there I think I need to improve my code overall. I tend to neglect the checking part :slight_smile:

Ok, I get what you are saying now. I don’t have enough experience with loops to know for certain if this will work. But I would guess it has something to do with defining an array:
[php]
$day = array(‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’);

foreach ($day as $value)
{
//Then run one of the mysql queries I described below, using WHERE something = ‘$value’
//Also use almost the same table making loop I described earlier as well.

}
[/php]
So yeah, a loop within a loop.

Sponsor our Newsletter | Privacy Policy | Terms of Service