0

如何从以下代码中读取最新号码

<table  width="100" border="1" >
   <tr align="center" bgcolor="#999999" >
     <td  >NO</td>
     <td  >Name</td>
     <td  >PBSID</td>
   </tr>

$query ="select * from stock ";
$hasil = mysql_query($query); 
$no=0;

while ($row = mysql_fetch_array($hasil))
{ 
    $no++;
    if($x != $row[pbsid] )
    { 
        $no=1;
        echo "<tr bgcolor=#CCCCCC>
                  <td colspan=4 ><b> GROUP $row[pbsid]</b></td>
              </tr>";
    }

    $mo=count($no);
    echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
    $x = $row["pbsid"];
}

结果:


===========
NO | NAME | GROUP
---------------------------------------
===========
GROUP 1
---------------------------------------
===========
1 | A | 1    
2 | B | 1    
3 | C | 1    
4 | D | 1
---------------------------------------
===========
GROUP 2
---------------------------------------
===========
1 | A | 2    
2 | B | 2    
3 | C | 2


我想显示最新的 GROUP 编号,Group 1=4 和 Group 2=3,有人可以帮我吗?

4

4 回答 4

0

这会起作用

$query ="select * from stock ";
$hasil = mysql_query($query); 
$no=0;
$latest = array();
while ($row = mysql_fetch_array($hasil))
{ 
$no++;
if($x != $row[pbsid] ){ 
$no=1;
echo "<tr bgcolor=#CCCCCC>
  <td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$key = 'Group'.$row['pbsid'];
$latest[$key] = $no;
}
print_r($latest);
于 2012-05-22T06:41:58.807 回答
0
<table  width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td  >NO</td>
<td  >Name</td>
<td  >PBSID</td>
</tr>
$last_no = array();
$query ="select * from stock ";
$hasil = mysql_query($query); 
$no=0;

while ($row = mysql_fetch_array($hasil))
{ 
$no++;
if($x != $row[pbsid] ){ 
$no=1;
echo "<tr bgcolor=#CCCCCC>
  <td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$last_no[] = $no;
}
print_r($last_no);
于 2012-05-22T06:43:17.833 回答
0

试试这个查询:

SELECT s.*, c.count
FROM stock AS s
LEFT JOIN ( SELECT pbsid, COUNT(1) AS `count` FROM stock GROUP BY pbsid ) AS c
    ON s.pbsid = c.pbsid

现在您将获得每个组的计数,这似乎是您真正想要的。

如果您出于某种原因更喜欢在 PHP 中完成所有工作,您可以这样做:

<table  width="100" border="1" >
   <tr align="center" bgcolor="#999999" >
     <td  >NO</td>
     <td  >Name</td>
     <td  >PBSID</td>
   </tr>

$query ="select * from stock ";
$hasil = mysql_query($query); 
$no=0;

$counts = array();  // make an array of counts for each group, keyed by pbsid
$rows = array();  // put all the data from SQL into an array
while ($row = mysql_fetch_array($hasil))
{
    $rows[] = $row;
    if (!isset($counts[$row[pbsid]]))
    {
        $counts[$row[pbsid]] = 0; // initialize it to 0
    }
    $counts[$row[pbsid]]++; // increment for each occurrence of a pbsid 
}

foreach ($rows as $row)
{ 
    $no++;
    if($x != $row[pbsid] )
    { 
        $no=1;
        echo "<tr bgcolor=#CCCCCC>
                  <td colspan=4 ><b> GROUP $row[pbsid]</b> Count = " . $counts[$row[pbsid]] . " </td>
              </tr>";
    }

    $mo=count($no);
    echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
    $x = $row["pbsid"];
}

虽然,我认为在查询中计算这些信息要简单得多。

于 2012-05-22T06:50:20.620 回答
0

更改您的查询$query ="select MAX(没有),bnama, pbsid from yourtablename GROUP BY pbsid ";

于 2012-05-22T08:31:06.237 回答