Foreach在一个循环中

时间:2011-12-04 09:20:57

标签: mysql arrays foreach while-loop smarty

我有两张桌子:“汽车”和“经销商”:

汽车表

id_car | name_car     | etc. 
----------------------------    
  1    | A3 Sportback | etc.
  2    | Ranger       | etc.
  3    | Transit Van  |etc.
  4    | Cayman       | etc.
 etc.  | etc.         | etc.

经销商表

在deal_cars列中,我将对应汽车的ID作为数组插入。

deal_id | deal_name | deal_cars | etc.
--------------------------------------
  1     | Ford      | 2,3       | etc.
  2     | Audi      | 1         | etc.
  3     | Porsche   |  4        | etc.
 etc.   | etc.      | etc.      |

我会看到一个显示以下信息的页面:

经销商名称 - 汽车

  • 福特 - Ranger,Transit Van
  • 奥迪 - A3 Sportback
  • Cayman - Porsche

提取经销商名称没问题,但我不知道如何从ID中提取汽车名称。

我试过了:

$dealerships_sql = $data->query("SELECT * FROM dealerships ORDER BY deal_name ASC"); 
while($dealerships_obj = $data->extract($dealerships_sql)){ 
  //Dealerships data 
    $deal_id[] = $dealerships_obj->deal_id; 
    $deal_name[] = $dealerships_obj->deal_name; 
    etc etc      

  //Try to get cars ids and turn them in cars names.
       $deal_cars[] = $dealerships_obj->deal_cars; 
       $deal_cars[] = explode(',',$deal_cars);    
       $cars = array();  
         foreach ($deal_cars AS $deal_car) { 
            $cars_sql = $data->query("SELECT name_car FROM cars WHERE id_car = '$deal_car'"); 
              while($cars_obj = $data->extract($cars_sql)){ 
                 $cars[] = stripslashes($cars_obj->name_car)." ";    
               } 
         }  
} 

我使用smarty作为模板引擎,因此我指定了一些变量:

$smarty->assign ("deal_id", $deal_id);
$smarty->assign ("deal_name", $deal_name);
etc etc.

$smarty->assign ("cars", $cars);

我的模板是:

<table border="1">
<tr> 
<td>Dealership</td>
<td>Cars</td>
</tr>
{section name="foo" loop=$deal_name}
<tr> 
<td>{$deal_name[foo]}</td>
<td>{$cars[foo]}</td>  
</tr>
{/section}
</table>

但代码返回:

  • 福特 - 游侠
  • 奥迪 - A3 Sportback
  • Cayman - Porsche

它仅显示每个经销商的第一辆汽车(阵列中的第一个元素)。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

我认为您的数据库设计不适合这里。

汽车表

id_car | name_car     | etc. 
----------------------------    
  1    | A3 Sportback | etc.
  2    | Ranger       | etc.
  3    | Transit Van  | etc.
  4    | Cayman       | etc.
 etc.  | etc.         | etc.

经销商表

deal_id | deal_name | etc.
---------------------------
  1     | Ford      | etc.
  2     | Audi      | etc.
  3     | Porsche   | etc.
 etc.   | etc.      |

经销商到汽车表

dealerid | carid 
    1    |   2
    1    |   3
    2    |   1
   etc.  |  etc.

如您所见,我为另外两张桌子之间的关系做了另一张表。

$dealerships_sql = $data->query("SELECT * FROM dealerships ORDER BY deal_name ASC"); 
$dealers = array();
while($dealerships_obj = $data->extract($dealerships_sql)){ 
  //Dealerships data, use object
    $dealerid = $dealerships_obj->deal_id;
    $dealers[$dealerid]['dealer'] = $dealerships_obj;     

  // Cars
    $cars = array();
    $car_sql = $data->query("SELECT name_car FROM cars JOIN dealerToCars ON carid = id_car JOIN dealer ON deal_id = dealerid WHERE deal_id = " . $dealerid);
  // now you have all cars from the selected dealer
    while ($cars_obj = $data->extract($car_sql))
    {
           $cars[] = stripslashes($cars_obj->name_car);
    }

  // Assign Cars to dealer
    $dealers[$dealerid]['cars'] = $cars;
} 

现在,所有经销商都在经销商处购买汽车。

由于你使用的是smarty,只需将整个数组传递给smarty,让其余的数组在它的模板中做得很聪明。 Smarty能够使用数组和对象,因此您无需将其拆分为多个数组:

$smarty->assign ("dealers", $dealer);

因为我不知道你的课程,你可能需要添加一些getter或setter,或者只是公开属性,所以smarty可以访问它们:

<table border="1">
<tr> 
<td>Dealership</td>
<td>Cars</td>
</tr>
{foreach $dealers as $dealer}
<tr> 
<td>{$dealer['dealer']->deal_name}</td>
<td>{foreach $dealer['cars'] as $car}{$car}, {/foreach}</td>  
</tr>
{/foreach}
</table>

也许您需要查看模板中的标识符。

(我正在使用smarty 3)

答案 1 :(得分:0)

我认为你应该拥有每辆车的经销商行(这样你就可以通过JOIN获得所有信息并轻松解析)。

答案 2 :(得分:0)

最好的方法是一次性完成所有事情,你可以这样做:

select 
    d.deal_name, 
    group_concat(c.name_car order by c.name_car separator ', ')
from dealerships d
join cars c on find_in_set(c.id_car, d.deal_cars) > 0
group by d.deal_name;

或(作为单独的字段):

select d.deal_id, d.deal_name, c.id_car, c.name_car
from dealerships d
join cars c on find_in_set(c.id_car, d.deal_cars) > 0;