niclake
niclake

Reputation: 134

php - Adding a link to my SQL query table

I currently have a functioning table displaying on my website using the following query/table display. I need to modify how the table is displayed so I can make the final column a link based on their CompanyID I call in the query.

Query (sanitized for security):

$tableResults = dbQuery('SELECT CompanyName, ContractStatus, LEFT(ContractDate, 10), CCNumber, LevelName, cinfo.CompanyID 
FROM <databases> WHERE <things>
order by CompanyName');

while($tableRow = mysql_fetch_assoc($tableResults))
{
    $tableData[] = $tableRow;
}

$tableColNames = array_keys(reset($tableData));

And the table (ignore the formatting, the data structure is my concern):

<table width="100%" border="0" cellspacing="1" cellpadding="5" class="tablesorter" id="contcomm_table">
    <thead>
        <tr>
            <th class="header"><strong>Company Name</strong></th>
            <th class="header"><strong>Contract Status</strong></th>
            <th class="header"><strong>Contract Date</strong></th> 
            <th class="header"><strong>Writing Number</strong></th>
            <th class="header"><strong>View Comm Schedule</strong></th>
        </tr>
    </thead>
<tbody>
<?
    foreach($tableData as $row)
    {
        echo "<tr>";
        foreach($tableColNames as $colName)
        {
            echo "<td>" . $row[$colName] . "</td>";
        }
        echo "</tr>";
    }
?>
</tbody>
</table>

The link in question would be '/_Document.pdf'. I think I'm having a case of the Mondays, because I just can't grasp this right now.

EDIT: The answers so far have gotten me closer, and now I just need to get over the last hump. I have everything displaying right, and the URL will show up... but now I need to concatenate the URL with the Company ID in the URL.

So, the relevant area I'm working on now, along with my best efforts at it:

if ($colName=='LevelName') {
    echo "<td><a href='http://my.url.here/".$CompanyID."_Document.pdf' target='_blank'>" . $row[$colName] . "</a></td>";
}
else {
    echo "<td>" . $row[$colName] . "</td>";
}

Upvotes: 0

Views: 102

Answers (4)

Dmitry Borovkov
Dmitry Borovkov

Reputation: 289

Should something like this:

<table width="100%" border="0" cellspacing="1" cellpadding="5" class="tablesorter" id="contcomm_table">
    <thead>
        <tr>
            <th class="header"><strong>Company Name</strong></th>
            <th class="header"><strong>Contract Status</strong></th>
            <th class="header"><strong>Contract Date</strong></th> 
            <th class="header"><strong>Writing Number</strong></th>
            <th class="header"><strong>View Comm Schedule</strong></th>
        </tr>
    </thead>
<tbody>
<?
    foreach($tableData as $row)
    {
        echo "<tr>";
        foreach($tableColNames as $colName)
        {
            if ($colName=='name_of_you_link)' echo "<td><a href='/_Document.pdf'>" . $row[$colName] . "</td>";
                echo "<td>" . $row[$colName] . "</td>";
        }
        echo "</tr>";
    }
?>
</tbody>
</table>

Upvotes: 1

Cowcow02
Cowcow02

Reputation: 571

Based on your latest code, will this solve your last problem?

if ($colName=='LevelName') {
    echo "<td><a href='http://my.url.here/".$row[CompanyID]."_Document.pdf' target='_blank'>" . $row[$colName] . "</a></td>";
}
else {
    echo "<td>" . $row[$colName] . "</td>";
}

Upvotes: 0

user557846
user557846

Reputation:

basically:

foreach($tableColNames as $colName) {

    if ($colName=='CompanyID') {
        echo "<td><a href='URL?' >" . $row[$colName] . "</a></td>";
    } else {
        echo "<td>" . $row[$colName] . "</td>";
    }

}

Upvotes: 0

Ethan Turk
Ethan Turk

Reputation: 437

Something like this should suffice:

if($colName == 'DocumentName') {
    echo "<td><a href='" . $row[$colName] . "' target='_blank'>View PDF</a></td>";
} else {
    echo "<td>" . $row[$colName] . "</td>";
}

That would replace the inner part of your foreach where you print out your table data. If the column name is NOT whatever your document column is named, it will print like normal. If it is, it prints a link.

Upvotes: 1

Related Questions