代码之家  ›  专栏  ›  技术社区  ›  Devner

PHP,MySQL:只需要显示一次父名称。。差不多了,但还是有问题

  •  0
  • Devner  · 技术社区  · 14 年前

    我的实验:

    我基本上试着写两个查询。一种是左键联接“models”上的“parent”表;使用GROUP BY parent_id,然后在while循环中,编写另一个查询,通过加入模型&来仅获取1个图像;使用model\ id字段对表进行映像。但是这样做只会列出一个模型,即使有多个模型。因此,我尝试使用GROUP BY parent\u id,model\u id。使用它确实会显示所有模型,但同时也会重复显示parent\u name&我需要家长的名字只显示一次整个页面。您可以说我正在尝试将模型名称分组到父&下;显示一个父项下的所有模型,我只显示模型的一个图像。如果我能避免多次显示父母的名字,问题就解决了。

    以下是我的表架构:

    //Table parent
    
    parent_id   parent_name
        1        Par1
        2       Par2
    
    
    //Table model
    
    model_id    parent_id   model_name
        1            1       Model1
        2           2       Model2
        3           1       Model3
        4           1       Model4
        5            2       Model5
    
    //Table model_images
    
    image_id    model_id
        1            1
        2           1
        3           1
        4            2
        5           3
        6           3    
    

    期望输出:

    Par1    ---> This is the parent. Needs to be shown only once.
    Model1  -->  This is a model. List all models that belong to this parent. 
    image_id 1 -> Show only 1 image of the model (model may have multiple images but I need just one) 
    
    Model3  -->  This is a model.
    image_id 5  -> Show only 1 image of the model    
    
    Model4  -->  This is a model.
    No Image    -> Note that no image exists for this model. So we show "No Image" text.
    
    ------------------------------------------------------------
    
    Par2    ---> This is the parent. Needs to be shown only once.
    Model2  -->  This is a model.
    image_id 4  -> Show only 1 image of the model
    
    Model5  -->  This is a model.
    No Image   -> Note that no image exists for this model. So we show "No Image" text.
    

    对不起,我忘了加这个。我是非面向对象的程序员。因此,如果您能在解决方案中避免使用面向对象的代码,并以非面向对象的方式向我展示它们,我将非常感激。谢谢。

    1 回复  |  直到 14 年前
        1
  •  1
  •   BenMorel Pankaj Kumar    10 年前

    您可以在一个查询中执行此操作,然后将其组合到关联数组中:

    $query = '  SELECT     *
                FROM       parent AS p
                LEFT JOIN  model AS m
                ON         p.id = m.parent_id
                LEFT JOIN  model_images AS m_i
                ON         m.model_id = m_i.model_id';
    
    $array = array();
    
    if($mysli->query($quer)){
        while($row = $result->fetch_assoc()){
            $array[$row['parent_name']][$row['model_id']] = $row;
        }
    }
    

    您将拥有一个以父名称作为数组键的关联数组。然后,您可以使用for循环只打印一次键(使用$i=0),其余的按值打印。

    够清楚了吗?

    编辑

    Array(
      'Par 1' => 
        Array(
          [0] => Array(
            'parent_id' => 1,
            'parent_name' => 'Par 1',
            'model_id' => 1,
            'model_name' => 'Model 1',
            'image_id',
          ),
          [1] => Array(...)
        ),
        'Par 2' => Array(...)
    )
    

    所以要打印出来需要两个循环。一个是父母的名字,一个是孩子的名字。

    foreach($array as $par_name => $models){
        echo 'Parent name: '.$par_name.'<br />';
        echo 'Model ID: '.$models[0]['model_id'].', Model Name: '.$models[0]['name']; // replace with your desired output
    }
    

    现在知道它是怎么工作的了吗?当然,正如Artefactor所说,如果您不喜欢OOP函数,可以使用过程函数。