我想从三个表中获取数据,每个表都与id链接

时间:2014-09-19 06:52:59

标签: php mysql

I have three tables name product, brand, product_info

    **product table**
    CREATE TABLE IF NOT EXISTS `product` (
  `p_id` int(11) NOT NULL AUTO_INCREMENT,
  `product` varchar(50) NOT NULL,
  `flag` int(11) NOT NULL,
  PRIMARY KEY (`p_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;





  INSERT INTO `product` (`p_id`, `product`, `flag`) VALUES
    (1, 'Atta', 0),
    (2, 'Oil', 0),
    (3, 'Biscut', 0),
    (4, 'Rice', 0);

    Brand table
    CREATE TABLE IF NOT EXISTS `brand` (
  `b_id` int(11) NOT NULL AUTO_INCREMENT,
  `p_id` int(11) NOT NULL,
  `brand` varchar(50) NOT NULL,
  `image` varchar(255) NOT NULL,
  PRIMARY KEY (`b_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;



INSERT INTO `brand` (`b_id`, `p_id`, `brand`, `image`) VALUES
(1, 1, 'Ashirvad', 'FreeVector-Blue-Squares-Vector.jpg'),
(2, 1, 'Phillsberry', 'ILBAGNOALESSI_One_02.jpg'),
(3, 2, 'Sunflower', '001-bi-fold-corporate-brochure-template-vol-1-2.jpg'),
(4, 3, 'Good Day', 'but-ok-bg.gif'),
(5, 3, 'Sunfeast', 'but_call_bg.gif'),
(6, 4, 'Mosura', '2.jpg'),
(7, 4, 'Samba', '3.jpg');

    **Product_info table**

    CREATE TABLE IF NOT EXISTS `product_info` (
  `pi_id` int(11) NOT NULL AUTO_INCREMENT,
  `pro` int(11) NOT NULL,
  `b_id` int(11) NOT NULL,
  `quantity` varchar(20) NOT NULL,
  `measurement` varchar(20) NOT NULL,
  `mrp` varchar(20) NOT NULL,
  `our_price` varchar(20) NOT NULL,
  PRIMARY KEY (`pi_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;



INSERT INTO `product_info` (`pi_id`, `pro`, `b_id`, `quantity`, `measurement`, `mrp`, `our_price`) VALUES
(1, 1, 1, '1', '1kg', '50', '48'),
(2, 1, 2, '1', '1kg', '60', '59'),
(3, 2, 3, '1', '1ltr', '90', '86'),
(4, 1, 1, '500', '500gms', '25', '23'),
(5, 2, 3, '500', '500ml', '45', '43'),
(6, 3, 4, '1', 'pcs', '15', '14'),
(7, 3, 5, '1', 'pcs', '10', '9'),
(8, 4, 6, '', '1 kg', '45', '43'),
(9, 4, 6, '', '500 gm', '23', '21'),
(10, 4, 7, '', '1 kg', '48', '47'),
(11, 4, 7, '', '500 gm', '24', '23');

         I used the below code to list the items from product table according to p_id

    $result = mysql_query("select * FROM product"); 
                    echo '<ul class="list-1 p2">'; 
                    while($row = mysql_fetch_array( $result )) {
                        // Print out the contents of each row into a table

                        echo '<li>';
                        echo "<a href='page.1.php?pid=$row[p_id]'>";
                        echo '<b>';
                        echo $row['product'];
                        echo '</b>';
                        echo "</a>";
                        echo '</li>';
                    } 
                    echo '</ul>';
                    mysql_close($con);


     **My intention is to display as in the below code**

     mysql_select_db("mr_bazaar",$con);
                    $pid=$_GET['pid'];
                    $result = mysql_query("SELECT p.product,b.brand,pi.quantity,pi.mrp,pi.our_price,pi.measurement FROM product as p INNER JOIN brand as b ON p.p_id=b.p_id
INNER JOIN product_info as pi ON pi.b_id=b.b_id WHERE p.p_id=$pid"); 
                    echo '<ul class="list-1 p2">'; 
                    while($row = mysql_fetch_array( $result )) {
                        // Print out the contents of each row into a table

                       echo '<tr>';
                        echo '<td>';
                        echo $row['brand'];
                        echo '</td><td>';
                        echo '<select name= qty>';
                        echo '<option value= 1>1</option>
                              <option value= 2>2</option>
                              <option value= 3>3</option>
                              <option value= 4>4</option>
                              <option value= 5>5</option>
                              <option value= 6>6</option>
                              <option value= 7>7</option>
                              <option value= 8>8</option>
                              <option value= 9>9</option>
                              <option value= 10>10</option>';
                        echo '</select>';

                        echo '</td><td>';   
                        echo $row['mrp'];
                        echo '</td><td>';
                        echo $row['our_price'];                     
                        echo '</td><td>';               
                        echo "<select name='measurement'>";
                                                while ($row = mysql_fetch_array($result)) {
                                                    $id = $row['p_id'];
                                                    $measurement = $row['measurement'];
                                                    echo "<option value=\"" . $row['p_id'] . "\">" . $row['measurement'] . "</option>";
                                                }
                                                echo "</select>";

                        echo '</td><td>';




                        echo '</td>';
                    } 
                    echo '</ul>';
                    mysql_close($con);

但我想显示名单,例如品牌数量价格我们的价格计量(根据表格的下拉列表中的值)。当我使用上面的代码时,它只显示一个项目!根据图像,它只显示Ashirvad,但在Phillsberry表中也有 enter image description here

1 个答案:

答案 0 :(得分:0)

你的第一个while循环总是只有一次迭代,因为你有一个内部while循环,它也在第一个while循环中使用相同的变量进行迭代。

因此,如果您在示例中给出了它返回的3条记录,那么您的第一个while循环将其迭代一个显示1条记录,而内部while循环迭代2再次显示测量结果。

因此,在这种情况下,非常直接的解决方案是不要在内部while循环中使用相同的变量进行迭代,并执行单独的查询以获得该品牌的所有度量。请检查下面的代码,我做了一些更正只是为了显示不同的产品/品牌列表。您可以根据产品显示要求更改逻辑。

$pid = $_GET['pid'];
$result = mysql_query("SELECT p.product,b.brand,pi.quantity,pi.mrp,pi.our_price,group_concat(pi.measurement) as measurement
                    FROM product as p 
                    INNER JOIN brand as b ON p.p_id=b.p_id
                    INNER JOIN product_info as pi ON pi.b_id=b.b_id 
                    WHERE p.p_id=$pid 
                    GROUP BY b.b_id");

echo '<ul class="list-1 p2">';
if(mysql_num_rows($result)) { // if result found
    echo "<table border=1 cellspacing=0>";
    while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table

       echo '<tr>';
        echo '<td>';
        echo $row['brand'];
        echo '</td><td>';
        echo '<select name= qty>';
        echo '<option value= 1>1</option>
              <option value= 2>2</option>
              <option value= 3>3</option>
              <option value= 4>4</option>
              <option value= 5>5</option>
              <option value= 6>6</option>
              <option value= 7>7</option>
              <option value= 8>8</option>
              <option value= 9>9</option>
              <option value= 10>10</option>';
        echo '</select>';

        echo '</td><td>';   
        echo $row['mrp'];
        echo '</td><td>';
        echo $row['our_price'];                     
        echo '</td><td>';

        $measurement = explode(',', $row['measurement']); // to get different measurements for a brand 

        echo "<select name='measurement'>";
        foreach ($measurement as $measure) {
            echo $measure."<br/>";

            echo "<option value=\"" . $row['p_id'] . "\">" . $measure . "</option>";
        }
        echo "</select>";

        echo '</td><td>';

        echo '</tr>';
    } 
    echo "</table>";
} else {
    echo "No result found";
}
echo '</ul>';