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 に答える