Mysql JOIN我需要FOREACH吗?

时间:2014-11-30 15:06:10

标签: php mysql

这是我目前的代码:

<?php
$mysql_hostname = "localhost";
$mysql_user     = "r0rypu115";
$mysql_password = "VSVMj9Py";
$mysql_database = "r0rypu115";
$bd             = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database

//$result = mysql_query("SELECT * FROM tbl_products ORDER BY bankIds"); // selecting data through mysql_query()
$result = mysql_query("
SELECT a.id,a.bankIds,a.sku,a.productTitle,a.productTitle,a.prodDesc,a.seed,a.productImg,a.characteristics,b.bankTitle,c.productID,c.bundleQty,c.bundlePrice,c.bundleSKU,c.seed
FROM tbl_products a 
JOIN tbl_seedBank b 
ON a.bankIds=b.id
JOIN tbl_bundles c
ON a.id=c.productID
ORDER BY bankTitle");

echo'<table border=1px>';
echo'<th>Collection</th><th>Vendor</th><th>Product</th><th>SKU</th><th>Description</th><th>Charactersitics</th><th>Bundles</th><th>Reg/Fem</th><th>Image</th>';

while($data = mysql_fetch_array($result))
{
echo'<tr>';
echo'<td>'.strip_tags($data['bankTitle']).'</td>
        <td>'.strip_tags($data['bankTitle']).'</td>
        <td>'.strip_tags($data['productTitle']).'</td>
        <td>'.strip_tags($data['sku']).'</td>
        <td><h3>Description</h3>'.$data['prodDesc'].'</td>
        <td><h3>characteristics</h3>'.$data['characteristics'].'</td>
        <td>'.$data['bundleQty'].'<br/>'.$data['seed'].'<br/>'.$data['bundlePrice'].'<br/>'.$data['bundleSKU'].'</td>
        <td>'.strip_tags($data['seed']).'</td>
        <td><image width="150px" height="150px" src="http://cdn.shopify.com/s/files/1/0709/2915/files/'.$data['productImg'].'"/></td>';
echo'</tr>';

}
echo'</table>';
?>

然而现在一切都很好! 这行代码的返回值:

<td>'.$data['bundleQty'].'<br/>'.$data['seed'].'<br/>'.$data['bundlePrice'].'<br/>'.$data['bundleSKU'].'</td>

返回表中另一行的结果,是的,它抓住了所有需要的信息,但我希望将结果放到同一行,只为每个新列...

捆绑产品与产品ID相关联,每个产品都有各种捆绑产品...... 就像我说的那样,我已经正确地回复了信息,但是我喜欢将捆绑包放在与产品相同的行上,而不是为每个捆绑包创建一个新行。

最诚挚的问候和感谢,看看我的问题。 ~~ Rory

  

一直在寻找几个小时,似乎无法在任何地方找到答案:(我真的可以用一些PHP FOR DUMMIES帮助:(

1 个答案:

答案 0 :(得分:0)

基本上你需要设置一个记住最后一个产品的var,然后如果这个产品与上一个产品相同,那么不要回显产品,而是添加,比如说只有一个捆绑的新行。

伪代码的解决方案将类似于此:

$last_product = ''; // before the loop starts

foreach( of your rows) { // as you have it

if( $last_product !== this product) {

// reset the temp variable
$last_product = this product;

//now echo out the bank, product etc

} // end of the conditional check

// echo out the rows of the bundle anyway, as you have it now

} // end of the loop

我首先在没有表的情况下工作 - 输出的数据最少。它不会很漂亮,但会有数据做你想要的,然后,逐渐添加表tr等等,否则你最终会混淆自己混淆隐藏在格式错误的html中的数据,查看源代码等等。

相关问题