Jump to content

How to display 3 rows, 8 columns, 24 total orded by time correctly?


slyte33

Recommended Posts

What I am trying to do is display 'drug' price changes for my online rpg game.

I have a cron that runs once every 15 minutes, and the time when the cron runs is inserted into the DB.

I have a for loop that looks like this:

 

if ($_GET['chart'])
{
$drugprices=$db->execute("select * from drugs_pricechange where name='".$_GET['chart']."' order by time desc limit 24");
echo "<table width=\"540\" bgcolor=\"666666\" cellspacing=\"1\">";
echo "<tr>";
echo "<td class=\"dark\" colspan=\"3\" align=\"center\"><b>Drug price changes for ".$_GET['chart']."</td>";
echo "</tr>";
for($i=1;$dp=$drugprices->fetchrow();$i++)
{
echo "<tr>";
echo "<td>";
echo "<b>";
echo date('G:i', $dp['time']);
echo "</b>";
echo " | $".$dp['newprice']."";
echo " | ";
if ($dp['sign']=="+"){$color="green";}else{$color="red";}
echo "<font color=\"$color>\"";
echo "".$dp['sign']."";
echo "$".$dp['pchange']."";
echo "<br></font>";
if ($i % 8 == 0){echo "<td class=\"light\">";}
}
die;
}

 

As you can see, this would not work. Every 8 "$i" that is looped would start a new row.

Although it does not work unless i put <tr><td> right after the for loop starts.(which results in 1 row, 24 columns)

 

If you understand the code above you can see what I'm asking, kinda.

 

I want 3 rows, 8 columns with the time the cron ran to be displayed like this:

 

1|9|17
2|10|18
3|11|19
4|12|20
5|13|21
6|14|22
7|15|23
8|16|24

 

The only way I can get this to work is by using <tr> instead which would result in this:

 

1|2|3
4|5|6
7|8|9
10|11|12
13|14|15
16|17|18
19|20|21
22|23|24

 

 

All help would me much appreciated, thank you ! :)

Link to comment
Share on other sites

Not tested, so may be some synta errors:

 

<?php

//Config variables
$max_rows    = 8;
$max_cols = 3;
//End config variables

function outputRecord($record)
{
    if (!is_array($record)) { return false; }
    $color = ($record['sign']=="+") ? "green" : "red";
    $date  = date('G:i', $record['time']);
    $tdOutput  = "<td><b>{$date}</b> | ${$record['newprice']} | ";
    $tdOutput .= "<span style=\"color:{$color};\">{$record['sign']}${$record['pchange']}</span><br>";
    $tdOutput .= "</td>\n";
    return $tdOuptu;
}

if ($_GET['chart'])
{
    $total_records = $max_rows * $max_cols;
    $chart = mysql_real_escape_string($_GET['chart']);
    $query = "SELECT * FOM drugs_pricechange WHERE name='{$chart}' ORDER BY time DESC LIMIT {$total_records}";
    $drugprices=$db->execute($query);

    //Dump restults into temporary array
    $records = array();
    while($dp=$drugprices->fetchrow())
    {
        $records[] = $dp;
    }

    //Ouput the results into columns
    $tableData = '';
    $rows = floor(count($records)/3);
    for ($row=0; $row<$rows; $row++)
    {
        $tableData .= "<tr>\n";
        for ($col=0; $col<$max_cols; $col++)
        {
            $tableData .= outputRecord($record[$i+($col*$rows)]);
        }
        $tableData .= "<tr>\n";
    }
}
?>
<table width=\"540\" bgcolor=\"666666\" cellspacing=\"1\">
  <tr>
    <th class=\"dark\" colspan=\"3\">Drug price changes for <?php echo $_GET['chart']; ?></th>
  </tr>
  <?php echo $tableData; ?>
</table>

Link to comment
Share on other sites

Not tested, so may be some synta errors:

 

<?php

//Config variables
$max_rows    = 8;
$max_cols = 3;
//End config variables

function outputRecord($record)
{
    if (!is_array($record)) { return false; }
    $color = ($record['sign']=="+") ? "green" : "red";
    $date  = date('G:i', $record['time']);
    $tdOutput  = "<td><b>{$date}</b> | ${$record['newprice']} | ";
    $tdOutput .= "<span style=\"color:{$color};\">{$record['sign']}${$record['pchange']}</span><br>";
    $tdOutput .= "</td>\n";
    return $tdOuptu;
}

if ($_GET['chart'])
{
    $total_records = $max_rows * $max_cols;
    $chart = mysql_real_escape_string($_GET['chart']);
    $query = "SELECT * FOM drugs_pricechange WHERE name='{$chart}' ORDER BY time DESC LIMIT {$total_records}";
    $drugprices=$db->execute($query);

    //Dump restults into temporary array
    $records = array();
    while($dp=$drugprices->fetchrow())
    {
        $records[] = $dp;
    }

    //Ouput the results into columns
    $tableData = '';
    $rows = floor(count($records)/3);
    for ($row=0; $row<$rows; $row++)
    {
        $tableData .= "<tr>\n";
        for ($col=0; $col<$max_cols; $col++)
        {
            $tableData .= outputRecord($record[$i+($col*$rows)]);
        }
        $tableData .= "<tr>\n";
    }
}
?>
<table width=\"540\" bgcolor=\"666666\" cellspacing=\"1\">
  <tr>
    <th class=\"dark\" colspan=\"3\">Drug price changes for <?php echo $_GET['chart']; ?></th>
  </tr>
  <?php echo $tableData; ?>
</table>

 

Hi Mjdamato, thank you for your time and reply to my question, it seems you really spend some time and work into helping us out :).

When I try using this, it displays "Drug price changes for" in green on the <td>, but it doesn't display the $_GET['chart']

 

Am I doing something wrong?

Ok i figured out what was wrong, but now I can't get it to display the rows and such.

It will only show this:

 

<table width=\"540\" bgcolor=\"666666\" cellspacing=\"1\">
  <tr>
    <td class=\"dark\" colspan=\"3\">Drug price changes for <?php echo $_GET['chart']; ?></td>
  </tr>
  <?php echo $tableData; ?>
</table>

Link to comment
Share on other sites

Oh hell! It's the little things that screws you up. Change that line to this

outputRecord($record[$i+($col*$rows)]);

 

Ok, I double failed. Forgot to make the change. Doh! That line should be echo'd like this:

echo outputRecord($record[$i+($col*$rows)]);

 

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.