Jump to content

Array from a query


mrt003003

Recommended Posts

Hi there i need to make an array with the result from a query (numbers 1-4) I need to calculate how many occurances of each 1-4 number is in the table. Im new to this and havent much experience with arrays but ive managed to create an array and echo the results:

 

2 Class

3 Class

1 Class

3 Class

3 Class

2 Class

2 Class

2 Class

 

So i need to somehow Add the occurance of them and echo the results e.g. 4 Class 2, 1 Class 1, 3 Class 3.

 

If someone could please point me in the right direct that would be great.

 

$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", GetSQLValueString($colname_Recordset1, "text"));
$result = mysql_query($query, $swb) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['Class']. " Class ";
echo "<br />";
}
mysql_free_result($result);?>;

 

 

Thank You :D

Link to comment
Share on other sites

Something like this?

 

<?php
$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", 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 $value . " Class " . $class;
echo "<br />";
}
mysql_free_result($result);?>;

Link to comment
Share on other sites

Hi there thanks for the reply,

 

Ive used:

mysql_select_db($database_swb, $swb);
$query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s", GetSQLValueString($colname_Recordset1, "text"));
$result = mysql_query($query, $swb) or die(mysql_error());

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 $value . " Class " . $class;
echo "<br />";
}
mysql_free_result($result);?>;

 

Just changing the class to Class... However i get a notice error: Notice: Undefined variable: value in C:\wamp\www\SWB\test1.php on line 55. Other than that Class 2, Class 3 and Class 1 is outputted, there no Class 4 and also it doesnt say how many occurances of the Classes there are.

 

Thanks

 

 

Link to comment
Share on other sites

I guess im clucthing at straws here but if i change the line:

 echo $value . " Class " . $class;

to:

 echo $result . " Class " . $class;

 

This is whats outputted:

Resource id #4 Class 2

Resource id #4 Class 3

Resource id #4 Class 1

 

Somethings not right :(

Link to comment
Share on other sites

Hi there im not 100% sure because i cant get your Select code to work without error:

( ! ) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\SWB\test1.php on line 39

 

In the Class field of my table there are 4 occurances of 2, 3 occurances of  3 and 1 occurance of 1. I just want to output what they are and how many of them there are.

 

Is this possible?

 

Thanks

Link to comment
Share on other sites

woops sorry, i renamed a variable and forgot to rename it somewhere else

 

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

 

Should be

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

 

i initially had the foreach as $key => $value but decided they were terrible variable names

Link to comment
Share on other sites

Hi there im not 100% sure because i cant get your Select code to work without error:

( ! ) Parse error: syntax error, unexpected T_STRING in C:\wamp\www\SWB\test1.php on line 39

 

In the Class field of my table there are 4 occurances of 2, 3 occurances of  3 and 1 occurance of 1. I just want to output what they are and how many of them there are.

 

Is this possible?

 

Thanks

 

I missed a quote at the end. All you needed was to add it.

Link to comment
Share on other sites

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.