Change a PHP background colour to a CSS call - php

I am no coder but am trying to take anything CSS related out of a php file but require a little help.
How can I re write the below code to get the bgcolor from an external CSS instead of the php doing the job
I just want the bellow rewriting to include the CSS class instead of the code actualy making the color.
Hope you understand what I am saying
first bit of code
$bgcolour = ($k % 2) ? 'bgcolor="#FFFEEE"' : '';
Second bit of code
'ROWCOLOUR' => ($row['highlighted'] == 'y') ? 'bgcolor="#fea100"' : $bgcolour,

CSS:
.fffeee {
background-color: #FFFEEE;
}
.fea100 {
background-color: #FEA100;
}
PHP:
$cssClass = ($k % 2 == 0 && $row['highlighted'] != 'y') ? 'fffeee' : 'fea100';
'ROWCOLOUR' => 'class="' . $cssClass . '"',

<style>
.mouseoverbg{
background-color : #eeefff;
}
.oddrowbg{
background-color : #fffeee;
}
.evenrowbg{
background-color : #fea100;
}
</style>
<table>
<?php
$ni = 5;
for($i=0 ; $i<$ni; $i++)
{
$bgcolor = ($i%2)?"evenrowbg" : "oddrowbg";
?>
<tr onmouseover="this.className='mouseoverbg'" onmouseout="this.className='<?php echo $bgcolor?>'" class="<?php echo $bgcolor?>">
<td><?php echo $i;?></td>
</tr>
<?php
}?>
</table>
Try this code it may help you.

Related

Overriding CSS code trough visual composer

I have the following line of html code which i don't have access to.
Since i wanted to hide a TAB which is the first <a href='#all' ...>, I used the custom css options of visual composer which allows me to override an existing code. To do that I simply used
.testing, a[href='#all'] {
visibility: hidden; }
I'm fine with this as long as that TAB is not visible. But next problem here is this particular TAB is always selected when i refresh the page, simply because the class=selected is applied to it. Can i somehow override it using css to apply the class selected to one of the other 2 <li>'s ?
Edit
I managed to find an access to the following line of php code which stands behind these TABs
<li>
<ul class="tabs">
<?php
$obj = new stdClass();
$obj->id = 0;
$obj->title = esc_html__('All', 'arcane');
$obj->abbr = esc_html__('All', 'arcane');
$obj->icon = 0;
array_unshift($games, $obj);
for($i = 0; $i < sizeof($games); $i++) :
$game = $games[$i];
$link = ($game->id == 0) ? 'all' : 'game-' . $game->id;
$p = array( 'game_id' => $game->id,'status' => array('active', 'done'));
$matches_tab = $ArcaneWpTeamWars->get_match($p, false, true);
if(!empty($matches_tab)){
?>
<li<?php if($i == 0) echo ' class="selected"'; ?>><?php echo esc_attr($game->abbr); ?><div class="clear"></div></li>
<?php } endfor; ?>
</ul>
<div class="clear"></div>
</li>
You can assign the selected class to the second li by changing the 0 to 1 and also use your own css to hide the first one:
<li<?php if($i == 1) echo ' class="selected"'; ?>>
or you can totally remove the first option which refers to all by starting the loop from 1 instead of 0 and ignore hiding first one using css:
for($i = 1; $i < sizeof($games); $i++) : // Start loop from 1 instead of 0
You can try to change the php code and instead of
$link = ($game->id == 0) ? 'all' : 'game-' . $game->id;
to have
$link = ($game->id == 2) ? 'game-2' : 'game-' . $game->id;
In this way the CS:GO should be the first. Im not 100% sure but try it :)

PHP - How to format cell background and font color according to MySQL query result in PHP file

