0

I'm not much of a PHP programmer, so I hope someone can help me with this. What I'm trying to do is get the distinct values from the competitor column, create an array of them, retrieve the share1-share12 values for each of those distinct values based on a number of variables, and output the competitors and their share1-12 values.

Below is the format of my data table along with the 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) 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++;
      }
    ?>
  • 1
    Usual warnings about escaping your mysql queries properly...unsanitised GET variables a very bad idea! – BrynJ Jan 25 '11 at 20:52
  • @Kid: rather than escaping input, you should be using [prepared statements](http://www.php.net/PDO.prepared-statements), though that won't help with the injection vulnerability in the table name. According to the relational model, use a separate table to store data in a one-to-many relationship (where ) rather than using an arbitrary number of columns (share1-12), then join the tables. Use a join rather than two separate statements. Don't use [`SELECT *`](http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select); select only the columns you need. – outis Sep 01 '11 at 20:58
  • ... If you find yourself naming variables with a common prefix and a numeric suffix, use an array instead. [`
    `](http://brainstormsandraves.com/articles/semantics/structure/#br) isn't [semantic](http://webstyleguide.com/wsg3/5-site-structure/2-semantic-markup.html); use something more appropriate, such as a paragraph or [list](http://www.w3.org/TR/html401/struct/lists.html) element. Don't use [`or die`](http://www.phpfreaks.com/blog/or-die-must-die) when outputting HTML. You'll get invalid HTML.
    – outis Sep 01 '11 at 21:00
  • ... If you're asking for help on a problem, describe the problem. In particular, describe what you want to happen and what actually happens, including any error messages. – outis Sep 01 '11 at 21:05
  • ... Having a separate specs_states table for each product seems a poor design, as it makes joining tables much harder. Tables should represent objects (in terms of their properties) and the relationships between the objects. – outis Sep 01 '11 at 21:09

1 Answers1

0

I don't quite understand completely but I'm guessing you want to output all of the competitors and their shares but this is only outputting the last competitor? If so the way to fix that would be to put your echoes in your foreach loop instead of creating another while loop.

<?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

?>
jb1785
  • 722
  • 1
  • 7
  • 19
  • Thanks jbile! That does output all of the competitors, but not their share1-12 values. Is this code correct to get those values? $r = $result->fetch_array(); $share1 = ($r[5]); $share2 = ($r[6]); $share3 = ($r[7]); $share4 = ($r[8]); $share5 = ($r[9]); $share6 = ($r[10]); etc, etc,etc... – Kid Kilowatt Jan 25 '11 at 21:08
  • It should be, however I don't know your database fields so I'm not entirely sure. Right after $r = $result->fetch_array(); do a print_r($r); and see if $r is actually filled with the values you want. – jb1785 Jan 25 '11 at 21:45
  • Dear everyone, please do not execute iterated queries -- this is unnecessary strain on your database. – mickmackusa Jul 21 '21 at 13:46