Jump to content

How to format 1st row in MySQL output differently from all following rows in PHP?


Recommended Posts

I have a MySQL query that is returning the results that I need correctly. However, I want to clean up the output a bit for my specific use case. Specifically, I'd like to remove a column from the output so that it only appears the first time it's pulled from the database. Here's a quick example of the type of output I'm getting:

Campaign A - asset 1
Campaign A - asset 2
Campaign A asset 3

Campaign B asset 1
Campaign B asset 3

Campaign C asset 2

Campaign D asset 1
Campaign D asset 2
etc

This is what I'd like instead:

Campaign A
asset 
1
asset 2
asset 3

Campaign B
asset 
1
asset 3

Campaign C
asset 
2

Campaign D
asset 
1
asset 2

 

So, basically, I just want to see the "Campaign A" title once, even though it's part of the record of workshops.

Here's the php code that I'm working with (in this case, "campaigntitle" is the column I only want to see once per group. "assettitle" would be shown in every row returned by the query) :

<?php do { ?>
<tr>
<td><a href="detail-nd.php?recordID=<?php echo $row_assetslist['assetid']; ?>"> Details</a> <a href="assetupdate-nd.php?recordID=<?php echo $row_assetslist['assetid']; ?>">edit</a> delete</td>
<td><?php echo $row_campaignassetjoin['campaigntitle']; ?></td>
<td><?php echo $row_campaignassetjoin['assettitle']; ?></td>
<td><?php echo $row_campaignassetjoin['assettype']; ?></td>
</tr>
<?php } while ($row_campaignassetjoin = mysql_fetch_assoc($campaignassetjoin)); ?>

 

here's the pertinent mysql query in the document header:

mysql_select_db($database_campaignarchive, $campaignarchive);
$query_campaignassetjoin = "SELECT assets.*, campaign.* FROM campaign JOIN assets ON assets.campaignid=campaign.campaignid ORDER BY campaign.campaignid DESC";
$campaignassetjoin = mysql_query($query_campaignassetjoin, $campaignarchive) or die(mysql_error());
$row_campaignassetjoin = mysql_fetch_assoc($campaignassetjoin);
$totalRows_campaignassetjoin = mysql_num_rows($campaignassetjoin);

Check for a change in colA and only output when it changes

 

pseudocode:

currentCampaign = ''
while fetch next row 
    if campaign != currentCampaign 
        print campaign
        currentCampaign = campaign
    endif
    print asset
endwhile
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.