Forum Moderators: coopster

Message Too Old, No Replies

Outputting variables in html table

How not to repeat variables in multiple rows if the same

         

ichabod

8:14 pm on Nov 8, 2006 (gmt 0)

10+ Year Member



This code works but if there are multiple $ATTACHMENT_NM values for the same $POLICY_TITLE, I'd like to echo all the $ATTACHMENT_NM values with <br> between in the same row/cell. Or it could be a new row but without repeating the $POLICY_TITLE each time. I've searched for a way to do it and tried a bunch of things but I can't figure out how to make it work. Anyone have a suggestion?

$query = "SELECT DISTINCT policy.POLICY_FILE_NM,policy.POLICY_TITLE,attachments.ATTACHMENT_NM,attachments.ATTACHMENT_FILE_NM FROM policy,policy_categories,attachments WHERE policy_categories.POLICY_CD=policy.POLICY_CD AND policy.POLICY_CD=attachments.POLICY_CD AND policy_categories.BY_LAW_CD = '3' ORDER BY policy.POLICY_TITLE ASC";

$result = mysql_query($query) or die ("Couldn't execute query.");
$row = mysql_num_rows($result);
$i=0;
while ($i < $row)
{
$POLICY_FILE_NM=mysql_result($result,$i,"POLICY_FILE_NM");
$POLICY_TITLE=mysql_result($result,$i,"POLICY_TITLE");
$ATTACHMENT_NM=mysql_result($result,$i,"ATTACHMENT_NM");
$ATTACHMENT_FILE_NM=mysql_result($result,$i,"ATTACHMENT_FILE_NM");

//display in table
if($i%2): $color = "#F8F8F8"; else: $color = "#F4F4F4"; endif;
echo "<tr>
<td style=\"background-color: ".$color."\" class='charttext' valign='top'><a href='$POLICY_FILE_NM' target='_blank'>$POLICY_TITLE</a></td>
<td style=\"background-color: ".$color."\" class='charttext' valign='top'>- <a href='$ATTACHMENT_FILE_NM' target='_blank'>$ATTACHMENT_NM</a></td>
</tr>";

$i++;
}

ramoneguru

10:53 pm on Nov 8, 2006 (gmt 0)

10+ Year Member



My logic:
I'm going to take a temp variable and a repeat variable and use those values to check to see if they both match. If they do, then I know there was a previous match of POLICY_TITLE so I can set policy_title to '' and still keep the repeat value to check the next titles....damn meeting, I don't have time to clarify this bbl.....


$i=0;
$POLICY_TITLE = mysql_result($result,$i,"POLICY_TITLE");
$repeatVal = $POLICY_TITLE;

while ($i < $row)
{
$POLICY_FILE_NM=mysql_result($result,$i,"POLICY_FILE_NM");
$ATTACHMENT_NM=mysql_result($result,$i,"ATTACHMENT_NM");
$ATTACHMENT_FILE_NM=mysql_result($result,$i,"ATTACHMENT_FILE_NM");

$i++;
//note the increase in $i before this statement
$temp = mysql_result($result,$i,"POLICY_TITLE");
if ($temp === $repeatVal) {
$POLICY_TITLE = '';
}else {
$POLICY_TITLE = $temp;
$repeatVal = $temp;
}

}//end while

Didn't test, let me know if it works.
--Nick