Page 1 of 1

[SOLVED] Counting in an array

Posted: Tue Jun 29, 2004 3:54 pm
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!

Posted: Tue Jun 29, 2004 4:10 pm
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'

Posted: Tue Jun 29, 2004 5:48 pm
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];
?>

Posted: Tue Jun 29, 2004 7:50 pm
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?

Posted: Tue Jun 29, 2004 7:52 pm
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..

Posted: Wed Jun 30, 2004 9:12 am
by brewmiser
I used the code redmonkey gave me and it seems to do the trick. Thanks again for your help.