0% found this document useful (0 votes)
32 views

Lab 4 Working With Arrays

The document describes exercises for working with arrays in PHP. It includes instructions for creating a folder called Lab4, developing applications for a tuna cafe and calculating drive distances, and an exercise for allowing users to sort array data using different sort methods by selecting a radio button option. The user is given sample code to modify so that the sorting application runs in a single PHP file, displaying the sorted or unsorted array values based on the selected radio button option.

Uploaded by

Lê Bảo Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lab 4 Working With Arrays

The document describes exercises for working with arrays in PHP. It includes instructions for creating a folder called Lab4, developing applications for a tuna cafe and calculating drive distances, and an exercise for allowing users to sort array data using different sort methods by selecting a radio button option. The user is given sample code to modify so that the sorting application runs in a single PHP file, displaying the sorted or unsorted array values based on the selected radio button option.

Uploaded by

Lê Bảo Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab

4. Working with Arrays


Prepared: TrangNTT

Lab 4. Working with Arrays ........................................................................................................... 1

4.1. Open workspace and Create Folder .................................................................................... 2

4.2. Tuna Café............................................................................................................................ 3

4.3. Drive Distance..................................................................................................................... 5

4.4. Exercise 1 - User Sorting...................................................................................................... 6

4.5. Exercise 2 – File list ................................................................ Error! Bookmark not defined.


4.1. Open workspace and Create Folder

Step 1. Open Zend Studio 7.0


• Select Start à All Programs à Zend Studio – 7.0.0 à Zend Studio – 7.0.0

• Choose OK to confirm the workspace.

• Close all the opening file in the PHP Editor view by right click to a file and choose Close All.

Step 2. Right click on LabProject, choose New Folder. Enter Lab4 as folder name.
From now, put all files in this lab to Lab4 folder.
4.2. Tuna Café

Step 1. TunaCafe.php

Step 2. TunaResults.php
Step 3. Run
4.3. Drive Distance

Step 1. DriveDistance.php

Step 2. CheckDistance.php
Step 3. Run

Step 4. Note that although the user selects 3 destination, the result only calculate
for the last destination. Modify CheckDistance.php to calculate and
display information for all destinations, each destination is located in a
row of a table (the table has 5 columns: No., Destination, Distance, Driving
time (60 mph), Walking time (5 mph)).

4.4. Exercise - User Sorting


Given following suggestion code.

<?php

function user_sort($a, $b) {


// smarts is all-important, so sort it first
if($b == 'smarts') {
return 1;
}
else if($a == 'smarts') {
return -1;
}

return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);


}
$values = array('name' => 'Buzz Lightyear',
'email_address' => '[email protected]',
'age' => 32,
'smarts' => 'some');

if($submitted) {
if($sort_type == 'usort' || $sort_type == 'uksort' || $sort_type == 'uasort') {
$sort_type($values, 'user_sort');
}
else {
$sort_type($values);
}
}
?>

<form action="UserSorting.php" method="post">


<p>
<input type="radio" name="sort_type" value="sort" checked="checked" />
Standard sort<br />
<input type="radio" name="sort_type" value="rsort" /> Reverse sort<br />
<input type="radio" name="sort_type" value="usort" /> User-defined sort<br />
<input type="radio" name="sort_type" value="ksort" /> Key sort<br />
<input type="radio" name="sort_type" value="krsort" /> Reverse key sort<br />
<input type="radio" name="sort_type" value="uksort" /> User-defined key sort<br
/>
<input type="radio" name="sort_type" value="asort" /> Value sort<br />
<input type="radio" name="sort_type" value="arsort" /> Reverse value sort<br />
<input type="radio" name="sort_type" value="uasort" /> User-defined value
sort<br />
</p>

<p align="center">
<input type="submit" value="Sort" name="submitted" />
</p>

<p>
Values <?= $submitted ? "sorted by $sort_type" : "unsorted"; ?>:
</p>

<ul>
<?php
foreach($values as $key=>$value) {
echo "<li><b>$key</b>: $value</li>";
}
?>
</ul>
</form>
Modify to run it in one PHP file allowing users to sort the data as they choose in the radio button.

The following result is the example:

You might also like