You are here:  » Columns and Rows


Columns and Rows

Submitted by crounauer on Wed, 2006-03-22 16:56 in

Hi David,

I am trying to split data from an xml feed into 5 columns. This is the code I have but cant seem to get the column accross.

        print "<table width='80%' border='0' cellpadding='0' cellspacing='2'>";
        print "<tr>";
        $itemCount = 0;
        foreach($item as $value){
        }
        print "<td width='20%' valign='top' bgcolor='#E4EAFC'>";
        print "<div align='center'> ";
        print "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
        print "<tr>";
        print "<td>";
        print "<div align='center'> ";
        print "".$item["COUNTRY_NAME"]."";
        print "</div>";
        print "</td>";
        print "</tr>";
        print "<tr>";
        print "<td>";
        print "</td>";
        print "</tr>";
        print "</table>";
        print "</div>";
        print "</td>";
        $itemCount++;
        if ($itemCount == 3)
        {
          print "</tr>";
print "<tr>";
$itemCount = 0;
        }
      }
      print "<tr>";
      print "</table>";

The current result can be seen at http://www.gynogapod.co.uk/stores/laterooms/index.php

Submitted by support on Wed, 2006-03-22 17:05

Hi,

It shouldn't be down to this but just very quickly - your last 2 lines should be:

  print "</tr>";
  print "</table>";

...other than that it looks OK so i'll have a go in a test script..

Submitted by support on Wed, 2006-03-22 17:17

Hiya,

Have a go with this:

<?php
  $items
[0]["COUNTRY_NAME"] = "ENGLAND";
  
$items[1]["COUNTRY_NAME"] = "IRELAND";
  
$items[2]["COUNTRY_NAME"] = "SCOTLAND";
  
$items[3]["COUNTRY_NAME"] = "WALES";
  
$items[4]["COUNTRY_NAME"] = "FRANCE";
  
$items[5]["COUNTRY_NAME"] = "GERMANY";
  print 
"<html>";
  print 
"<table width='80%' border='0' cellpadding='0' cellspacing='2'>";
  print 
"<tr>";
  
$itemCount 0;
  foreach(
$items as $item)
  {
    print 
"<td width='20%' valign='top' bgcolor='#E4EAFC'>";
    print 
"<div align='center'> ";
    print 
"<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
    print 
"<tr>";
    print 
"<td>";
    print 
"<div align='center'> ";
    print 
"".$item["COUNTRY_NAME"]."";
    print 
"</div>";
    print 
"</td>";
    print 
"</tr>";
    print 
"<tr>";
    print 
"<td>";
    print 
"</td>";
    print 
"</tr>";
    print 
"</table>";
    print 
"</div>";
    print 
"</td>";
    
$itemCount++;
    if (
$itemCount == 3)
    {
      print 
"</tr>";
      print 
"<tr>";
      
$itemCount 0;
    }
  }
  print 
"</tr>";
  print 
"</table>";
?>

Submitted by crounauer on Thu, 2006-03-23 12:03

Hi David,

Thank for your previous post.

The code works but doesn't create the country list dynamically from the xml feed

<?php
  function myRecordHandler($item)
  {
  $items[0]["COUNTRY_NAME"] = "ENGLAND";
  $items[1]["COUNTRY_NAME"] = "IRELAND";
  $items[2]["COUNTRY_NAME"] = "SCOTLAND";
  $items[3]["COUNTRY_NAME"] = "WALES";
  $items[4]["COUNTRY_NAME"] = "FRANCE";
  $items[5]["COUNTRY_NAME"] = "GERMANY";
  print "<table width='80%' border='0' cellpadding='0' cellspacing='2'>";
  print "<tr>";
  $itemCount = 0;
  foreach($items as $item)
  {
    print "<td width='20%' valign='top' bgcolor='#E4EAFC'>";
    print "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
    print "<tr>";
    print "<td>";
    print "".$item["COUNTRY_NAME"]."";
    print "</td>";
    print "</tr>";
    print "</table>";
    print "</td>";
    $itemCount++;
    if ($itemCount == 5)
    {
      print "</tr>";
      print "<tr>";
      $itemCount = 0;
    }}
  print "</tr>";
  print "</table>";
  return 1;
  }
  $url = "http://xxxxxxx.com/xml_ctry.php3?lang=eng";
  MagicParser_parse($url,"myRecordHandler","xml|CTRY_LIST/COUNTRY/");
?>

Should it no read something like this?

<?php
  function myRecordHandler($item)
  {
  print "<table width='80%' border='0' cellpadding='0' cellspacing='2'>";
  print "<tr>";
  $itemCount = 0;
  foreach($item as $value)
  {
    $value = "".$item["COUNTRY_NAME"]."";
    print "<td width='20%' valign='top' bgcolor='#E4EAFC'>";
    print "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
    print "<tr>";
    print "<td>";
    print "".$item["COUNTRY_NAME"]."";
    print "</td>";
    print "</tr>";
    print "</table>";
    print "</td>";
    $itemCount++;
    if ($itemCount == 5)
    {
      print "</tr>";
      print "<tr>";
      $itemCount = 0;
    }}
  print "</tr>";
  print "</table>";
  return 1;
  }
  $url = "http://xxxx.com/xml_ctry.php3?lang=eng";
  MagicParser_parse($url,"myRecordHandler","xml|CTRY_LIST/COUNTRY/");
?>

Submitted by support on Thu, 2006-03-23 13:19

Sorry Simon, I was just concentrating on the table part; not the feed reading. What I would do is read each country into an array first, and then build the table, something like this (might have some syntax errors as i've not test this, but it should be ok...!):

<?php
  
require("MagicParser.php");
  
// get all countries from the XML into $items array
  
$items = array();
  function 
myRecordHandler($item)
  {
    global 
$items;
    
$items[]["COUNTRY_NAME"] = $item["COUNTRY_NAME"];
  }
  
$url "http://xxxx.com/xml_ctry.php3?lang=eng";
  
MagicParser_parse($url,"myRecordHandler","xml|CTRY_LIST/COUNTRY/");
  
// generate the HTML and table as per the previous example
  
print "<html>";
  print 
"<table width='80%' border='0' cellpadding='0' cellspacing='2'>";
  print 
"<tr>";
  
$itemCount 0;
  foreach(
$items as $item)
  {
    print 
"<td width='20%' valign='top' bgcolor='#E4EAFC'>";
    print 
"<div align='center'> ";
    print 
"<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
    print 
"<tr>";
    print 
"<td>";
    print 
"<div align='center'> ";
    print 
"".$item["COUNTRY_NAME"]."";
    print 
"</div>";
    print 
"</td>";
    print 
"</tr>";
    print 
"<tr>";
    print 
"<td>";
    print 
"</td>";
    print 
"</tr>";
    print 
"</table>";
    print 
"</div>";
    print 
"</td>";
    
$itemCount++;
    if (
$itemCount == 3)
    {
      print 
"</tr>";
      print 
"<tr>";
      
$itemCount 0;
    }
  }
  print 
"</tr>";
  print 
"</table>";
?>