[英]MySQL selecting distinct values from a table, ordering by the count of rows with those distinct values
[英]PHP / MySQL - Create array of distinct values, query db table for data associated with those values, and loop for each
我不是一個PHP程序員,所以我希望有人可以幫助我。 我想做的是從競爭對手列中獲取不同的值,創建它們的數組,基於許多變量為每個不同的值檢索share1-share12值,然后輸出競爭對手及其share1- 12個值。
以下是我的數據表的格式以及我一起整理的一堆代碼:
狀態|大貓|競爭對手|度量標准| share1 | share2 | share3 | share4 | share5 | share6 | share7 | share8 | share9 | share10 | share11 | share12
<?php
$product = $_GET['product'];
$cat = $_GET['cat'];
$state = $_GET['state'];
$metric = $_GET['metric'];
$table = $product ."_specs_states";
$q = " SELECT distinct(competitor) as competitor from $table";
$result = $dbc->query($q) or die("unable to execute the query<br />" . $dbc->errno . "<br />" . $dbc->error);
$r = $result->fetch_array();
$competitors = array();
do {
$competitors[] = $r[0];
} while ($r = $result->fetch_array());
echo $competitors;
foreach($competitors as $competitor){
$q = "SELECT * FROM $table where state = '$state' && bigcat = '$cat' && product = '$product' && metric = '$metric' && competitor = '$competitor'";
$result = $dbc->query($q) or die("unable to execute the query<br />" . $dbc->errno . "<br />" . $dbc->error);
$r = $result->fetch_array();
$share1 = ($r[5]);
$share2 = ($r[6]);
$share3 = ($r[7]);
$share4 = ($r[8]);
$share5 = ($r[9]);
$share6 = ($r[10]);
$share7 = ($r[11]);
$share8 = ($r[12]);
$share9 = ($r[13]);
$share10 = ($r[14]);
$share11 = ($r[15]);
$share12 = ($r[16]);
}//end loop
?>
<?php
$i=1;
while($i<=#)
{
?>
<?= $competitor ?><br />
<?= $share1 ?><br />
<?= $share2 ?><br />
<?= $share3 ?><br />
<?= $share4 ?><br />
<?= $share5 ?><br />
<?= $share6 ?><br />
<?= $share7 ?><br />
<?= $share8 ?><br />
<?= $share9 ?><br />
<?= $share10 ?><br />
<?= $share11 ?><br />
<?= $share12 ?>
<?php
$i++;
}
?>
我不太了解,但我想您想輸出所有競爭對手和他們的股份,但這只是輸出最后一個競爭對手? 如果是這樣,解決該問題的方法是將您的回聲放入foreach循環中,而不是創建另一個while循環。
<?php
$product = $_GET['product'];
$cat = $_GET['cat'];
$state = $_GET['state'];
$metric = $_GET['metric'];
$table = $product ."_specs_states";
$q = " SELECT distinct(competitor) as competitor from $table";
$result = $dbc->query($q) or die("unable to execute the query<br />" . $dbc->errno . "<br />" . $dbc->error);
$r = $result->fetch_array();
$competitors = array();
do {
$competitors[] = $r[0];
} while ($r = $result->fetch_array());
echo $competitors;
foreach($competitors as $competitor){
$q = "SELECT * FROM $table where state = '$state' && bigcat = '$cat' && product = '$product' && metric = '$metric' && competitor = '$competitor'";
$result = $dbc->query($q) or die("unable to execute the query<br />" . $dbc->errno . "<br />" . $dbc->error);
$r = $result->fetch_array();
$share1 = ($r[5]);
$share2 = ($r[6]);
$share3 = ($r[7]);
$share4 = ($r[8]);
$share5 = ($r[9]);
$share6 = ($r[10]);
$share7 = ($r[11]);
$share8 = ($r[12]);
$share9 = ($r[13]);
$share10 = ($r[14]);
$share11 = ($r[15]);
$share12 = ($r[16]);
echo $competitor ."<br />";
echo $share1 ."<br />";
echo $share2 ."<br />";
echo $share3 ."<br />";
echo $share4 ."<br />";
echo $share5 ."<br />";
echo $share6 ."<br />";
echo $share7 ."<br />";
echo $share8 ."<br />";
echo $share9 ."<br />";
echo $share10 ."<br />";
echo $share11 ."<br />";
echo $share12;
}//end loop
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.