首页 文章

结合MySQL结果的多个产品属性

提问于
浏览
0

我有一个mySQL表,其中包含产品属性行,每个属性都与特定的属性类别(id_attrib_cat)相关联 .

用户应该为每个产品属性组合定义一个价格,所以我需要一个循环来创建一个属性表,在每行的末尾输入一个价格 .

属性类别值对于排除来自相同类别的属性进行组合非常重要 .

我怎么能做到这一点?

EDIT

属性类别示例:值

Format :方形,圆形

Size :S,M,L

Color :白色,蓝色,黑色,黄色

属性组合表的示例(下面的循环应该这样做):

  • Square S White = [价格输入]

  • Square S Blue = [价格输入]

  • Square S Black = [价格输入]

[...]


$q = mysql_query("SELECT id_attrib_cat, id_attrib, name FROM cms_products_attribs WHERE id_product=10 ORDER BY id_attrib ASC"); 

  while ($row = mysql_fetch_array($q, MYSQL_NUM)) {

      [** attribute combination + price input code **] 

  }

2 回答

  • 3

    使用CONCAT在查询本身中连接

    SELECT CONCAT(`id_attrib_cat`, ' ', `id_attrib`) AS `attributes`, `name` 
    FROM `cms_products_attribs` 
    WHERE `id_product`=10 
    ORDER BY `id_attrib` ASC
    

    这对你意味着你将从行中获得一个输出:

    while ($row = mysql_fetch_array($q, MYSQL_NUM)) {
      $attribs = $row['attributes'];
      echo $attribs . '<input name="price" type="text" />;
    }
    

    从机械上讲,你可能需要更多的东西,包括完整形成表格并在提交时处理表格,但这应该让你开始 .

    当你可以的时候,你应该总是让你的数据库完成它为之设计的繁重工作 .


    stop using mysql_* functions . These extensions已在PHP 7中删除 . 了解PDOMySQLiprepared语句并考虑使用PDO,it's really pretty easy .

  • 0

    首先,我建议使用PDO . mysql_query在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除

    您的查询应该是这样的:

    $q  =   $db->prepare("SELECT `id_attrib_cat`, `id_attrib`, `name` FROM cms_products_attribs WHERE `id_product`=:id_product ORDER BY `id_attrib` ASC");
    $q->execute(array(':id_product'=>"10"));
    

    我相信查询将返回多行 . 而不是同时,使用foreach:

    foreach($q as $row){
    
    $id_attrib_cat  =   $row['id_attrib_cat'];
    $id_attrib      =   $row['id_attrib'];
    $name           =   $row['name'];
    
    //Price Input goes here
    echo $id_attrib_cat.'<br>';
    echo $id_attrib.'<br>';
    echo $name.'<br>';
    echo '<input type = "text" name="'.$id_attrib.'">';
    }
    

相关问题