Log in

View Full Version : Resolved PHP Change Table BG Color



M2com
07-29-2011, 11:57 PM
How can I change the background color of a table without having to reload the page? Thanks in advanced!

PS: I'm placing the change BG color after a sleep(); function.

JShor
07-30-2011, 12:34 AM
Be more specific.

M2com
07-30-2011, 12:37 AM
Well all I need is a way to change the background of a table without having to constantly reload the page. I don't know how to be any more specific.

Please be more specific on what you need to me to be more specific on. :)

Thanks for your time!

JShor
07-30-2011, 12:51 AM
Do you mean that the background of the table would be changed dynamically? This could be accomplished in JavaScript.

JShor
07-30-2011, 12:52 AM
For example, you would click a button and it would change the background of the table. If that's what you're getting at, it can be accomplished. Also, do you have any code?

M2com
07-30-2011, 12:55 AM
Hmm, I was hoping not to use Javascript but if that's the best then we could use it. Here is a portion of my code:


sleep(10);
$myFile = "../$id2/status.txt";
$timestamp = date("l, F d, Y h:i" ,time());
$fh = fopen($myFile, 'w') or die("
Data: 133484354354236253690528");
$stringData = "Hacked";
fwrite($fh, $stringData);
fclose($fh);//Here is where you will set your path for entry
$myFile = "../$id2/status2.txt";
$timestamp = date("l, F d, Y h:i" ,time());
$fh = fopen($myFile, 'w') or die("
Data: 133484354354236253690528");

$stringData = "Hacked";
fwrite($fh, $stringData);
fclose($fh);//Here is where you will set your path for entry
$fp=fopen("credits.txt","r"); //Opens the Pass.txt file
$data=fgets($fp); //Reads the File entirely
fclose($fp); //Closes the file since we already got our info

$filename= "credits.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
fclose($fd) ;

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 50 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;

// This is where I would place the background color change.




It would change this table:





<table width='400' border='1' bgcolor='$bg2'>
<tr>
<th scope='col'>ID #1: $id1</th>
</tr>
</table>

JShor
07-30-2011, 01:07 AM
PHP is server-side, so changing the table's background color dynamically is not possible once the page is loaded.

It is possible to change it client-side using JavaScript, but it sounds like you want a more robust solution to that.

Also, in your PHP code, nothing is being printed out -- it's only files being read/modified/closed. Where exactly is your table being created?

M2com
07-30-2011, 01:21 AM
My tables are being created above the .TXT reading and editing script.

JShor
07-30-2011, 01:32 AM
The PHP compiler compiles code in descending order (so code created in the beginning cannot be manipulated at the end).

Here is a jQuery solution that will dynamically change the background color of a table whose ID is named "table_id".



<script type="text/javascript">
$(document).ready(function() {
$("#table_id").css("background-color", "<?php echo $bg2; ?>");
});
</script>


... and your table should look something like:


<table width='400' border='1' id='table_id'>
<tr>
<th scope='col'>ID #1: $id1</th>
</tr>
</table>


Remember to include the jQuery library in your code. If you don't want to use jQuery, I could give you another solution, but it would be much longer, complex and convoluted.

M2com
07-31-2011, 01:31 AM
Thanks so much for your help! But for some reason the script you gave me isn't working. Should I show you my current code?

Thanks so much again!

EDIT: I think I fixed it now! Thanks so much for your help! Though only one thing is that it changes the color of the tables after it's done with the whole process not during it (real time). If there is a way of fixing that, that would be nice, otherwise it's fine.

JShor
07-31-2011, 02:37 AM
Well, the delay happens because the script waits for the page to finish loading (the document.ready state) before changing the color of the table.

You could work around this by inserting the script on the next line that the table is made.

So you would have something like this:


<table width='400' border='1' id='table_id'>
<tr>
<th scope='col'>ID #1: $id1</th>
</tr>
</table>
<script type="text/javascript">document.getElementById('table_id').style.backgroundColor = '<?php echo $bg2; ?>';
</script>


And, while it wouldn't completely eliminate a delay, it would come very close to it.

bluewalrus
07-31-2011, 05:19 AM
Why not just do this with css?

JShor
07-31-2011, 05:07 PM
You could, but calling CSS outside of the <head> tags is generally not a good idea (and causes problems in some browsers).

bluewalrus
08-01-2011, 04:15 AM
The css can be in the head, are the text files building the whole page, or just parts of it? I don't see any html in the example provided, seeing the whole page layout would be the easiest.

for example



<?php
//php stuff here
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<?php
echo $define_stuff;
?>
</head>
<body>
<h1>Welcome</h1>
<?php
echo $php_generated_content;
?>
</body>
</html>

JShor
08-01-2011, 04:23 AM
But he's already defining the bgcolor attribute when the table is being created, which would overwrite the CSS value.

bluewalrus
08-01-2011, 04:44 AM
Yea, but he doesn't need to define it there he could do it conditionally in the css with the php.

djr33
08-01-2011, 05:59 PM
I agree with bluewalrus. This is why pre-processing a page then doing the output is best. If that's not possible, it's at least best to do any setup like this (that is-- checking what kind of format you will need) and to be able to output some special (PHP-generated, dynamic, conditional) CSS at the top.

It may take redesigning the page some but it's much better in the end. Your choice.

M2com
08-03-2011, 12:31 AM
Thanks guys for your help!