jcjst21 Posted August 14, 2011 Share Posted August 14, 2011 I am using this function to get the day of the week and placing it into a variable called $today $today=date("l"); I have a mysql table that has 7 fields one for each day of the week, and in that field if a checkbox on the form selects that day, a "1" is placed in the table and "0" of it is not I want to easily look for that field using the $today variable Can I do this in my mysql statement when i use a while loop? $getSchedule=mysql_query("SELECT * FROM schedule"); $schedule=mysql_fetch_array($getSchedule); while($schedule[$today]=="1") { /* where $today will place the value for instance $today will place "Sunday" for $schedule[sunday] */ } Will this work or do I need to it another way? Thanks, jcjst21 Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/ Share on other sites More sharing options...
voip03 Posted August 14, 2011 Share Posted August 14, 2011 What I understand from your statement while($schedule['date']==$today1) { } Note date is uyour field name. Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/#findComment-1257168 Share on other sites More sharing options...
jcjst21 Posted August 14, 2011 Author Share Posted August 14, 2011 Ok. So I can't use the php variable "today" to place the field name in the $schedule['today'] field? I have seven fields for Sunday through Saturday, by using the $today variable it will place what day of the week it is so the mysql will know what field in the table to look at... so I don't have to have a bunch of if statements to look at each field for each day of the week. Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/#findComment-1257169 Share on other sites More sharing options...
AbraCadaver Posted August 14, 2011 Share Posted August 14, 2011 Depends on what you want to do with the rows that have the column matching $today as 1. What you posted is perfectly valid: $today = date("l"); $getSchedule = mysql_query("SELECT * FROM schedule"); while($schedule = mysql_fetch_array($getSchedule)) { if($schedule[$today] == 1) { // do something } } But if you just want rows for today you could just do: $getSchedule = mysql_query("SELECT * FROM schedule WHERE $today = 1"); Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/#findComment-1257172 Share on other sites More sharing options...
jcjst21 Posted August 14, 2011 Author Share Posted August 14, 2011 ok so I can use the php variable in place of a field name in a mysql statement? That will work? If it will that's great, that's what i need to avoid tons of if statements. Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/#findComment-1257175 Share on other sites More sharing options...
AbraCadaver Posted August 14, 2011 Share Posted August 14, 2011 ok so I can use the php variable in place of a field name in a mysql statement? That will work? If it will that's great, that's what i need to avoid tons of if statements. Yes, but you might be better off changing the database to have a separate table with the days for each event and then join it when you query: Quote Link to comment https://forums.phpfreaks.com/topic/244762-help-with-using-php-variable-to-look-for-field-name-in-mysql-table/#findComment-1257178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.