gere06 Posted July 13, 2008 Share Posted July 13, 2008 <? include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $trophies=mysql_query("SELECT userid,var FROM playertrophy WHERE userid=1 "); $trophies=count($var); echo"$trophies"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/ Share on other sites More sharing options...
gere06 Posted July 13, 2008 Author Share Posted July 13, 2008 What i want to to do is count all the elements in the array and get a total so if the array is (1,2,3,4,5,6) i want to add them up and get 6 but all it does is give me a 0 Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589150 Share on other sites More sharing options...
unsider Posted July 13, 2008 Share Posted July 13, 2008 like this? Just configure your SQL query to fit the code. <?php // sizeof function $value= array(2,5,6,8,9); echo 'size of array = " .sizeof($value). "'; // Output = 5 ?> Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589151 Share on other sites More sharing options...
gere06 Posted July 14, 2008 Author Share Posted July 14, 2008 all that is doing is giving me a 1 this is what is in the val{4,2,3,5,6,7,8 } for user id 1 i want to add it up and get a total so in this case i want a 7 Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589155 Share on other sites More sharing options...
unsider Posted July 14, 2008 Share Posted July 14, 2008 I apologize I didn't test that piece of code, I just wrote it, rather quickly at that. Here is another method. This is where it came from. http://us.php.net/count <?php $food = array('cat' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive count echo count($food, COUNT_RECURSIVE); // output 8 // normal count echo count($food); // output 2 ?> May I also see the code you used that gave you the '1' output? Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589161 Share on other sites More sharing options...
gere06 Posted July 14, 2008 Author Share Posted July 14, 2008 <? include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $tro=mysql_query("SELECT userid,val FROM playertrophy WHERE userid=1"); while(list($userid,$val)=mysql_fetch_row($tro)){ echo count($val); } ?> what i got now still geting 1 Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589166 Share on other sites More sharing options...
AndyB Posted July 14, 2008 Share Posted July 14, 2008 <?php include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $query = "SELECT userid,var FROM playertrophy WHERE userid=1 "; $result=mysql_query($query) or die("Error: ". mysql_error. " with query ". $query); $stuff = mysql_fetch_array($result); echo count($stuff['var']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589167 Share on other sites More sharing options...
gere06 Posted July 14, 2008 Author Share Posted July 14, 2008 <?php include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $query = "SELECT userid,var FROM playertrophy WHERE userid=1 "; $result=mysql_query($query) or die("Error: ". mysql_error. " with query ". $query); $stuff = mysql_fetch_array($result); echo count($stuff['var']); ?> still just gives me a 1 Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589168 Share on other sites More sharing options...
gere06 Posted July 14, 2008 Author Share Posted July 14, 2008 any one got anything Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589208 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 If the field contains a comma separated list, you need to use the explode() function on it to get an array and then count the elements in that array: <?php include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $query = "SELECT userid,var FROM playertrophy WHERE userid=1 "; $result=mysql_query($query) or die("Error: ". mysql_error. " with query ". $query); $stuff = mysql_fetch_array($result); echo count(explode(',',$stuff['var'])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589252 Share on other sites More sharing options...
gere06 Posted July 14, 2008 Author Share Posted July 14, 2008 If the field contains a comma separated list, you need to use the explode() function on it to get an array and then count the elements in that array: <?php include("/home/content/g/e/r/gereb/html/ps3t/config22.php"); $query = "SELECT userid,var FROM playertrophy WHERE userid=1 "; $result=mysql_query($query) or die("Error: ". mysql_error. " with query ". $query); $stuff = mysql_fetch_array($result); echo count(explode(',',$stuff['var'])); ?> Ken thank you i ben looking all day and could not find a way to get it to work but this worked thanks Quote Link to comment https://forums.phpfreaks.com/topic/114581-solved-count-total-number-of-elements-in-a-array/#findComment-589293 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.