Hi all I have a MYSQL database field which as some urls in the following format http://www.webstite.com/ http://www.webstite.com/ each line is terminated with CR I wanted to know if there is a way when I output the field wrap the <a href=" code around it to make them links when I query the database
Sure. Assuming that $row is the mysql_fetch_assoc for the resulting DB query and 'url' is the name of the field, just do this: echo "<a href=\"{$row['url']}\">Anchor Text</a>"; PHP:
I suggest somthing like: //Assume that $row is the query result: $urls = explode("\n",$row); foreach($urls as $url){ echo '<a href="'.$url.'">'.$url.'</a><br />'; } PHP:
Great as i was up late last night i wanted to now have the name in the url I now have the data like htt://www.xx.com,site name, htt://www.xx.com,site name, so at a guess the delimiter is , explode(",",$row); how do i output each string in in the correct places so i have output like<A href='http://www.xx.com'>site name</a>
This is really something that should have been done using unique fields in a separate table. It would be much easier to add/delete/modify urls in the future and saves you the trouble of having to hack something together to make it work the way you want. Anyway... //Assume that $row is the query result: $urls = explode("\n",$row); $count=count($urls); for($i=0;$i=$i+2;$i<=$count) { $j=$i+1; $url=$urls[$i]; $name=$urls[$j]; echo "<a href=\"{$url}\">{$name}</a><br />"; } PHP:
Please try this: A combination of explode() and list(); <?php //Assume that $row is the query result: $row= "http://www.xx1.com,site1 name, http://www.xx2.com,site2 name, http://www.xx3.com,site3 name, http://www.xx4.com,site4 name,"; $lines = explode("\n",$row); foreach($lines as $line){ list($href,$anchorText)=explode(",",$line); echo '<a href="'.$href.'">'.$anchorText.'</a><br />'; } ?> PHP: