PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
tristanlee85
Forum Contributor
Posts: 172 Joined: Fri Dec 19, 2003 7:28 am
Post
by tristanlee85 » Sun Aug 20, 2006 1:24 am
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?
Last edited by
tristanlee85 on Sun Aug 20, 2006 11:08 pm, edited 1 time in total.
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Sun Aug 20, 2006 1:28 am
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"));
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Sun Aug 20, 2006 2:31 am
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
tristanlee85
Forum Contributor
Posts: 172 Joined: Fri Dec 19, 2003 7:28 am
Post
by tristanlee85 » Sun Aug 20, 2006 9:52 am
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sun Aug 20, 2006 10:38 am
Works properly and works good enough are two different things.
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.
tristanlee85
Forum Contributor
Posts: 172 Joined: Fri Dec 19, 2003 7:28 am
Post
by tristanlee85 » Sun Aug 20, 2006 10:42 am
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sun Aug 20, 2006 10:48 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Aug 20, 2006 11:14 am
substr_count() may be of interest too.
tristanlee85
Forum Contributor
Posts: 172 Joined: Fri Dec 19, 2003 7:28 am
Post
by tristanlee85 » Sun Aug 20, 2006 11:57 am
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;
}
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sun Aug 20, 2006 5:45 pm
Ok, so does this work for you?
tristanlee85
Forum Contributor
Posts: 172 Joined: Fri Dec 19, 2003 7:28 am
Post
by tristanlee85 » Sun Aug 20, 2006 10:29 pm
Yeh, except for if the value is null or 0, but that's why I have the IF statement.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Sun Aug 20, 2006 10:43 pm
Excellent. Maybe you can edit your original post title and add '[SOLVED] ' to the front of it.