Page 1 of 1

[SOLVED] Using explode() to count

Posted: Sun Aug 20, 2006 1:24 am
by tristanlee85

Code: Select all

explode=(",","12345,54321,12345,54321");
Obviously I'm using the comma to break apart the string, but how can I make it count the total break-ups and give me a number, in this case, 4?

Re: Using explode() to count

Posted: Sun Aug 20, 2006 1:28 am
by Luke
tristanlee85 wrote:

Code: Select all

explode=(",","12345,54321,12345,54321");
Obviously I'm using the comma to break apart the string, but how can I make it count the total break-ups and give me a number, in this case, 4?

Code: Select all

$amount = count(explode=(",","12345,54321,12345,54321"));

Posted: Sun Aug 20, 2006 2:31 am
by Oren
This is not tested, but maybe it'd be faster... If all you want to know is the total break-ups and you don't need to make it an array, then maybe using a regex to count the number of commas would be faster. Once you get the number of commas, add +1 to it :wink:

Posted: Sun Aug 20, 2006 9:52 am
by tristanlee85
Well, I had explode working, but even if the value was empty it would count 1, so I just made an if statement assuming that if the variable that was being count() was 0 or empty or null, then automatically make $amount 0. Works good enough.

Posted: Sun Aug 20, 2006 10:38 am
by RobertGonzalez
Works properly and works good enough are two different things. :wink:

Ninja's approach is a pretty good one. Explode() will return an array. Count() will count the array. The one thing to keep in mind in that if your string contains extra commas, your numbers maybe off, so you are probably going to want to check the string first to make sure that the string does not start or end with a comma.

Posted: Sun Aug 20, 2006 10:42 am
by tristanlee85
True. I'm pulling information from a database so there may or not be any information that's being pulled.

Code: Select all

$misloads = "0";
$amount = count(explode=(",",$misloads));
That will count 1 when I really want it to count 0. That's why I did that IF statement.

Posted: Sun Aug 20, 2006 10:48 am
by RobertGonzalez
If you are pulling from a database why not use the num_rows function for that database?

mysql_num_rows() if you are using mysql.

Posted: Sun Aug 20, 2006 11:14 am
by feyd
substr_count() may be of interest too.

Posted: Sun Aug 20, 2006 11:57 am
by tristanlee85
Everah wrote:If you are pulling from a database why not use the num_rows function for that database?

mysql_num_rows() if you are using mysql.

Code: Select all

if (isset($_POST['get'])) { //START POST IF

$getdate = $_POST['date'];
$query="SELECT * FROM misloads where date = '$getdate'";
$result=mysql_query($query);
$num=mysql_numrows($result);

$query1="SELECT * FROM load_factor where date = '$getdate'";
$result1=mysql_query($query1);
$num1=mysql_numrows($result1);

if ($num == 0 || $num1 == 0)
{
	echo "No results in the database.";
}
else
{
$count=0;
while ($count < $num && $count < $num1) {

$employee=mysql_result($result,$count,"loader");
$door_number=mysql_result($result,$count,"door_num");
$misloads=mysql_result($result,$count,"misload_zip");
$start=mysql_result($result1,$count,"start_percent");
$end=mysql_result($result1,$count,"end_percent");
$trailers=mysql_result($result1,$count,"trailers");
$total=mysql_result($result1,$count,"total");
//$date=mysql_result($result,$count,"date");

if (!empty($misloads)) 
	{
	$total_misloads = explode(",",$misloads);
	$total_misloads = count($total_misloads);
	}
else
	{
	$total_misloads = 0;
	}

Posted: Sun Aug 20, 2006 5:45 pm
by RobertGonzalez
Ok, so does this work for you?

Posted: Sun Aug 20, 2006 10:29 pm
by tristanlee85
Yeh, except for if the value is null or 0, but that's why I have the IF statement.

Posted: Sun Aug 20, 2006 10:43 pm
by RobertGonzalez
Excellent. Maybe you can edit your original post title and add '[SOLVED] ' to the front of it.