webguync Posted January 19, 2012 Share Posted January 19, 2012 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
webguync Posted January 19, 2012 Author Share Posted January 19, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 19, 2012 Share Posted January 19, 2012 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"; } Quote Link to comment Share on other sites More sharing options...
Andy-H Posted January 19, 2012 Share Posted January 19, 2012 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 Quote Link to comment Share on other sites More sharing options...
webguync Posted January 20, 2012 Author Share Posted January 20, 2012 see I knew there was a better way! Thanks this is a much better implementation. Quote Link to comment 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.