[SOLVED] Counting in an array

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

Post Reply
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

[SOLVED] Counting in an array

Post by brewmiser »

I am sure that this has been discussed in the forum somewhere before, but I can not find anything to help me.... so if you could, here is what I am trying to do.

Scenario: I have approximatly 100 monitor's that I am trying to track in our office. I want to list the monitor's from a database by CRT's and LCD's. I then want to place the information in a table that will list the number of 15" CRT's, 17" CRT's and 21" CRT''s. I then want to do the same with LCD. Along with this I want to give a total number of monitors.

First:

Code: Select all

<?php
$sqlmonitors     = "SELECT * FROM monitor";
$result  = mysql_query($sql) or die("<b>MySQL Error 1:</b> ".mysql_errno()." : ".mysql_error());
$numrows = mysql_numrows($result);
?>
Second:

Code: Select all

<?php
$x=0;
while($x<$numrows){
  $array   = mysql_fetch_assoc($result);
  while (list($indice,$crt) = each($array)){
    if ($crt=="LCD"){
      $num = count($crt);
       echo $num;
     }
  }
  $x++;
}?>
Now the problem that I am having is doing the count for each type within one large array! I have tried several different things, but nothing has worked properly, and the one above does not work either.......HELP!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about something like this:

Code: Select all

SELECT COUNT( a.type ) num, a.type, a.size FROM monitor a
LEFT JOIN monitor b ON b.type = 'LCD'
LEFT JOIN monitor c ON c.type = 'CRT'
LEFT JOIN monitor d ON d.type = 'LCD' AND d.size = '15'
LEFT JOIN monitor e ON e.type = 'LCD' AND e.size = '17'
LEFT JOIN monitor f ON e.type = 'LCD' AND f.size = '19'
LEFT JOIN monitor g ON e.type = 'LCD' AND g.size = '21'
LEFT JOIN monitor h ON d.type = 'CRT' AND h.size = '15'
LEFT JOIN monitor i ON e.type = 'CRT' AND i.size = '17'
LEFT JOIN monitor j ON e.type = 'CRT' AND j.size = '19'
LEFT JOIN monitor k ON e.type = 'CRT' AND k.size = '21'
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Or perhaps....

Code: Select all

<?php

$monitor = array();

$sqlmonitors     = "SELECT type, size FROM monitor";
$result  = mysql_query($sql) or die("<b>MySQL Error 1:</b> ".mysql_errno()." : ".mysql_error());
$numrows = mysql_numrows($result);
if ($numrows > 0)
{
  while ($row = mysql_fetch_row($result))
  {
    $monitor[$row[0]][$row[1]]++;
  }
}

$total_moinotrs = $numrows;
$lcd_15   = $monitor['LCD'][15];
$lcd_17   = $monitor['LCD'][17];
$lcd_19   = $monitor['LCD'][19];
$lcd_21   = $monitor['LCD'][21];
$crt_15   = $monitor['CRT'][15];
$crt_17   = $monitor['CRT'][17];
$crt_19   = $monitor['CRT'][19];
$crt_21   = $monitor['CRT'][21];
?>
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

I thought that you could count the number of times that the word "LCD" would occur in the array. I could never get it to work. You guys are saying to use more of mysql type statement?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can use either way.. my way uses mysql to do it.. and will need some hand holding to add more in the future.. whereas redmonkey's uses PHP to post process and count all the results..
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

I used the code redmonkey gave me and it seems to do the trick. Thanks again for your help.
Post Reply