Jump to content

Recommended Posts

Hi there,

 

I'm stepping in the deep end here for a first post. Being a newbie and all. Here goes however.

 

I have a form where someone can compare the stats of one troop with another and see the difference. As an example below I've kept it to just two troops to be compared.

 

 

Table name: Troops

Columns:

Name: Stat1: Stat2: Stat3

Medtronic 12 20 40

Tank 20 10 34

 

In the form the user selects Medtronic and Tank and wishes to know the differences in stats.

 

So I have the $Result being Medtronic and Tank but am lost in comparing.

 

Should I after the form have defined arrays?

 

Such as $Medtronic = "[1],[2]" , "[1],[3]" , "[1],[4]" ,

Such as $Tank = "[2],[2]" , "[2],[3]" , "[2],[4]" ,

 

But how do I compare each of these results? I understand a if, else, else if but how to apply this?

 

End result should be like this:

 

You have selected Medtronic VS Tank:

Medtronic Stat1 is 8 weaker thank Tank

Medtronic stat2 is 10 stronger thank Tank

Medtronic stat3 is 6 stronger thank Tank

 

.....

 

It is the process of comparing a user selected row from a drop down to another row that has me confused.

 

Any help would be greatly appreciated.

 

Thank you heaps

Edited by Check202
Link to comment
https://forums.phpfreaks.com/topic/288426-compare-a-row-selected-by-a-form/
Share on other sites

This should work for you.

<?php
$mediatronic = "12 20 40";
$tank        = "20 10 34";
$number  = -1;
$m_array = array();
$t_array = array();
$m_array = explode(" ", $mediatronic);
$t_array = explode(" ", $tank);
echo "<b> You have selected Medtronic VS Tank: </b><br />";

foreach ($m_array as $m_value) {
    ++$number;
    $show_number = $number + 1;
    if ($m_value < $t_array[$number]) {
        $amount = $t_array[$number] - $m_value;
        echo "Medtronic Stat" . $show_number . " is ". $amount . " weaker than Tank <br />";
    } elseif ($m_value > $t_array[$number]) {
        $amount = $m_value - $t_array[$number];
        echo "Medtronic Stat" . $show_number . " is ". $amount . " stronger than Tank <br />";
    } elseif ($m_value == $t_array[$number]) {
        echo "Medtronic Stat" . $show_number . " is equal to Tank <br />";
    } else {
        echo "Unable to compare. <br />";
    }
}
?>

returns:

You have selected Medtronic VS Tank:
Medtronic Stat1 is 8 weaker than Tank
Medtronic Stat2 is 10 stronger than Tank
Medtronic Stat3 is 6 stronger than Tank

Edited by QuickOldCar
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.