PHP / MySQL - Create array of distinct values, query db table for data associated with those values, and loop for each -
i'm not of php programmer, hope can me this. i'm trying distinct values competitor column, create array of them, retrieve share1-share12 values each of distinct values based on number of variables, , output competitors , share1-12 values.
below format of data table along mess of code i've been cobbling together:
state|bigcat|competitor|metric|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) competitor $table"; $result = $dbc->query($q) or die("unable execute query<br />" . $dbc->errno . "<br />" . $dbc->error); $r = $result->fetch_array(); $competitors = array(); { $competitors[] = $r[0]; } while ($r = $result->fetch_array()); echo $competitors; foreach($competitors $competitor){ $q = "select * $table state = '$state' && bigcat = '$cat' && product = '$product' && metric = '$metric' && competitor = '$competitor'"; $result = $dbc->query($q) or die("unable execute 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++; } ?>
i don't quite understand i'm guessing want output of competitors , shares outputting last competitor? if way fix put echoes in foreach loop instead of creating while loop.
<?php $product = $_get['product']; $cat = $_get['cat']; $state = $_get['state']; $metric = $_get['metric']; $table = $product ."_specs_states"; $q = " select distinct(competitor) competitor $table"; $result = $dbc->query($q) or die("unable execute query<br />" . $dbc->errno . "<br />" . $dbc->error); $r = $result->fetch_array(); $competitors = array(); { $competitors[] = $r[0]; } while ($r = $result->fetch_array()); echo $competitors; foreach($competitors $competitor){ $q = "select * $table state = '$state' && bigcat = '$cat' && product = '$product' && metric = '$metric' && competitor = '$competitor'"; $result = $dbc->query($q) or die("unable execute 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 ?>
Comments
Post a Comment