webguync Posted February 28, 2011 Share Posted February 28, 2011 I am trying to figure out the best way to do something if there is a way. I am pulling values from a MySQL table and using a while loop to display the data, so it looks something like this. while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['last_name'] . "</td>\n"; echo "<td>" . ucwords($row['first_name']) . "</td>\n"; echo "<td>" . $row['Class_Date'] . "</td>\n"; echo "</tr>"; for the class dates, I want the column background to be a different color depending on the date. For instance January would hvae a blue background, February a red one etc. I was going to try and do that with CSS like echo "<td class="blue">" . $row['Class_Date'] . "</td>\n"; in the CSS... .blue{ background-color:#06c; } but not sure the best way to make the changing data values different colors with CSS. any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/ Share on other sites More sharing options...
JonnoTheDev Posted February 28, 2011 Share Posted February 28, 2011 How is the date formatted in $row['Class_Date']. Is it YYYY-MM-DD? If so you could use a simple function: <?php function cssfromdate($date) { $parts = explode('-',$date); $month = $parts[1]; $class = array('01' => 'red', '02' => 'green', '03' => 'blue', '04' => 'orange', '05' => 'white', '06' => 'black', '07' => 'blue', '08' => 'orange', '09' => 'white', '10' => 'black', '11' => 'black', '12' => 'black'); return $class[$month]; } while($row = mysql_fetch_array($result)) { echo "<tr>\n"; echo "<td>".$row['last_name']."</td>\n"; echo "<td>".ucwords($row['first_name'])."</td>\n"; echo "<td class=\"".cssfromdate($row['Class_Date'])."\">".$row['Class_Date']."</td>\n"; echo "</tr>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180808 Share on other sites More sharing options...
webguync Posted February 28, 2011 Author Share Posted February 28, 2011 currently it is just text (January 2011), but I can change to any format it needs to be. Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180812 Share on other sites More sharing options...
JonnoTheDev Posted February 28, 2011 Share Posted February 28, 2011 OK, well you can see how I have done this. The date value that is passed into the function is in the format YYYY-MM-DD. It extracts the month and returns the class name for that month. You can either change the array keys in the function to the month name if you want to pass the likes of 'January' in or just reformat the date. It's really simple. Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180813 Share on other sites More sharing options...
webguync Posted February 28, 2011 Author Share Posted February 28, 2011 ok neat thanks. So if I wanted to change the array keys it would be... $class = array('January' => 'red', 'February' => 'green', 'March' => 'blue', 'April' => 'orange', etc... '); return $class[$month]; } ? Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180816 Share on other sites More sharing options...
webguync Posted February 28, 2011 Author Share Posted February 28, 2011 I am getting some notices with the code I am trying Notice: Undefined offset: 1 in index.php on line 57 Notice: Undefined index: in index.php on line 62 I changed the database content to be either 'January' or 'March' just to try this out. in my PHP function cssfromdate($date) { $parts = explode('-',$date); $month = $parts[1]; $class = array('January' => 'January', 'March' => 'March', ); return $class[$month]; } while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['last_name'] . "</td>\n"; echo "<td>" . ucwords($row['first_name']) . "</td>\n"; echo "<td class=\"".cssfromdate($row['Class_Date'])."\">".$row['Class_Date']."</td>\n"; echo "</tr>"; } it looks like the notices are for these two lines $month = $parts[1]; return $class[$month]; please explain what I need to do to fix. Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180873 Share on other sites More sharing options...
JonnoTheDev Posted February 28, 2011 Share Posted February 28, 2011 function cssfromdate($date) { $class = array('January' => 'red', 'March' => 'blue'); return $class[$date]; } Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1180964 Share on other sites More sharing options...
webguync Posted March 1, 2011 Author Share Posted March 1, 2011 thanks, seems to be working as intended now. I am still getting this notice though. Notice: Undefined index: in index.php on line 57 which is this line [php return $class[$date]; [/code] how do I get rid of that notice? Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1181282 Share on other sites More sharing options...
webguync Posted March 1, 2011 Author Share Posted March 1, 2011 never mind. I had error reporting set to ALL. I just took that out and no more notice so all is good. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/229142-how-to-assign-different-css-values-to-info-pulled-from-mysql/#findComment-1181315 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.