Jump to content

need help with adding a CSS class


webguync

Recommended Posts

I had this working before, but my coding structure has changed a bit and need help with syntax.

 

My function I have down ok.

 

function cssfrommvp($mvp) {
$class = array('MVP' => 'MVP');
return $class[$mvp];
}

 

this is how I would add the class to a <td> with $row.

 

echo "<td class=\"".cssfrommvp($row['mvp'])."\">".$row['mvp']."</td>\n";

 

now using $array

 

<td> . $MVP .</td>\n</tr>

 

how would I do the same thing as done before with $row?

 

 

Link to comment
Share on other sites

My function I have down ok.

 

function cssfrommvp($mvp) {
$class = array('MVP' => 'MVP');
return $class[$mvp];
}

 

That function will not work. It will always return nothing (unless the value of $mvp is the literal string 'MVP'). So, I'm not sure what you are really trying to do.

 

The function will generate a variable array ($class) with exactly one element with the index 'MVP' . Then it will attempt to return the value of $class[$mvp]. If $mvp has any value other than 'MVP' then there is no value for $class[$mvp] defined.

Link to comment
Share on other sites

I am pretty sure it worked before. What I am trying to do is add CSS to every record in the database with the value MVP. The column is also called MVP. As an example I want to add a background so in my CSS.

 

.MVP{

background:red;

}

 

and I want that class added to every record with the value MVP in the MVP column.

 

my while loop

while($array = mysql_fetch_array($sql)) {

	$sport = $array['sport'];
	$first_name = $array['first_name'];
	$last_name = $array['last_name'];
	$team = $array['team'];
	$MVP = $array['MVP'];
			echo "<tr><td>" . $first_name . "</td>\n<td>". $last_name ."</td>\n<td>" .$team."</td>\n<td> . $MVP .</td>\n</tr>\n";
    
  

}

 

there is probably an easier way of doing it.

Link to comment
Share on other sites

If you are adding the class 'MVP' to the element then why do you need a function? maybe I am completely misunderstanding what you are doing. BUt, that function will only return the value 'MVP' if it is passed the value 'MVP', so I don't really see what you are wanting. The only thing I can think of is that you have a column in that table, called 'MVP', and if the record is an MVP then the value in that field is 'MVP', else it is some other value. That's really a poor implemetnation. If you have a column to identify if a record is an MVP or not, then it should be an INT type with a 0 or 1 (where 1 is true and the record is an MVP)

 

As for the code you just posted, it's kind of a waste to define variables just to use them once in that loop. But, if my assumption above is correct and you want to set the class of the TDs as 'MVP' if the value of the 'MVP' field is 'MVP' then this would work (no function needed).

while($row = mysql_fetch_assoc($sql))
{
    $class = ($row['MVP']=='MVP') ? 'MVP' : '';
    echo "<tr>\n";
    echo "  <td class='{$class}'>{$row['first_name']}</td>\n";
    echo "  <td class='{$class}'>{$row['last_name']}</td>\n";
    echo "  <td class='{$class}'>{$row['team']}</td>\n";
    echo "  <td class='{$class}'>{$row['MVP']}</td>\n";
    echo "</tr>\n";
}

 

And, if you changed that field to an int and used 0/1 this would work to change the class and set the MVP value

while($row = mysql_fetch_assoc($sql))
{
    $class = ($row['MVP']) ? 'MVP' : '';
    $mvpText = ($row['MVP']) ? 'MVP' : '';
    echo "<tr>\n";
    echo "  <td class='{$class}'>{$row['first_name']}</td>\n";
    echo "  <td class='{$class}'>{$row['last_name']}</td>\n";
    echo "  <td class='{$class}'>{$row['team']}</td>\n";
    echo "  <td class='{$class}'>{$mvpText}</td>\n";
    echo "</tr>\n";
}

Link to comment
Share on other sites

while($row = mysql_fetch_assoc($sql))
{
    $class    = ($row['MVP']) ? 'class="MVP"' : '';
    $mvpText = ($row['MVP']) ? 'MVP' : '';
    echo "<tr>\n";
    echo "  <td {$class}>{$row['first_name']}</td>\n";
    echo "  <td {$class}>{$row['last_name']}</td>\n";
    echo "  <td {$class}>{$row['team']}</td>\n";
    echo "  <td {$class}>{$mvpText}</td>\n";
    echo "</tr>\n";
}

sorry, couldn't bear the class='' that was going to leave lol

 

 

Nice to see someone else with view source indent OCD too tho  ::)

Link to comment
Share on other sites

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.