Page 1 of 1

Who's got a good head on their shoulders?

Posted: Mon Sep 18, 2006 1:23 pm
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>";
  }

Posted: Mon Sep 18, 2006 1:36 pm
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?

ok.

Posted: Mon Sep 18, 2006 1:38 pm
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

Posted: Mon Sep 18, 2006 2:49 pm
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/>";
}

Posted: Mon Sep 18, 2006 2:58 pm
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>";

?>

Posted: Mon Sep 18, 2006 3:11 pm
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,

Posted: Mon Sep 18, 2006 3:15 pm
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.

Posted: Mon Sep 18, 2006 4:50 pm
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.

Posted: Mon Sep 18, 2006 5:09 pm
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.