simple code displaying hyperlink

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cosmini
Forum Newbie
Posts: 2
Joined: Thu Dec 31, 2009 10:52 am

simple code displaying hyperlink

Post by cosmini »

hi all, I'm a complete PHP newbie and trying to get something basic working, basically, a hyperlink, if a field name is 'LINK'
here's the original script, to be modified towards the bottom
thx much for any help.
Cos


if($num_row >0)
{
//Get number of fields in query
$num_fields=mysql_num_fields($result);



# get column metadata
$i = 0;

//Set table width 15% for each column
$width=15 * $num_fields;

print('<br><table width='.$width.'% align="center"><tr>');
print('<tr><th colspan='.$num_fields.'>View&nbsp'.$table.'</th></tr>');

while ($i < $num_fields)
{

//Get fields (columns) names
$meta = mysql_fetch_field($result);

$fields_array[]=$meta->name;

//Display column headers in upper case
print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');

$i=$i+1;
}

print('</tr>');


//Get values for each row and column
while($row=mysql_fetch_row($result))
{
print('<tr>');

for($i=0; $i<$num_fields; $i++)
{
//Display values for each row and column, however display hyperlink if table field name is 'LINK'
// <a href="http://www.website.com/repository/file.mp3">file.mp3</a>

if(strtoupper($fields_array[$i])='LINK')
{print('<td><a href="http://www.website.com/repository/'.$row[$i].'</td>')}
else

{print('<td>'.$row[$i].'</td>')}

}

print('</tr>');
}

}
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: simple code displaying hyperlink

Post by SimpleManWeb »

Here's a really easy solution:

Code: Select all

 
   if(strtoupper($fields_array[$i])='LINK') {
       ?>
           <td>
                 <a href="http://www.website.com/repository/<?php echo $row[$i] ?>">LINK</a>
           </td>
       <?php
   } else {
        print('<td>'.$row[$i].'</td>')}
 
}
 
Enjoy
cosmini
Forum Newbie
Posts: 2
Joined: Thu Dec 31, 2009 10:52 am

Re: simple code displaying hyperlink

Post by cosmini »

thanks a bunch !!! it worked like a charm ;-) (after a minor correction)
Post Reply