I have workable mysql query in PHP file.
It is giving me 3 possible different results in my table on site in one column. They are 'WON', 'LOST', or 'PENDING'.
What i want to achieve further is when there is WON word in those specific cell to be in green background, when query result turns LOST to be red background, when PENDING to be grey.
In which way to do this?
I am newbie in this so couldnt find anser myself online.
Here is code of workable query:
<?
$qry = "
SELECT timelive,match_title,selection,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE game.selection THEN 'WON'
WHEN game.result NOT LIKE game.selection THEN 'LOST'
END AS result
FROM game
";
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = mysql_real_escape_string($_REQUEST['search_text']);
$qry .=" WHERE game.timelive LIKE '%$searchText%' " .
" OR game.match_title LIKE '%$searchText%' " .
" OR game.selection LIKE '%$searchText%' " .
" OR game.username LIKE '%$searchText%'";
}
$qry .= " ORDER BY timelive DESC";
$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;
?>
and HTML part of code for this part of output on site is this:
<table>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr>
<td align="center"><? echo $data['username']; ?></TD>
<td align="center"><? echo $data['result']; ?></TD>
</tr>
<?
$counter ++;
} ?>
i need to get this desired formatting described above according to output word in 'result' column.
Thanks.
A couple of options, the brute-force way, in which you simply apply a generated style, or by predefining style classes and applying them based on the output...
In the latter case (most reasonable, IMO), you simply apply the content of $result to the class property:
<td align="center" class="<?php echo $result;?>"><? echo $data['result']; ?></td>
In the first case, you might have something like this:
function getStyleColorForStatus($status) {
if ($status == 'WON') {
return 'Green';
}
else if ($status == 'LOST') {
return 'Red';
}
else if ($status == 'PENDING') {
return 'Grey';
}
return '';
}
<td align="center" style="background-color:<?php echo getStyleColorForStatus($data['result']); ?>"><? echo $data['result']; ?></td>
I'm missing the part, which creates your $data variable.
Just add "game.result" to your $data array. Now in your code, you could do something like this:
<tr>
<td align="center" class="<? echo $data['result'];?>"><? echo $data['username']; ?></TD>
<td align="center"><? echo $data['result']; ?></TD>
</tr>
Now you can work with CSS. Create three classes for LOST, PENDING and WON. Example:
.LOST {
background-color: #F00;
}
Your username field in your table should have a red background, when game.result is "LOST"
Create the following css definitions, if possible in a separate css file:
.won
{
background-color:green;
}
.lost
{
background-color:red;
}
After this, link the css file to your page and finally use jQuery to add/remove the css class depending on the given condition.
You can read more on the following links:
http://api.jquery.com/removeClass/
http://api.jquery.com/addClass/
Assign a css class to your table cell, based on the result, like so:
$tdClass = '';
switch ($data['result']) {
case 'WON':
$tdClass = 'won';
break;
case 'LOST':
$tdClass = 'lost';
break;
case 'PENDING':
$tdClass = 'pending';
break;
}
So obviously, this is your php, in your html you do:
<td class="<?php $tdClass; ?>"><?php echo $data['result']; ?></td>
I would lose the align="center" and use in your css text-align: center; instead. Furthermore, in your css, you'd do:
.won {
background: green;
}
.lost {
background: red;
}
.pending {
background: grey;
}
But instead of green, red, etc. choose the exact color you like.
before echo the result put an condition like
if( $data['result'] == 'WON' ){
echo '<div class="green">' . $data['result'] . '</div>'
}
elseif( $data['result'] == 'LOST' ){
echo '<div class="red">' . $data['result'] . '</div>'
}
elseif( $data['result'] == 'PENDDING' ){
echo '<div class="gray">' . $data['result'] . '</div>'
}

Table tr background color change

