Jump to content

getting biggest field value


shaddf

Recommended Posts

i have this table:

+---------+-------+----------------+
| Snem    | Nem   | lge            |
+---------+-------+----------------+
| 2016/17 | Mwase | GBS CUP,NDIV V |
| 2015/16 | Mwase | GBS CUP        |
+---------+-------+----------------+
2 rows in set, 2 warnings (0.01 sec)

how can i get the field with the longest or biggest string in the lge column using php?
i have tried this so far by array comparison but failed:

$length=array();
			//loop through the rows of resultset
			for($j=0;$j<count($obj->mPlayerstatistics);$j++){ 
			// get  each string into an array
		   $arr_lge1=explode(',',$obj->mPlayerstatistics[$j]['lge']);
				//get array length
			$length=count($arr_lge1);
				//do comparison with previous and store the biggest length
				if((count($length)>0)&& ($length[$j-1]>$length[$j])){$max=$j;}else{$max=0;}
			}
Link to comment
Share on other sites

You already did what? If you had a normalized table and basic SQL knowledge, then we wouldn't even have this discussion. The only reason why you need those weird PHP string gymnastics is because you do not have a normalized table but some kind of Excel-style spreadsheet pretending to be an SQL table.

 

In SQL, one column contains one value. Not two values, not 10 values, not a comma-separated list of values. A single value. When you follow this simple rule, counting items becomes trivial, because you just have to COUNT(*) the rows.

Link to comment
Share on other sites

You already did what? If you had a normalized table and basic SQL knowledge, then we wouldn't even have this discussion. The only reason why you need those weird PHP string gymnastics is because you do not have a normalized table but some kind of Excel-style spreadsheet pretending to be an SQL table.

 

In SQL, one column contains one value. Not two values, not 10 values, not a comma-separated list of values. A single value. When you follow this simple rule, counting items becomes trivial, because you just have to COUNT(*) the rows.

the above table is a result set from an sql query group_concated  together.Haven;t you heard of the  group_concat().

Link to comment
Share on other sites

lolwat?

 

So you take your pretty normalized table, break the entire normalization by throwing all data into one big collection of CSVs, load the whole thing into PHP and then try to reconstruct the data you already have with the weirdest strings hacks you could come up with?

 

You're horribly, horribly confused. You've reached the point where you're fighting against MySQL rather than working with it.

 

Forget about GROUP_CONCAT() and start writing actual queries which give you the data you want. As I already said, counting rows is one of the most basic SQL features. All you need is a COUNT(*).

Link to comment
Share on other sites

lolwat?

 

So you take your pretty normalized table, break the entire normalization by throwing all data into one big collection of CSVs, load the whole thing into PHP and then try to reconstruct the data you already have with the weirdest strings hacks you could come up with?

 

You're horribly, horribly confused. You've reached the point where you're fighting against MySQL rather than working with it.

 

Forget about GROUP_CONCAT() and start writing actual queries which give you the data you want. As I already said, counting rows is one of the most basic SQL features. All you need is a COUNT(*).

thanks for you advice but i googled and got a php solution:

$length=array();
			for($j=0;$j<count($obj->mPlayerstatistics);$j++){ $arr_lge1=array(); 
		   $arr_lge1=explode(',',$obj->mPlayerstatistics[$j]['lge']);
			$length[$j]=count($arr_lge1);
			}$value = max($length);$key = array_search($value, $length);
Link to comment
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.