douaoj0994 2011-01-25 20:41
浏览 35
已采纳

PHP / MySQL - 创建不同值的数组,查询与这些值关联的数据的db表,并为每个值循环

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条回答 默认 最新

  • douruduan8812 2011-01-25 20:52
    关注

    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
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)