I'm trying to make in sort that odd <tr> and even <tr> have different color for easier reading.
Here's my code:
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res)){
$rc++;
if (($rc > 1)){
$tr = '#cccccc';
} else {
$tr = '#ffffff';
}?>
<tr style="background-color:<?php echo $tr ;?>">
It doesn't work, am I missing something?
You want to use modulo %
Some examples:
5 % 2 = remainder is 1
4 % 2 = remainder is 0
6 % 2 = remainder is 0
9 % 2 = remainder is 1
So based on whether the remainder is 1 or 0 you change the color. And you want the <tr> element to be a part of your loop as that's constantly being changed from one color or the other, back and forth.
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res))
{
$rc++;
if ($rc % 2 == 1)
{
$tr = '#cccccc';
}
else
{
$tr = '#ffffff';
?>
<tr style="background-color:<?php echo $tr ;?>">
<?
}
}
?>
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res)){
$rc++;
if (($rc > 1)){
$tr = 'odd';
} else {
$tr = 'even';
}?>
<tr class="<?php echo $tr ;?>">
CSS
.odd td { background-color: #FFF; }
.even td { backgorund-color: #F6F6F6; }
Use CSS and set the color background to the TD not the TR element.
As this is strictly Display, I would recommend doing is using Javascript, not PHP.
Look how simple it can be:
http://paragasu.wordpress.com/2009/01/05/alternate-table-row-color-the-easy-way/
Did you define $rc=0; anywhere?
The issue you're having appears to be that you're not resetting $rc++ after you've incremented it. Try setting it to 0 then reset it after you've incremented it to 1.
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res))
{
if ( isset($k) and $k==1)
{
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k=1;
}
..... more statement
}

Hide / Show divs from url string in php

A complete beginners question.
I have a large number of divs (>80) on a page (page2.php) and what I would like to do is open page1.php, click on a link to open page2.php and show only one of these divs depending on which link was clicked.
I have a basic working version of this by adding an if else to the divs. I've only done this on 5 of the divs so far and it works but it also seems a fairly in-eloquent way of doing things.
Page 1:
this is a link
Page 2:
<?php
$divID = $_GET['id'];
?>
<div id="r0101" <? if($divID == r0101): ?>class="show"<? else: ?>class="hidden"<? endif; ?> >
This then applies a css class to hide or show the div.
Would it be possible to have a function or whatever at the top of the page that takes the id from the url, figures out that there is a div with that id also, show it and hide all the others? This is probably an easy thing to do but it has me stumped.
Any help greatly appreciated.
Thanks.
Let alone the divs and work on css (as you relay on that to hide/show the divs).
You can generate not only markup but css stylesheet too. Use a similar one (put it at
the end of your head section). And let the browser do the work for you ;)
<style type="text/css">
div {
display: none;
}
div#<?php echo $_GET['id']; ?>:{
display: block;
}
</style>
$divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
echo '<div id="'.$div.'" class="';
if ($div == $divID)
{
echo 'show';
}
else
{
echo 'hidden';
}
echo '">';
}
Assuming I have read the question correctly, you have a set of divs (r0101, r0102, etc.) and wish to show only one of these depending on the page you are on. The code above creates an array of these divs, loops through and creates the div. The class of the div is 'show' if the div matches the div from the page url, and 'hidden' otherwise.
First of all, you should consider a way of making your divs to be printed dynamically. Something like:
<?php
for($i = 1; $i <= 80; $i++):
?>
<div id="r<?php print $i; ?>">div contents</div>
<?php
endfor;
?>
Also, if you find a way of doing what's stated above, you can also do something like:
<?php
for($i = 1; $i <= 80; $i++):
if($i == $_GET['id']){
$class = 'show';
} else {
$class = 'hidden';
}
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>
or
<?php
for($i = 1; $i <= 80; $i++):
$class = ($i == $_GET['id']) ? 'show' : 'hidden';
?>
<div id="r<?php print $i; ?>" class="<?php print $class; ?>">div contents</div>
<?php
endfor;
?>
which is exactly the same but (using the ternary operator) spares a few lines and (some people think) it decreases readability.
If you want to make your download faster, you should output only the div you want to show. You could do something like this:
$divs = array('r0101', 'r0102', 'r0103');
$divID = $_GET['id'];
foreach($divs as $div)
{
if($div == $divID){ echo '<div> /* CONTENT HERE */ </div> };
}
Hope this helps.

fetching mysql data with multi color with php

how can i fetch data from mysql with php and display it in mutliple color....like
[gray]first post
[white]second post
[gray]third post
[white]forth post
i know mysql_fetch_array but how can i display data with multi color like above
You have to loop over your lines, knowing if you are on an "even" or "odd" line (at least, if you want two colors), which can be calculated using the modulo operator.
For instance, as a quick idea, the following code :
$arr = array(
'first',
'second',
'third',
'fourth',
);
$i = 0;
foreach ($arr as $line) {
$class = ($i%2 ? 'odd' : 'even');
echo '<div class="' . $class . '">' . htmlspecialchars($line) . '</div>' . "\n";
$i++;
}
Will give this HTML output :
<div class="even">first</div>
<div class="odd">second</div>
<div class="even">third</div>
<div class="odd">fourth</div>
And now, up to you to configure the two .odd and .even CSS classes to get the colors you want.
There are many ways to do this (most of them more sophisticated than this example) but you're seemingly looking for a simple solution.
for($c=true; false!==($row=mysql_fetch_array($result, MYSQL_ASSOC)); $c=!$c) {
$class = $c ? 'even':'odd';
echo '<div class="', $class, '">', $row['x'], '</div>', "\n";
}
$c ? 'even':'odd'; tests whether $c is true or false and the result is 'even' if it is true and 'odd' if it is false. I.e. $class is either 'even' or 'odd' depending on the value of $c.
The for-loop starts with $c=true (initialization part of the for-loop). At the end of each iteration of the loop $c=!$c is executed which flips $c from true->false or false->true, i.e. $c alternates between true and false.
You can also use javascript to let the client add the coloring as an enhancement.
E.g. using jquery:
<html>
<head>
<style type="text/css">
tr.even td { background-color: red; }
tr.odd td { background-color: blue; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#t1 tr:odd').addClass('even');
$('#t1 tr:even').addClass('odd');
});
</script>
</head>
<body>
<table id="t1">
<tr><td>a</td><td>A</td></tr>
<tr><td>b</td><td>B</td></tr>
<tr><td>c</td><td>C</td></tr>
<tr><td>d</td><td>D</td></tr>
<tr><td>e</td><td>E</td></tr>
</table>
</body>
</html>
Itay Moav's and Pascal MARTIN's examples work, but if you have a numeric array you can simplify the code slightly:
foreach ($rows as $i => $row){
$even_odd = ($i%2) ? 'even' : 'odd';
echo "<tr class='$even_odd'>......";
}
In the loop
$i=0;
foreach ($rows as $row){
echo '<tr class="color'.($i%2).'">.........';
$i++;
}
in the css
.color1{ background-color: white}
.color0{ background-color: silver}

Categories