PHP help

For help and advice about coding

Moderator: Tech Moderators

User avatar
Chuck Steak
Posts: 5317
Joined: Mon Mar 19, 2007 11:06 am
Tag: poo

PHP help

Postby Chuck Steak » Wed Jul 22, 2009 9:12 pm

First off, I'm a noob so some or all of the following code may well be nooby.

I have a data file like so:

Code: Select all

row1col1||row1col2||row1col3||row1col4
row2col1||row2col2||row2col3||row2col4
row3col1||row3col2||row3col3||row3col4


which I want converted into a table and displayed, the relevant section of code is as follows but all it does is make a table with the right number of rows and columns but no data in any of the cells: (have to ignore the crap about colour for the moment, unless I've done it wrong.

Code: Select all

<?php
//extract data from file
$Lines = file("data.txt");

foreach($Lines as $Key => $Val)
{
   //explode that data into a new array: 
   $Data[$Key] = explode("||", $Val);
}

//process data

echo "<table border=\"1\">";

for($R = 0; $R < sizeof($Lines); $R++)
{

if($data[$R][1]==1000){

         $colour = "#000000";

   }else if($data[$R][1]==100){

   $colour = "#000000";

   }else if($data[$R][1]==10){     

   $colour = "#000000";

   }else{
   $colour = "#FFFFFF";

   }

echo "<tr>";

for($C = 0; $C < 4; $C++)
{
echo "<td bgcolor=\"$colour\"> <p>";
echo $data[$R][$C];
echo "</p> </td>";
}

echo "</tr>";
}

echo "</table>";
?>
It might look like I'm doing nothing, but at the cellular level I'm really quite busy.

User avatar
nomis
Site Admin
Posts: 1412
Joined: Sun Mar 18, 2007 4:50 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: PHP help

Postby nomis » Wed Jul 22, 2009 9:42 pm

Code: Select all

<?php
//extract data from file
$Lines = file("data.txt");

echo '<table>';
foreach($Lines as $Val)
{
   echo '<tr>';
   $data = explode("||", $Val);
   foreach($data as $value){
      echo '<td>' . $value . '</td>';
    }
   echo '</tr>';
}
echo '</table>';
?>


Does this work?

User avatar
Chuck Steak
Posts: 5317
Joined: Mon Mar 19, 2007 11:06 am
Tag: poo

Re: PHP help

Postby Chuck Steak » Wed Jul 22, 2009 10:12 pm

Yes.

Thanks
It might look like I'm doing nothing, but at the cellular level I'm really quite busy.

User avatar
Chuck Steak
Posts: 5317
Joined: Mon Mar 19, 2007 11:06 am
Tag: poo

Re: PHP help

Postby Chuck Steak » Wed Jul 22, 2009 10:31 pm

Although actually it wont work for what I want to do because I want it to colour code each row based only on the numbers that are going to be in column 2

Alternatively I'd like it to colour code every cell in column 2 based on the value it contains.

Either way that doesn't seem possible with your version of the code.
It might look like I'm doing nothing, but at the cellular level I'm really quite busy.

User avatar
Blak
Tech Moderator
Posts: 1759
Joined: Mon Mar 19, 2007 12:07 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: PHP help

Postby Blak » Wed Jul 22, 2009 11:10 pm

Code: Select all

<?php
//extract data from file
$Lines = file("data.txt");

echo '<table>';
foreach ($Lines as $Val) {
  echo '<tr>';
  $data = explode("||", $Val);

  if ($data[1] >= 1000) { $colour = "#f00"; } //red
  elseif ($data[1] >= 100) { $colour = "#0f0"; } //green
  elseif ($data[1] >= 10) { $colour = "#00f"; } //blue
  else { $colour = "#000"; }; //black
 
 foreach ($data as $value) {
    echo "<td style='color:$colour'>" . $value . "</td>";
  }
  echo '</tr>';
}
echo '</table>';
?>


Not tested but there we go...

[edit] ficksed!

User avatar
siteguru
Posts: 2124
Joined: Mon Apr 23, 2007 7:07 pm
Tag: SIR CUNTALOT
Location: Out of view ... but I AM watching you! MWUHAHAHAHA!!
Contact:

Re: PHP help

Postby siteguru » Thu Jul 23, 2009 8:09 am

Code: Select all

<?php
//extract data from file
$Lines = file("data.txt");

echo '<table>';
foreach ($Lines as $Val) {
  echo '<tr>';
  $data = explode("||", $Val);

  if ($data[1] >= 1000) { $colour = "#f00"; } //red
  elseif ($data[1] >= 100) { $colour = "#0f0"; } //green
  elseif ($data[1] >= 10) { $colour = "#00f"; } //blue
  else { $colour = "#000"; }; //black

  foreach ($data as $value) {
    echo '<td style="color:$colour">' . $value . '</td>';
  }
  echo '</tr>';
}
echo '</table>';
?>

Ficksed. ;)

User avatar
Blak
Tech Moderator
Posts: 1759
Joined: Mon Mar 19, 2007 12:07 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: PHP help

Postby Blak » Thu Jul 23, 2009 9:59 am

siteguru wrote:

Code: Select all

<?php
...
    echo '<td style="color:$colour">' . $value . '</td>';
...
?>

Ficksed. ;)


Except the single quotes around that echo mean $colour won't be parsed ;)

Fixed mine setting $colour properly in the else statment though, ta for that :)

User avatar
siteguru
Posts: 2124
Joined: Mon Apr 23, 2007 7:07 pm
Tag: SIR CUNTALOT
Location: Out of view ... but I AM watching you! MWUHAHAHAHA!!
Contact:

Re: PHP help

Postby siteguru » Thu Jul 23, 2009 4:33 pm

I meant to take the $colour out of the quotes. :oops:

Code: Select all

    echo '<td style="color:'.$colour.'">' . $value . '</td>';

User avatar
Chuck Steak
Posts: 5317
Joined: Mon Mar 19, 2007 11:06 am
Tag: poo

Re: PHP help

Postby Chuck Steak » Fri Jul 24, 2009 7:58 pm

Cheers for that guys, works great.
It might look like I'm doing nothing, but at the cellular level I'm really quite busy.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 1 guest