Jump to content

[SOLVED] count total number of elements in a array


gere06

Recommended Posts

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?

<?
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

<?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']);

?>

<?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

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

 

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

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.