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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/
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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208845
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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1208980
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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209005
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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209011
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
https://forums.phpfreaks.com/topic/235232-array-from-a-query/#findComment-1209020
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.