News:

Wondering if this will always be free?  See why free is better.

Main Menu

Help me with my homework ;D

Started by Jade Elizabeth, June 04, 2014, 10:24:58 AM

Previous topic - Next topic

Jade Elizabeth

So I'm learning PHP and I have been given this monstrosity.

$uni = get_uni_scores();

function get_uni_scores() {
$uni = array();
$uni['university_name'] = "Melbourne University";
$uni['teaching_score'] = 89.0;
$uni['int_outlook_score'] = 12;
$uni['ind_income_score'] = 89;
$uni['research_score'] = 13;
$uni['citations_score'] = 89;

// Overall score
$uni['overall_score'] = ($uni['teaching_score'] + $uni['int_outlook_score'] + $uni['ind_income_score'] + $uni['research_score'] + $uni['citations_score']);

return $uni;
}


On a technicality I did the calculation part ha ha. I can't change it to be in a DB because that's in the next part but I want to use a foreach loop so I don't have to do 100 lines of <li> crap :). Anyway I have got it to show the values but I was hoping to get the variables too so I can do little headings/classes based on it (like <li class="over-score">).

Can someone please help me figure it out? :)
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Dragooon

Okay so you have a single row of information, foreach would only get you one row, and one li. How do you plan on storing multiple rows of information or different universities?

ziycon

Do a search for key value pairs, this should point you in the right direction.

Edit: Also look at multi-dimensional arrays for storing numerous universities until you can use a database to store data, crude for now but it works.

Jade Elizabeth

Quote from: Dragooon on June 04, 2014, 10:30:23 AM
Okay so you have a single row of information, foreach would only get you one row, and one li. How do you plan on storing multiple rows of information or different universities?

I don't. I have that array, that's what I am allowed to work with (no database, only one university). I have a database for part two of my assignment which I will be doing MVC style ha ha.

Quote from: ziycon on June 04, 2014, 10:31:55 AM
Do a search for key value pairs, this should point you in the right direction.

But it's not set out like that sort of array...will that still work?
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Dragooon

So, you have that one dimensional array and you can only use that? You need a 2 dimensional array to be applicable for a foreach loop, when you are storing multiple universities. A 2-D array is basically a array in which each element is also a array, in your case it would mean each element stores a university's data. If you only have one university, you can straight away use that for displaying data without the need of a loop.

Jade Elizabeth

Okay I will look that up. Give me like 10 mins.
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

ziycon

If your going to store multiple universities, you would be using a 2D array (like Dragooon said) as your question currently stands. You can use nested foreach loops to output key/value pairs on a 2D array.

This will give you the general idea of how the foreach loop can be used with key/value pairs:
http://www.php.net/manual/en/control-structures.foreach.php

Jade Elizabeth

Okay how do I do the calculation part?

$uni = array(
                array(
                      university_name => "Melbourne University",
                      teaching_score => 89.0,
                      int_outlook_score => 12,
                      ind_income_score => 89,
                      research_score => 13,
                      citations_score => 89,
                      overall_score => ,
                    ),
             );


Also is there a way to make "ind_income_score" be "Income Score" when output?
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Jade Elizabeth

Here's what I need to end up with....


<?php
foreach ($uni as $u)
{
echo '
<li class="[key]">
<span class="topic">[title]</span>
<div class="percentage-container">
<div class="percentage overall-score" style="width: [u]">
<span class="fig">[u]</span>
</div>
</div>
</li>'
;
}
?>



Where [title] is [key] but nicer (University Name)
Where [key] is the variables (university_name)
Where [u] is the value (Melbourne University)
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

kat

You sure you won't marry me, Lizzy? ;)

Dragooon

Ah that's simpler then, you need to use key => value loop, like

foreach ($uni as $key => $value)

Now for the labels what you can do is what SMF does, make an array with each key having their nice text label similar to

$labels = array(
    
'university_name' => 'University Name',
);

...and so on and then use $labels[$key] where you need to display the text label.

Jade Elizabeth

Quote from: K@ on June 04, 2014, 05:14:08 PM
You sure you won't marry me, Lizzy? ;)

Oh no you're too late! :P

I just got engaged to my boyfriend Michael ha ha.


Quote from: Dragooon on June 04, 2014, 05:16:38 PM
Ah that's simpler then, you need to use key => value loop, like

foreach ($uni as $key => $value)

Now for the labels what you can do is what SMF does, make an array with each key having their nice text label similar to

$labels = array(
    
'university_name' => 'University Name',
);

...and so on and then use $labels[$key] where you need to display the text label.

Ohhh that is such a good idea. Then I can have labels, unis :D.

Thanks so much!
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

kat


Jade Elizabeth

So I have to use the same code as above (in first post) but re-write it into a "new array" I can call for use in this pie graph:

https://google-developers.appspot.com/chart/interactive/docs/quick_start

The step I am stuck on is LITERALLY "since the data for the pie is not coming directly from the $uni array you will need to build a seperate array in the uni.php file to send the correct data to the view"

I don't understand what that says at all. I've been through their guide. Without doing it for me can someone explain in idiots terms or send me in the right direction please :D?
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

Advertisement: