Need help with PHP homework - friend matching algorithm

I am completely new to php and started to learn it. I have two homework in php and html.

Assignment 1:

  • I have to keep the names of some people and the names of all their friends. I should list only those who have common friends. My problem is that if a person has no friends with someone else, I get the message "Rana has 0 friends with Roni. I warn this:

Assignment 2:

I have an html form to search for a person from the last php file

  • When I search for Rana, the PHP form opens and prints:

    Rana has 4 friends, and he has a mutual friend with Nandini and Mamun.

  • When I search for Tanmoy, the page will open and type:

    Tanmoy is a friend of Ranas who has 4 friends and friends with Nandini and Mamun.

  • For this I have to use the function "post / get / request"

Here is my code:

<?php
   # Function: finfCommon

function findCommon($current, $arr) {
    $cUser = $arr[$current];
    unset($arr[$current]);
    foreach ($arr As $user => $friends) {
        $common = array();
        $total = array();
        foreach ($friends As $friend) {
            if (in_array($friend, $cUser)) {
                $common[] = $friend;
            }
        }
        $total = count($common);
        $add = ($total != 1) ? 's' : '';
        $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
    }
    return implode('<br />', $final);
}

# Array of users and friends

$Friends = array(
    "Rana" => array("Pothik", "Zaman", "Tanmoy", "Ishita"),
    "Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "Ishita"),
    "Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
    "Liton" => array("Mahadi", "Pavel"),
    "Mamun" => array("Meheli", "Tarek", "Zaman")
);

# Creating the output value

$output = "<ul>";
foreach ($Friends As $user => $friends) {
    $total = count($friends);
    $common = findCommon($user, $Friends);
    $output .= "<li><u>{$user} has {$total} friends.</u><br /><strong>Friends:</strong>";
    if (is_array($friends) && !empty($friends[0])) {
        $output .= "<ul>";
        foreach ($friends As $friend) {
            $output .= "<li>{$friend}</li>";
        }
        $output .= "</ul>";
    }
    $output .= "{$common}<br /><br /></li>";
}
$output .= "</ul>";

# Printing the output value
print $output;
?>
+3
source share
2 answers

For assignment 1.1: you must filter out unanswered responses with

if ($total>0) { 
    $output .= "<li><u>{$user} has {$ ...
}

paragraph.

For the second purpose:

You need to create a page that expects the parameter to be read through $ _GET or $ _POST or $ _REQUEST (for example, name it). They are not functions, they are arrays, and you can access them from every area of ​​your program (these are superglobals, and you do not need to declare them using gobals).

I would just print the selection with famous people, if the name (for example, $ _REQUEST ['name']) is missing, just print the form, otherwise find the person indicated by this entry and print the search result.

EDIT:

, . ,

, ,

<form action="homework.php" method="get">
   Person: <select name="searchperson" />
     <option> </option>
     ...
     <option> </option>
   </select>
</form>

:

$friends = array(
  "Rana" => array("Pothik", "Zaman", "Tanmoy", "Ishita"),
  "Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "Ishita"),
  "Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
  "Liton" => array("Mahadi", "Pavel"),
  "Mamun" => array("Meheli", "Tarek", "Zaman")
);

// create a flat array with all the known peoples
$knownPeople = array_reduce(
  $friends, 
  function($current, $result) { 
    return array_merge($result, $current);
  }, 
  array_keys($friends)
);

$knownPeople = array_flip(array_flip($knownPeople)); //get rid of duplicates
asort(knownPeople);     // sort the names
$knownPeople = array_values($knownPeople); // normalize the array indexes

:

function theForm($people) {
  $options = '<option>'.join("</option>\n<option>",$people). '</option>';
  echo "
  <form action=\"homework.php\" method=\"get\">
  <h2>Chose the people you want investigate on:</h2>
  Person: <select name=\"person\" />
    $options
  </select>
  </form>"; 
}

, , :

, script homework.php( ), http://example.com. URI http://example.com/homework.php

:

  • script
  • script
  • script , .

:

. , . , .

, , php.

+3

findCommon

function findCommon($current, $arr) {
    $cUser = $arr[$current];
    unset($arr[$current]);
    foreach ($arr As $user => $friends) {
        $common = array();
        $total = array();
        foreach ($friends As $friend) {
            if (in_array($friend, $cUser)) {
                $common[] = $friend;
            }
        }
        $total = count($common);
        $add = ($total != 1) ? 's' : '';
        $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
    }
    return implode('<br />', $final);
}

function findCommon($current, $arr) {
    $cUser = $arr[$current];
    unset($arr[$current]);
    foreach ($arr As $user => $friends) {
        $common = array();
        $total = array();
        foreach ($friends As $friend) {
            if (in_array($friend, $cUser)) {
                $common[] = $friend;
            }
        }
        $total = count($common);
        $add = ($total != 1) ? 's' : '';
        if ( $total > 0 ) $final[] = "<i>{$current} has {$total} friend{$add} in common with {$user}.</i>";
    }
    return implode('<br />', $final);
}

: if ( $total > 0 ) $final[] = ..

.

<?php

function searchPerson($person, $friends){
    $direct = false;
$found = false;
    if ( in_array ($person, array_keys($friends) ) ){
        list($total, $common_friends) = commonFriend ($person, $friends);
        $direct = true;
    $found = true;
    }
    else{
        foreach ( $friends as $friend => $his_friends ){
            if ( in_array ( $person, $his_friends ) ){
                list($total, $common_friends) = commonFriend ($friend, $friends);
                $direct = false;
                    $found = true;
                $friend_person = $friend;
                break;
            }
        }
    }
    if ( !$found ) return false;
    $output = $person . " ";
    if ( $direct ){
        $output .= " has " . $total . " friends";
    }
    else{
        $output .= " is " . $friend_person . " friend who has " . $total . " friends"; 
    }

    if ( isset($common_friends[0]) ) $output .= " and common friends with " . $common_friends;

    return $output;
}

function commonFriend ($person, $friends){
    $my_friends = $friends[$person];
    unset($friends[$person]);
    $total_friends = count($my_friends);
    $common_with = array();

    foreach ( $friends as $friend => $his_friends ){
        foreach ( $my_friends as $my_friend ){
            if ( in_array ($my_friend, $his_friends) ){
                $common_with[] = $friend;
            }
        }
    }

    $common_with = array_unique ($common_with);

    $common_friends = "";
    if ( count($common_with) > 0 ){
        $common_friends = join (", ", $common_with );
    }

    return array ( $total_friends, $common_friends );
}

$friends = array(
    "Rana" => array("Pothik", "Zaman", "Tanmoy", "Ishita"),
    "Nandini" => array("Bonna", "Shakib", "Kamal", "Minhaj", "Ishita"),
    "Roni" => array("Akbar", "Anwar", "Khakan", "Pavel"),
    "Liton" => array("Mahadi", "Pavel"),
    "Mamun" => array("Meheli", "Tarek", "Zaman")
);


$person = $_GET['person'];

$output = searchPerson($person, $friends);

if ( $output === false ) print $person . " is not on the list";
else print $output;

?>

, .

, searchPerson.php

html-,

<html>
    <head>
        <title>Search for friend</title>
    </head>
    <body>
        <form action="searchPerson.php" method="get">
            Person: <input type="text" name="person" /><input type="submit" value="search" />
        </form>
    </body>
</html>

,

searchPerson.php?person=Rana

, $person = $_GET['person']; url, u , searchPerson.php, $person.

- EDIT

,

+1

Source: https://habr.com/ru/post/1785766/


All Articles