Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Implementing Arrays
 
Article Index
Implementing Arrays
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
 
Listing 2.16. Sorting an Array-Based Table Using asort()
<HTML>
<HEAD><TITLE>Sorting Arrays</TITLE></HEAD>
<BODY>
<CENTER>
<H2>Sorting Arrays using the <code>asort()</code> function</H2>
<?php

$petshack['name'] = array('Rosco', 'Icky', 'Rex', 'Buster', 'Delta');
$petshack['owner'] = array('John', 'Ann', 'Cliff', 'Amy', 'Hollie');
$petshack['weight'] = array(20, 10, 3, 54, 30);
$petshack['animal'] = array('Dog', 'Cat', 'Iguana', 'Dog', 'Dog');

/* Sort the weights */
asort($petshack['weight'], SORT_NUMERIC);

?>
<TABLE>
<TR>
<TD>Pet Name</TD>
<TD>Pet Owner</TD>
<TD>Pet Weight</TD>
<TD>Type of Animal</TD>
</TR>
<?php

foreach($petshack['weight'] as $key=>$weight) {
echo "<TR>";
echo "<TD>{$petshack['name'][$key]}</TD><TD>{$petshack['owner'][$key]} </TD>";
echo "<TD>{$weight}</TD><TD>{$petshack['animal'][$key]}</TD>";
echo "</TR>";
}

?>
</TABLE>
</CENTER>
</BODY>
</HTML>

By changing the array passed to the asort() function (and also changing the foreach() function to loop through the array that had been sorted), this script can be modified with little difficulty to sort based on any of the four columns.

Using Arrays as a Lookup Table

Now that you have an idea of how to use arrays to develop a simple table and how array tables can be designed to provide for the maximum flexibility when dealing with sorting, let's take a look at a different type of tablethe lookup table. Unlike the tables discussed in the previous section of this chapter, a lookup table is not designed to be displayed to the user. Rather, it can be described as a reference table created and used by the PHP script to make a script more efficient or simplify a task. In this section, you'll be looking at a simple application of a lookup table. Specifically, you'll see how to implement a lookup table to create cryptograms (a puzzle game).


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Your Ad Here
Natural 246
Statistic


Last Post

 
Top! Top!