如何编辑这个PHP函数以使用基于值的不同模板?

This code below currently just rotate flags to load different templates based on count. But I want to make use flag 1 for Sell if its not null or otherwise Flag 2 for Buy ?

How I can do that?

    $content = '';
    $flag = 1;
    while ($row = $db->doRead())
    {
        $content .= Template::Load('market-' . $flag++, 
            array
            (
                'name' => $row['UserID'], 
                'buy' => $row['Buy'],
                'sell' => $row['Sell'],
                'Item' => $row['Item'],
                'Item2' => $row['Item2']
            )
        );
        if ($flag > 2) $flag = 1;
    }

Check if $row['Sell'] is NULL, and set $flag accordingly:

if (! is_null($row['Sell'])) {
   $flag = 1;
}
else {
   $flag = 2;
}

Not sure if I understand you correctly, but this should work:

    $content = '';
    while ($row = $db->doRead())
    {
        $flag = $row['Sell']?1:2;
        $content .= Template::Load('market-' . $flag, 
            array
            (
                'name' => $row['UserID'], 
                'buy' => $row['Buy'],
                'sell' => $row['Sell'],
                'Item' => $row['Item'],
                'Item2' => $row['Item2']
            )
        );
    }