Jump to content

Recommended Posts

Hi there i have 2 queries one for each table that calculates values in one or more fields.

 

The two tables are different in structure so i have had to calculate them differently:

 

| Ship table |

Class

 

|Planet table |

Class 1

Class 2

Class 3

Class 4

 

Depending on what type of ship is added (Class 1-4 ship) its corresponding Class is determined. The planet table is different though as it can have any number of Class 1 - 4 ships. 

 

So with help ive put the Ship query results into an array and echoed the results.  The Planet query is not in an array as such as i could acheive the same results using the SUM() function. Both work fine and the outputted results look like this:

 

Ship query

1 Class 1

4 Class 2

3 Class 3

1 Class 4

 

Planet query

1 Class 1

2 Class 2

4 Class 3

13 Class 4

 

What i really need to do now is compare each queries Class and if the planets class 1-4 < ships class 1-4 then echo the difference. For example the results would be:

 

Class 1 = 0 echo no Class 1 needed

Class 2 = - 2 echo no Class 2 needed

Class 3 = +1 echo 1 Class 3 needed

Class 4 = +12 echo 12 class 4 needed

 

Im sorry if it seems longwinded or if ive not explained my self very well but im finding it difficult.

 

Heres the code:

<?php 
$colname_resultp = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_resultp = sprintf("SELECT SUM(Class1) As Class1_totla, SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text"));
$resultp = mysql_query($query_resultp, $swb) or die(mysql_error());
$row_resultp = mysql_fetch_assoc($resultp);
$totalRows_resultp = mysql_num_rows($resultp);

$colname_Recordset1 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s ORDER BY Class ASC", GetSQLValueString($colname_Recordset1, "text"));
$result = mysql_query($query, $swb) or die(mysql_error());

$number_of_results = array();
while($row = mysql_fetch_array($result)){
if (isset($number_of_results[$row['Class']]))
{
	$number_of_results[$row['Class']]++;
}
else
{
	$number_of_results[$row['Class']] = 1;
}
}

foreach ($number_of_results as $class => $number_of_class)
{
echo $number_of_class . " Class " . $class;
echo "<br />";
}

<?php do { ?>
    
    <?php 
echo $row_resultp['Class1_totla']; echo ' Class 1';
echo'<br>';
echo $row_resultp['Class2_totla']; echo ' Class 2'; echo'<br>';
echo $row_resultp['Class3_totla']; echo ' Class 3'; echo'<br>';
echo $row_resultp['Class4_totla']; echo ' Class 4'; ?>

<?php } while ($row_resultp = mysql_fetch_assoc($resultp)); 
mysql_free_result($result);
mysql_free_result($resultp); ?>

 

If you could please help that would be ace.

 

Thanks for listening. :)

 

ive not explained my self very well

 

i cant work out the class/planet/ship deal. mostly because of this statement:

 

Depending on what type of ship is added (Class 1-4 ship) its corresponding Class is determined. The planet table is different though as it can have any number of Class 1 - 4 ships. 

 

Each ship thats added into the database can be either a class 1,2 3, or 4 ship (Vbig, Big, Small, Fighter).  Each planet can hold a number of class 1, 2 ,3, 4 ships. So the main difference is that in the ship table the user enters the class of ship that it is. Which is then talliied up. Where as in the planet table the user enters how many class 1,2,3,4 ships it can have.  I hope thats what you meant.

In the ship table a single ship and its Class is entered e.g.

Ship table

|ShipID: 1|

|ShipName: big ship|

|Class: 2|

 

In the planet table a field is given to each class 1-4 ship and a user can enter of many of each class ships that the planet can have:

PlanetID: 1|

Class1: 0|

Class2: 1|

Class3:2|

Class4: 3|

 

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.