Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert table value to an "HTTPS" link 2

Status
Not open for further replies.

ABOzIT

IS-IT--Management
May 1, 2003
226
JP
Hello!

I have a very simple PHP script that extracts data from a MySQL DB and displays it in a table. One of the columns contains IP addresses that I'd like to convert into a link preceeded with "
I'm not an expert in PHP and cannot claim the following script as my own. I can however take bits out of other scripts and join them to do what I need. This one has me a little confused.

Here's my code (that doesn't work)for creating the table
Code:
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
if ($row=="IP Address")
{
        echo "<td nowrap='nowrap'><font face='Verdana'><font size='1'><a href ="[URL unfurl="true"]https://"$cell></a></font></td>";[/URL]  
}
else {
        echo "<td nowrap='nowrap'><font face='Verdana'><font size='1'>$cell</font></td>";
}
    echo "</tr>\n";
}

So, what this should do is convert each IP address in the "IP Address" column into a link. Eg - 192.168.1.1 whould be converted into a link that when clicked would open the web address
This should be a no brainer I would think but not being a programmer I would appreciate a little assistance.

Thanks!
 
Your quotes are in complete mismatch and that is probably what is causing your problems. Make sure your quotes always match:
Code:
echo [b][COLOR=red yellow]"<td nowrap='nowrap'><font face='Verdana'><font size='1'><a href ="[/color][/b]https://[b][COLOR=red yellow]"$cell></a></font></td>"[/color][/b];
Highlight shows what is quoted as a string, leaving the remainder for PHP to process. As you can see from this, PHP has no idea what to do with 'https:', which is not a command in PHP, not to mentioned that concatenating is not present. See solution below:
Code:
echo [b][COLOR=red yellow]"<td nowrap='nowrap'><font face='Verdana'><font size='1'><a href ='[URL unfurl="true"]https://"[/URL][/color][/b] . $cell . [b][COLOR=red yellow]"'>"[/color][/b] . $cell . [b][COLOR=red yellow]"</a></font></td>"[/color][/b];
You were also missing the text to appear as your link. Without a text, you can't really see anything.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Thanks for taking the time to look at this Vragabond.

Unfortunately this doesn't seem to work either. The text is displaying correctly in the cells but still don't appear as links.

Any further thoughts?

Thanks!
 
It's the former as the values are appearing OK but just not as links. I proved the "if staement isn't being matched by changing the font of that statement to see if the values changed but they didn't.

That being the case, looks like I'm not applying the right "test" to the data. I can't think of a way to test if the data is in a particular column in a table.

If I can test if the text I want to appear as links is in a particular column it should be simple enough to apply that to the "if" statement and it should work.

Any suggestions?
 
the line
Code:
if($row == 'IP address')
will never match (return true) as, definitionally, $row is an array and not a string.
equally I doubt you mean $cell either as 'IP Address' is neither a valid IP Address nor a valid domain name.

Are you perhaps referring to the [red]name[/red] of the field that you are examining? i.e. if the db column is IP Address, then encapsulate the response in a link? If so then you need to use something like mysql_fetch_assoc and examine the key of the array as well.

If, on the other hand, you are simply occluding the actual IP address that you are wanting to test (so the real variable would have a value of '123.456.789.000' (ish) then the above is not correct and you should change the code below to check for $cell rather than $fieldName. From your last post, I think my assumption above is correct, however.

Stylistically, you may find it easier to use breakouts when mixing html and php. alternatively heredoc blocks make things much clearer too.

Code:
// printing table rows
<style type="text/css">
td.normalTD{
font-family: Verdana;
font-size: 12px;
}
</style>
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
<?php 
foreach($row as $fieldName=>$cell):
   if ($fieldName == "IP Address"): 
?>

    <td nowrap="nowrap"><a href ="[URL unfurl="true"]https://<?php[/URL] echo $cell;?>"><?php echo $cell;>></a></td>  

<?php else: ?>
 
    <td nowrap="nowrap" class="normalTD"><?php echo $cell;?></td>  

<?php 
  endif; 
endforeach;
?>
</tr>
<?php endwhile; ?>
 
Thanks for taking the time to provide the detailed replies guys! This has given me some good info to persue.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top