Jump to content

[SOLVED] Automatically parse links, hopefully without REGEX


gmcalp

Recommended Posts

Good evening all, I hope your Friday is going well.

 

A few months back premiso helped me out with a script in this thread. His help was greatly appreciated, and it got me up and running with a better script.

 

Now I am back with one question, can anyone help me figure out a way to parse two of the database fields to automatically output as click-able links?

 

I am hoping to be able to do this without REGEX, but if that's the only way to go, then I guess that'll have to do. The two fields would be: row['url] and row['contactemail'].

 

Here is the code of the script:

 

else {
    while ($row = mysql_fetch_assoc($result)) {

      $row['added'] = date("m/d/y", strtotime($row['added']));

    // convert newline characters to HTML break tag ( <br /> )
      $row['description'] = nl2br($row['description']);
      $row['qualifications'] = nl2br($row['qualifications']);
      $row['preferred'] = nl2br($row['preferred']);
      $row['howtoapply'] = nl2br($row['howtoapply']);

    echo "<h3>".$row['title']."</h3>\n";
   //table format
   echo "<div class=\"table\">\n";
   echo "<div class=\"trow-jobs\">\n";   
   echo "<div class=\"tcell_1\"><strong>Title:</strong></div><div class=\"tcell_2\">".$row['title']."</div>\n";
   echo "</div>\n"; 
   echo "<div class=\"trow-jobs\">\n";   
   echo "<div class=\"tcell_1\"><strong>Employer:</strong></div><div class=\"tcell_2\">".$row['employer']."</div>\n";
   echo "</div>\n";   
   
    foreach ($row as $key => $val) {
      if ($key == "added") 
             break; // because that means we past the last line we wanted displayed this way.
      if ($val != "") 
		echo "<div class=\"trow-jobs\"><div class=\"tcell_1\"><strong>" . replaceTableTitle($key) . ":</strong></div><div class=\"tcell_2\">".$val."</div></div>\n";
   }
   
   echo "</div>\n";
   echo "<div>(Added on: " .$row['added']. ")</div>\n";
   echo "<br/><br/>\n";
   }   
}
function replaceTableTitle($title) {
    switch (strtolower($title)) {
        case 'fte':
               return "FTE";
	case 'howtoapply':
               return "How To Apply";
        case 'url':
               return "URL";
        case 'contactname':
               return "Contact Name";
        case 'contactphone':
               return "Contact Phone";
        case 'contactemail':
               return "Contact E-mail";			   
        break;
}

   // if we get here just upercase the first word
   return ucfirst($title);



}
mysql_close();

 

As always, any help is much appreciated.

Ok so you want to output their email as a mailto: link, and the url as a http: link.

 

ok; like this:

 

<?php

// Echo a link with http://$URL
echo '<a href="http://'.$row['url'].'">Website</a>';

// Echo a link to mailto:$EMAIL
echo '<a href="mailto:'.$row['contactemail'].'">Email</a>';

?>

Ok so you want to output their email as a mailto: link, and the url as a http: link.

 

ok; like this:

 

<?php

// Echo a link with http://$URL
echo '<a href="http://'.$row['url'].'">Website</a>';

// Echo a link to mailto:$EMAIL
echo '<a href="mailto:'.$row['contactemail'].'">Email</a>';

?>

 

I understand I could do it that way, but I would like it parsed automatically with the rest of the script, within the foreach loop so each of the two rows are displayed in order like it is now.

 

<?php

foreach ($row as $key => $val) {
      if ($key == "added")
             break; // because that means we past the last line we wanted displayed this way.
      if ($val != "")
         echo "<div class=\"trow-jobs\"><div class=\"tcell_1\"><strong>" . replaceTableTitle($key) . ":</strong></div><div class=\"tcell_2\">".$val."</div></div>\n";
   }

?>

 

Here is the output of that script: http://www.bmet.org/job-listings/ . As you can see, the two fields (URL and Contact Email) are not click-able links as I would like them to be.

Ok, I figured it out (with a little help from my brother-in-law). Here is what we did:

 

<?php
foreach ($row as $key => $val) {
      if ($key == "added") 
             break; // because that means we past the last line we wanted displayed this way.
      if ($val != "") {
	if (! strcmp($key,'url')) {
		echo "<div class=\"trow-jobs\"><div class=\"tcell_1\"><strong>" . replaceTableTitle($key) . ":</strong></div><div class=\"tcell_2\"><a href=\"http://$val\" target=\"_blank\" class=\"popup\">http://$val</a></div></div>\n";
	} elseif (! strcmp($key,'contactemail')) {
		echo "<div class=\"trow-jobs\"><div class=\"tcell_1\"><strong>" . replaceTableTitle($key) . ":</strong></div><div class=\"tcell_2\"><a href=\"mailto:$val\">$val</a></div></div>\n";
	} else {
echo "<div class=\"trow-jobs\"><div class=\"tcell_1\"><strong>" . replaceTableTitle($key) . ":</strong></div><div class=\"tcell_2\">".$val."</div></div>\n";
	}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.