0

我有这个查询:

$query = "SELECT ads.*,
       trafficsource.name AS trafficsource,
       placement.name AS placement,
       advertiser.name AS advertiser,
       country.name AS country
       FROM ads
           JOIN trafficsource ON ads.trafficsourceId = trafficsource.id
           JOIN placement ON ads.placementId = placement.id
           JOIN advertiser ON ads.advertiserId = advertiser.id
           JOIN country ON ads.countryId = country.id
       WHERE advertiserId = '$advertiser_id'";

和广告表

ads Table
      ad_id PK
      size
      price
      trafficsourceId FK
      placementId FK
      advertiserId FK
      countryId FK

为了获取我正在使用的数据

$result = mysql_query($query) or die('Invalid query: ' . mysql_error());
    while ($row = mysql_fetch_assoc($result)) {

}

我无法弄清楚我需要如何打印页面,以便它看起来不像行,但还需要例如交通源名称的 id。我想做这样的事情:

编辑:

<div id="adscontent">
    <h1>Advertiser:</h1> Advertiser name
    <h2>Traffic Sources:</h2> Company1, Company2, Company 3
    <h2>Placements:</h2> Like: Newspaper, radio, website, bla bla
</div>

谢谢

4

1 回答 1

0

您将需要使用打印输出,但我认为这样的事情会起作用:

$results = array();
while ($row = mysql_fetch_assoc($result)) {
    $results[$row['advertiser']]['countries'][]      = $row['country'];
    $results[$row['advertiser']]['trafficsources'][] = $row['trafficsource'];
    $results[$row['advertiser']]['placements'][]     = $row['placement'];
}

// And now print the data
foreach ($results as $arvertiser => $data)
{
    echo "<h1>{$advertiser}</h1>";

    // Print Placements
    echo "Placements: " . implode(", ", $data['placements']) . '<br />;

    // Print Countries
    echo "Countries: " . implode(", ", $data['countries']) . '<br />;

    // Print Placements
    echo "Traffic Sources: " . implode(", ", $data['trafficsources']) . '<br />;

}

编辑:如果您需要添加 ID,您需要将您的选择更改为:

$query = "SELECT ads.*,
       trafficsource.name AS trafficsource,
       trafficsource.id AS trafficsourse_id,
       placement.name AS placement,
       placement.id AS placement_id,
       advertiser.name AS advertiser,
       advertiser.id AS advertiser_id,
       country.name AS country
       country.id AS country_id
       FROM ads
           JOIN trafficsource ON ads.trafficsourceId = trafficsource.id
           JOIN placement ON ads.placementId = placement.id
           JOIN advertiser ON ads.advertiserId = advertiser.id
           JOIN country ON ads.countryId = country.id
       WHERE advertiserId = '$advertiser_id'";

从那时起,您可以将这些信息包含在 $results 数组中,如下所示:

$results[$row['advertiser']['countries'] = array(
                                               'id'    => $row['country_id'], 
                                               'value' => $row['country')
                                           );

并从那里打印出你需要的任何东西。

于 2012-10-12T15:44:33.333 回答