shaddf Posted July 9, 2017 Share Posted July 9, 2017 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;} } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 How about actually learning SQL (especially normalization) and then doing it with the database system like you're supposed to? 1 Quote Link to comment Share on other sites More sharing options...
shaddf Posted July 9, 2017 Author Share Posted July 9, 2017 How about actually learning SQL (especially normalization) and then doing it with the database system like you're supposed to? I already did that and the issue is just how to compare the data and get the biggest one Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 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. Quote Link to comment Share on other sites More sharing options...
shaddf Posted July 9, 2017 Author Share Posted July 9, 2017 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(). Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 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(*). Quote Link to comment Share on other sites More sharing options...
shaddf Posted July 9, 2017 Author Share Posted July 9, 2017 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); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 9, 2017 Share Posted July 9, 2017 Cool. Then ask Google for help from now on. Because I don't think any of us is interested in 20 new threads titled “Plz fix my completely messed up application”. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.