Jump to content

how to assign different css values to info pulled from MySQL


Recommended Posts

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?

 

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";
}
?>

 

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.

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];						   
}

 

?

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.