Jump to content

formatting error


tachioma

Recommended Posts

Morning all,

 

I've been writing a few pages over the last week (learning by doing) and I've been pretty successful so far. However i now have a problem that has boggled me for days and no amount of googling is helping.

 

<?php

// Define cache file
$cache_file = "cache.txt";

// Cache file is less than thirty minutes old.
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 30  ))) {

// Don't bother refreshing, just use the file as-is.
$file = file_get_contents($cache_file);

// Our cache is out-of-date, so load the data from our remote server,
// and also save it over our cache for next time.
} else {
$file = file_get_contents('http://eu.battle.net/api/wow/guild/chamber-of-aspects/requiem%20paradisum?fields=achievements');
file_put_contents($cache_file, $file, LOCK_EX);
}
// Decode cache file
$achie = json_decode($file);


// Seperate achievements and timestamps as variables
$achiachi = ($achie->achievements->achievementsCompleted);
$achitime = ($achie->achievements->achievementsCompletedTimestamp);
foreach(array_combine($achiachi, $achitime) as $f => $n){

// Combine achievement number into Link for wowhead script
$achilink = ("http://www.wowhead.com/achievement=". $f);

// Sort out time formatting
$unix = ($n);
$not_unix = $unix / 1000;
$date = date("F d Y @ h:i A", $not_unix);

//Print into table
echo "
<table align=center class=mytable cellpadding=8 style=font-family:verdana;font-size:8pt;color:white;>
<tr>
        <th>Achievement</th><th>Timestamp</th>
<tr>
      <td><a href=\"$achilink\" rel=\"item=$achiachi\"></a></td>
      <td>$date</td>
</tr>
</table>

";
}
?>

 

This does work for the most part . . .  . but . . . . .when the arrays are printed it creates a new table for every array entry.

I'm expecting : 

 

table1

header1          -         header2

achievement1 - achievement time

achievement2 - achievement time

achievement3 - achievement time

achievement4 - achievement time

 

 

I'm getting : 

 

 

table1

header1          -         header2

achievement1 - achievement time

 

 

table2

header1          -         header2

achievement2 - achievement time
 

table3

header1          -         header2

achievement3 - achievement time
 

 

table4

header1          -         header2

achievement4 - achievement time
 
 
Most annoying! If someone could tell me what the chuff I've done wrong I'd be very appreciative.
 
Thanks
 
 
 
John
Link to comment
https://forums.phpfreaks.com/topic/276468-formatting-error/
Share on other sites

The issue here is that your echo statement in the foreach loop includes the opening and closing table structure - so the opening and closing table tags are being rendered for each iteration of your array.  What you want is to only loop through your array when creating rows in the actual table.  Something like:

 

<?php $superheroes = array('Superman' => 'Clark Kent', 'Spiderman' => 'Peter Parker', 'Batman' => 'Bruce Wayne'); ?>

<table>
    <thead>
        <tr>
            <th>Superhero</th>
            <th>Secret Identity</th>
        </tr>
        <tbody>
            <?php foreach ($superheroes as $hero => $identity) : ?>
            <tr>
                <td><?php echo $hero; ?></td>
                <td><?php echo $identity; ?></td>                
            </tr>
            <?php endforeach; ?>
        </tbody>
    </thead>
</table>

 

Notice how only the table row creation falls inside the foreach loop.

Link to comment
https://forums.phpfreaks.com/topic/276468-formatting-error/#findComment-1422730
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.