Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I could use some help with this problem for my html class. NOTE: PUT THE PRINTIN

ID: 3753788 • Letter: I

Question

I could use some help with this problem for my html class.

NOTE: PUT THE PRINTING OF THE TABLE IN A FUNCTION!!! Then call the function as many times as necessary to complete the tasks below.

Create a PHP file

Part I

a.) Declare a one dimensional associative array that has at least 8 key/element pairs.

b ) Print out the array in a table, printing out keys and values (use html as you did in Lab 1).

c.) Print out the sorted array in table form, printing out keys and values.

d.) unset the second element in the sorted array (use the unset function, look it up on php.net)

e.) Print out the reverse sorted array in table form, printing out keys and values.

f.) Print out the array sorted by KEY value in table form, printing out keys and values.

g.) EXTRA CREDIT: use the array_rand function to print out 3 random elements of the array. Remember that every time you refresh your page, these will be different!

Part II

Create a two dimensional array that contains the following information about twitter users: (HINT: the second dimension should be associative, with keys tweets, followers, following. These refer to the number of tweets the user tweeted, how many followers the user has and how many other users this user is following! (The first dimension can be indexed, and you can add Username to the second dimension, or you can have both dimensions be associative, where the Username points to the first person etc. In this case the key joeDoe, for example, has the value array(tweets=>20000,followers=>45,following=>8)  

Handle (Username)

Tweets

Number of Followers

Following

joeDoe

20000

45

8

JohnD

62000

123

160

computerSavy

75

1

25

myTwitterAccount

1800

15

6

a.) Print out the array sorted by tweets (ascending)

b.) Print out the Username of the user with the most  followers

c.) Print out only users who have followers > 10, sorted by followers in descending order.

d.) EXTRA CREDIT: Print out the total (sum) of all tweets for all Usernames in the table.

I am a Novice at PHP and HTML, I appreciate all help, Thank you!

Handle (Username)

Tweets

Number of Followers

Following

joeDoe

20000

45

8

JohnD

62000

123

160

computerSavy

75

1

25

myTwitterAccount

1800

15

6

Explanation / Answer

Part I:

<?php

//Function to print table

function printTable($tab) {
    $html = '<table>';
    $html .= '<tr>';

    foreach( $tab as $key=>$value){
        $html .= '<tr>';
        $html .= '<td>' . $key . '</td>';
       $html .= '<td>' . $value . '</td>';
        $html .= '</tr>';
    }

    $html .= '</table></br>';
    return $html;
}

// (a)
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43", "Kim"=>"53", "Jack"=>"23","Lisa"=>"29","Jasper"=>"61","Alex"=>"31");

// (b)

echo printTable($age);

// (c)

asort($age);

echo "The table sorted on values is <br>";

echo printTable($age);

// (d)

$keys = array_keys($age);
$key = $keys[1];
unset($age[$key]);
echo "The table after removing the 2nd element";
echo printTable($age);

// (e)

arsort($age);

echo "The reverse sorted table on values is <br>";

echo printTable($age);

// (f)

ksort($age);

echo "The table sorted on keys is <br>";

echo printTable($age);

// (g)

$random_keys=array_rand($age,3);

echo $random_keys[0]."=>". $age[$random_keys[0]]."<br>";
echo $random_keys[1]."=>". $age[$random_keys[1]]."<br>";
echo $random_keys[2]."=>". $age[$random_keys[2]];

?>

Part II:

<?php

$array = array(
    array('Username'=>'joeDoe', 'Tweets'=>'20000', 'Followers'=>'45', 'Following'=>'8'),
    array('Username'=>'JohnD', 'Tweets'=>'62000', 'Followers'=>'123', 'Following'=>'160'),
    array('Username'=>'computerSavy', 'Tweets'=>'75', 'Followers'=>'1', 'Following'=>'25'),
array('Username'=>'myTwitterAccount', 'Tweets'=>'1800', 'Followers'=>'15', 'Following'=>'6')
);

function print_table($array){
    $html = '<table>';
    $html .= '<tr>';
    foreach($array[0] as $key=>$value){
            $html .= '<th>' . htmlspecialchars($key) . '</th>';
        }
    $html .= '</tr>';

    foreach( $array as $key=>$value){
        $html .= '<tr>';
        foreach($value as $key2=>$value2){
            $html .= '<td>' . htmlspecialchars($value2) . '</td>';
        }
        $html .= '</tr>';
    }

    $html .= '</table>';
    return $html;
}

// (a)
   foreach ($array as $key => $row) {
       $Tweets[$key] = $row['Tweets'];
       $Followers[$key] = $row['Followers'];
   }

   $Tweets = array_column($array, 'Tweets');

   array_multisort($Tweets, SORT_ASC, $array);
   echo print_table($array);

   // (b)
   $Followers = array_column($array, 'Followers');
   $max_Followers = max($Followers);

   foreach($array as $key => $value){
       if ($value['Followers'] === $max_Followers){
           echo $value['Username']. " has maximum number of followers";
           break;
       }
   }

   //(c)
   $new = array_filter($array, function ($var){
       return ($var['Followers'] > '10');
   });
   $Followers = array_column($new, 'Followers');
   array_multisort($Followers, SORT_DESC, $new);

   echo 'Users with more than 10 followers </br>';
   foreach($new as $key => $value){
           echo $value['Username']. '</br>';
   }

   // (d)
   $sum = 0;
   foreach ($array as $item) {
       $sum += $item['Tweets'];
   }
   echo '</br>The sum of all tweets for all username is '.$sum;

   ?>

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote