Jump to content

formatting error


tachioma
 Share

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
Edited by tachioma
Link to comment
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
Share on other sites

This thread is more than a year old.

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.

 Share

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