Array to Table

Trying to understand how to get an array to html coding.

I have this for the array…

[php]
class dataTable{
private $data = array(
array(‘name’=>‘Mr. Jon Jones’,‘state’=>‘NV’,‘company’=>‘123 Construction’,
‘phone’=>array(
‘cell’=>‘5552223333’,
‘office’=>‘5552220001’
),
‘email’=>array(
‘primary’=>‘[email protected]’,
[email protected]
)
),
array(‘name’=>‘Bree Young’,‘city’=>‘Tucson’,‘phone’=>‘5553331111’,‘email’=>‘[email protected]’),
array(‘name’=>‘Jason Bourne’,‘city’=>‘New York City’,‘company’=>‘Bourne Electricity’,‘email’=>array(‘[email protected]’,‘personal’=>‘[email protected]’),‘phone’=>‘6021110000’)
);
}
[/php]

and this as the out going source for the HMTL table…

NAME COMPANY CITY STATE EMAIL PHONE
Mr. Jon Jones 123 Construction   NV primary: [email protected]
[email protected]
cell: (555) 222-3333
office: (555) 222-0001
Bree Young   Tucson   [email protected] (555) 333-1111
Jason Bourne Bourne Electricity New York City   [email protected]
personal: [email protected]
(602) 111-0000

Can someone lead me to a tutorial to help me out with this and I can go from there? Or perhaps just write up a mock code that’ll get me started?

In my opinion I think you should setup your array a little bit better:

Maybe something like this:
[php]
private $data = array(
‘John Jones’ => array(
‘name’ => ‘John Jones’,
‘company’ => ‘Widget Company’,
‘address’ => ‘1313 Mockingbird Lane’,
‘city’ => ‘New York City’,
‘state’ => ‘New York’,
‘zipCode’ => ‘13134’
),
‘Willy Blates’ => array(
‘name’ => ‘Willy Blates’,
‘company’ => ‘Micro Silly’,
‘address’ => ‘1 Washington Drive’,
‘city’ => ‘Silly City’,
‘state’ => ‘Washington’,
‘zipCode’ => ‘99000’
)
);
[/php]

I create a script on how to get the information out of a class, but I didn’t stick it in a table. I figure that you could figure on your own. I would also suggest finding tutorials on Object-Orient Programing either from a book ( I like Larry Ullman’s “Advance PHP and Object-Oriented Programming”) or a current online tutorial.

Here’s is the whole script:
[php] <?php
class DataTable {
private $data = array(
‘John Jones’ => array(
‘name’ => ‘John Jones’,
‘company’ => ‘Widget Company’,
‘address’ => ‘1313 Mockingbird Lane’,
‘city’ => ‘New York City’,
‘state’ => ‘New York’,
‘zipCode’ => ‘13134’
),
‘Willy Blates’ => array(
‘name’ => ‘Willy Blates’,
‘company’ => ‘Micro Silly’,
‘address’ => ‘1 Washington Drive’,
‘city’ => ‘Silly City’,
‘state’ => ‘Washington’,
‘zipCode’ => ‘99000’
)
);
public $addressBook = array();

	public function getAllRecords() {
		
		return $this->addressBook = $this->data;
	}

	public function getIndividualAddress($name){
		
		foreach ($this->data as $nameSearch => $value) {
			
			if ($name == $nameSearch) {
					return $value;
			}
		}

	}

}

/* Create a new instance /
$myAddressBook = new DataTable();
/
Grab all the Address /
$addresses = $myAddressBook->getAllRecords();
/
Search for a specific address */
$addressBook = $myAddressBook->getIndividualAddress(‘John Jones’);

?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<title>Untitled Document</title>
</head>

<body>

All Addresses

<?php foreach ($addresses as $key => $value ) { echo ''; echo $value['name'] . '
'; echo $value['address'] . '
'; echo $value['city'] . ', ' . $value['state'] . ' ' . $value['zipCode']; echo '

'; } ?>

Individual Search of John Jones

<?php echo $addressBook['name'] . '
'; echo $addressBook['address'] . '
'; echo $addressBook['city'] . ', ' . $addressBook['state'] . ' ' . $addressBook['zipCode']; ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service