tags:

views:

76

answers:

3

This is a simple rating system. There are 3 values which contain a percentage with an accompanying description/name. I only want to display the the description/name.

For some reason, it displays:

A

T

I

This is my script:

if(isset($_POST['analyze'])) {

    // get total value of "Time Management" section
    for($i = 0; $i < 19; $i++) {
        $time_management_total += $_POST["time_management_Q$i"];
    }


    // get total value of "Attract Clients" section
    for($i = 0; $i < 16; $i++) {
        $attract_clients_total += $_POST["attract_clients_Q$i"];
    }

    // get total value of "Internet Marketing" section
    for($i = 0; $i < 21; $i++) {
        $internet_marketing_total += $_POST["internet_marketing_Q$i"];
    }


    // calculate user's personal "Time Management" score
    $time_management_rating = ($time_management_total / 19) / 5;

    // calculate user's personal "Attract Clients" score
    $attract_clients_rating = ($attract_clients_total / 16) / 5;

    // calculate user's personal "Internet Marketing" score
    $internet_marketing_rating = ($internet_marketing_total / 21) / 5;


    // add user ratings to array
    $user_rating[0] = array('percent' => $time_management_rating, 'description' => 'Time Management');
    $user_rating[1] = array('percent' => $attract_clients_rating, 'description' => 'Attract Clients');
    $user_rating[2] = array('percent' => $internet_marketing_rating, 'description' => 'Internet Marketing');


    // reverse sort the array - highest percentage first
    rsort($user_rating);


    // echo the description
    foreach($user_rating as $category) {
        foreach($category as $description) {
            echo $description[description] . "<br />";
        }
    }
}
?>
+1  A: 

Change

echo $description[description]

to

echo $description['description']
codaddict
+2  A: 

You have to do:

foreach($user_rating as $category) {
        echo $category['description'] . "<br />";
}

Explanation of your code:

Your array is like

$user_rating = array(array('percent' => ..., 'description' => ...), array(...));

That means you have a two-dimensional array.

When you do foreach($user_rating as $category), you are looping over the outer array. That means $category will be an array, namely, array('percent' => ..., 'description' => ...).

Now you make the mistake, to loop over that array again, which means that $description will always be the string value of percent and description.

In PHP you can access strings with array notation to get the character at that position. As description is not defined it probably resolves to 0 and you get the first character.

$foo = 'bar';
echo $foo[0];
// echos 'b'

By just adding some echos, you can see, which element in your loop contains which value:

$user_rating[0] = array('description' => 'Time Management');
$user_rating[1] = array('description' => 'Attract Clients');
$user_rating[2] = array('description' => 'Internet Marketing');

foreach($user_rating as $category) {
    echo '$category: ' . $category . PHP_EOL;
    foreach($category as $description) {
        echo '$description: '. $description . PHP_EOL;
        echo $description[description] . "<br />" .PHP_EOL;
    }
}

gives

$category: Array
$description: Time Management
T<br />
$category: Array
$description: Attract Clients
A<br />
$category: Array
$description: Internet Marketing
I<br />
Felix Kling
Thanks Felix, this did the trick!
Kenny Powers
A: 

try

foreach($user_rating as $category) {
            echo $category['description'] . "<br />";
    }
Yogesh