New at PHP, for college I got a skeleton with a basic assignment: I have to make an assosciate array with 3 people and their age, I then have to loop that array (foreach) and make an HTML anchor/link for every key. Every anchor/link influences what is between the if (isset($_GET['name'])
This is the associate array ($age)
$age['Atticus'] ="2100";
$age['McDunna'] ="96";
$age['Oberon'] ="13";
What can I change/add in this "loop" so that they influences what is between the if (isset($_GET['name'])
foreach ($age as $key => $value) {
echo "<a href=\"GET\">'$key'</a>";
echo "<br>";
I have also concidered to let the array create a form from each key so that I can use form method =get but I'm not too sure this is possible.
This is my first question, so I am sorry if some parts are confusing, I will gladly clear something up. If it is easier I can provide the skeleton-code:
<?php
// TODO make an assoc array with 3 people and their age.;
if( isset( $_GET['name']) ){
// TODO create a text with the name and age;
$infoText= "$age";
$infoText = NULL;
}else{
// TODO create generic text.;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<?php
// TODO "Loop" the $age array. and for every key create an HTML anchor/link.;
foreach ($age as $key => $value) {
echo "<a href=\"GET\">'$key'</a>";
echo "<br>";
}
?>
</header>
<h3><?php // TODO display the infoText ?></h3>
</body>
Replace this:
<?php
// TODO make an assoc array with 3 people and their age.;
$age['Atticus'] = 2100;
$age['McDunna'] = 96;
$age['Oberon'] = 13;
$infoText = 'Not selected';
if (isset( $_GET['name']) ){
$name = $_GET['name'];
if(array_key_exists($name, $age)) {
$infoText = 'Name: ' .$name . ' Age: '. $age[$name]; // Name: John Age: 27
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<?php
// TODO "Loop" the $age array. and for every key create an HTML anchor/link.;
foreach ($age as $key => $value) {
echo "<a href=\"?name=".$key."\">".$key."</a>";
echo "<br>";
}
?>
</header>
<h3><?php echo $infoText; ?></h3>
</body>