Who's got a good head on their shoulders?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Who's got a good head on their shoulders?

Post by impulse() »

I was hoping to write some code to display the results of a poll but I want a string printed before each one, depending on which has the higher value. Prepare yourself for this code because it's a bit of a head twister:

Code: Select all

if ($totalCplus > $totalJava && $totalCplus > $totalPhp) { 
  echo "C++ leads the vote with: <b>$totalCplus<br></b>"; }
  if ($totalJava > $totalPhp ) {
    echo "Java is in second place with:<b> $totalJava<br></b>";
    echo "PHP lacks behind with:<b>$totalPhp<br></b>"; }
  else {
    echo "PHP is in second place with:<b> $totalPhp<br></b>";
    echo "Java lacks behind with:<b> $totalJava<br></b>";
  }

 


if ($totalJava > $totalCplus && $totalJava > $totalPhp) {  
  echo "Java leads the vote with: <b>$totalJava<br></b>"; }
  if ($totalCplus > $totalPhp) {
    echo "C++ is in second place with:<b> $totalCplus<br></b>";
    echo "PHP lacks behind with:<b>$totalPhp<br></b>"; }
  else {
    echo "PHP is in second place with:<b> $totalPhp<br></b>";
    echo "C++ lacks behind with:<b>$totalCplus<br></b>";
  }




if ($totalPhp > $totalJava && $totalPhp > $totalCplus) { 
  echo "PHP leads the vote with: <b>$totalPhp<br></b>"; }
  if ($totalJava > $totalPhp) {
    echo "Java is in second place with:<b> $totalJava<br></b>";
    echo "C++ lacks behind with:<b> $totalCplus<br></b>"; }
  else {
    echo "Cplus is in second place with:<b> $totalCplus<br></b>";
    echo "Java llacks behind with:<b> $totalJava<br></b>";
  }
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Why don't you put the variables in an array with keys as the programming languages, sort the array and tehn echo the key and its value with the text. Would look neater.

What was your question by the way?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok.

Post by akimm »

For large if statements, it seems for functional to use either a

Code: Select all

switch ("a") {
case 0:
   echo "0";
   break;
case "a": // never reached because "a" is already matched with 0
   echo "a";
   break;
or...

check out
http://us2.php.net/manual/en/language.operators.php
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

andym01480 wrote:Why don't you put the variables in an array with keys as the programming languages, sort the array and tehn echo the key and its value with the text. Would look neater.

What was your question by the way?

Code: Select all

$allVotes = array (
   'php'=>500000000000000000000,
   'java' => 12,
   'cpp' =>5000
);

asort($allVotes);

foreach ($allVotes as $language=>$votes) {
   echo "The Language {$language} received a total of {$votes} votes.<br/>";
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

An example of sorting with array_multisort() ..

Code: Select all

<?php

	$totalC = 20;
	$totalJava = 40;
	$totalPHP = 10;

	$languages[] = array('title'=>"C++",'total'=>$totalC);
	$languages[] = array('title'=>"Java",'total'=>$totalJava);
	$languages[] = array('title'=>"PHP",'total'=>$totalPHP);

	foreach ($languages as $key => $row) {
	   $title[$key] = $row['title'];
	   $total[$key] = $row['total'];
	}

	array_multisort($total, SORT_DESC, SORT_NUMERIC, $title, SORT_ASC, $languages);	
	
	echo "First place:".$languages[0]['title']."<br>";
	echo "Second place:".$languages[1]['title']."<br>";
	echo "Third place:".$languages[2]['title']."<br>";

?>
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

In regards to your post Onion2k, so it makes more sense to me - Would I need to add

Code: Select all

for ($i = 0; $i <2; $i++) {
echo $languages[$i]['total'];}
For it to work completely or havn't I saw the depth of your code yet?


Ste,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

What I've posted is just an example to get you started. If you want to make any changes then I suggest you tinker with the script and see what happens. You'll learn much more that way than if I just tell you.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Am I correct in thinking that's a 2-D array you've used in your example? I havn't jumped too far into 2Ds but I know it's something I'll be facing in the future.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

It's an array of associative arrays. That's then broken down into 2 seperate associative arrays with indexes that are the same for each element. Then those are fed into array_multisort() which rebuilds the original array of associative arrays nicely sorted into the right order.
Post Reply