Dubird 6,815 Report post Posted June 4, 2008 Ok, ran into another problem. I have a database I want to retrieve info from and format it on the page. Here's the catch: if there is a website link in that person's info, I want it to display the link, otherwise, just some text. I can't figure out what I need to do to make PHP check one certian field in a result to determine which IF-ELSE to use. I have that set up, but it's not checking, so everything is under the IF clause. (if that makes sense to others). Any suggestions? Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 4, 2008 are you saving the links as html links <a href="http://link.com">link</a> or are they plain text http://bla.com If there plain text you will have to use regular expressions (regex) and or str_replace() to replace them with html links. Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Mathias 426 Report post Posted June 5, 2008 (edited) Take this with a grain of salt as it's untested. Let me know if it works. (important: [^ -> not (\s -> space or $ -> end of line)]) $String = preg_replace('/(http:\/\/[^\s$])/', '<a href="$1">$1</a>', $String); It may require the select all before and after: $String = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $String); Edit: Egad the colors are psychedelic in Final Fantasy theme. Edited June 5, 2008 by Mathias Quote Understand this lad, fate is a fickle lady. Work with the hand you're dealt and you may just be able to run your flag up the pole. Don't, and well, you may just find your mast cut down. Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 5, 2008 That's not the problem. My problem is making it check one field from the database, and then use IF-ELSE to output the correct thing. Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 5, 2008 so you want it to check a person's content (ie. field that has something they inputed) for urls. If it finds a url you want php to make the url into a link so people can click on it, correct? Or are you talking about something different? Mathias, just did the regex for you to switch urls into clickable links. Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 5, 2008 so you want it to check a person's content (ie. field that has something they inputed) for urls. If it finds a url you want php to make the url into a link so people can click on it, correct? Or are you talking about something different?Mathias, just did the regex for you to switch urls into clickable links. Pretty much, yeah. Or rather, if the field contains one thing, output one thing, (IF) and if it contains anything else, it will be a link, so it'll output something else (ELSE). Here's what I have: <?php // Request data $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the members while ( $row = mysql_fetch_array($result) ) { echo("<li>" . $row["name"] . " - "); $site = mysql_fetch_field($result, $row['sitelink']); if($site = "none") { print ("hpage"); } else { echo("<a href=" .$row["sitelink"]."homepage</a>"); } echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>"); } ?> Now, this gives me no errors, but it only outputs the first clause, then goes on to the rest. I know the $site row is probally wrong, but I can't figure out what else to use there. See, that field will either have a link or nothing in it, so I just want to change that one part of the outputed HTML to reflect that. Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 5, 2008 Not sure if this will work or not. I didn't test it. I used Mathias' regex as well so who knows if it works or not. Add a few comments as well so you know what it does. <?php // Request data $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the members while ( $row = mysql_fetch_array($result) ) { echo("<li>" . $row["name"] . " - "); //$site = mysql_fetch_field($result, $row['sitelink']); // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link. $site = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $row['sitelink']); //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage if($site = "") { print ("hpage"); } //else we print <a href="http://url">url</a> else { print $site; } echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>"); } ?> Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 5, 2008 no, that didn't do it...it just leaves that spot empty. No error, just outputs everything else. Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 6, 2008 try changing the print $site; to echo ("$site"); PS. is their an sql field with the name sitelink? If their is not, then thats why nothing is being displayed. Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 6, 2008 nope, that didn't help either...and yes, one of the columums is named 'sitelink' and it either has a link starting with http or it's blank.... Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 6, 2008 (edited) oops my bad I forgot to add another = <?php // Request data $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the members while ( $row = mysql_fetch_array($result) ) { echo("<li>" . $row["name"] . " - "); //$site = mysql_fetch_field($result, $row['sitelink']); // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link. $site = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $row['sitelink']); //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage if($site == "") { print ("hpage"); } //else we print <a href="http://url">url</a> else { echo $site; } echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>"); } ?> </span> Edited June 6, 2008 by gokuDX7 Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 7, 2008 oh very close!...it's not formatting the link properly, but at least it's doing the IF-ELSE thing.... http://journal.thegwg.com/members3.php There's the page it's on. Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 7, 2008 try this I changed the regex. <?php // Request data $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the members while ( $row = mysql_fetch_array($result) ) { echo("<li>" . $row["name"] . " - "); //$site = mysql_fetch_field($result, $row['sitelink']); // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link. $site = preg_replace('@(http?://([-\w\.]+)+(\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $row['sitelink']); //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage if($site == "") { print ("hpage"); } //else we print <a href="http://url">url</a> else { echo $site; } echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>"); } ?> Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Dubird 6,815 Report post Posted June 7, 2008 Super Special Awesome! Now, explain to me why it worked. What exactly does 'preg_replace' do and why the long string of characters? Quote Yesterday was the deadline for all complaints! Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 7, 2008 Super Special Awesome! Now, explain to me why it worked. What exactly does 'preg_replace' do and why the long string of characters? well theirs about 100 different ways you can do it using regular expressions (aka. regex). preg_replace takes the regex that we set and searches for that expression in whatever source we feed it. So for your site it's searching for a number of things. 1) content thats starting with http:// 2) a word after http:// (1 or more) 3) a digit (if any at all) and some other random stuff that could be stripped out but I was to lazy to do lol. This regex is over kill and could be cut down even more but since it works, don't worry about it. Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Mathias 426 Report post Posted June 7, 2008 If the assumption is that the user entered a valid website, then you could have: $site = '<a href="' . $row['sitelink'] . '">' . $row['sitelink'] . '</a>'; I'll explain a little of the regex below with a few notes. Most of it is just preference or something easily overlooked as Goku definitely knows his stuff. I've never used the @ to replace the / required at the start and end of a php regular expression. http://us3.php.net/manual/en/function.preg-replace.php I usually use double quotes around my pattern as well to allow for non-printable characters such as \n (new line), \r\n (carriage return and new line), \t (tab), etc.. http://www.regular-expressions.info/characters.html http? ~ means htt or http did you mean: https? for http or https ([-\w\.]+) ~ means literal dash, a word, or a period one or more times. (the \. could be just . since most metacharacters do not need escaped in square brackets) Reference the character classes for a list of definitions of what characters are caught with \w and other special characters. http://www.regular-expressions.info/charclass.html Kelene's telling me to wrap this up, but the references at http://www.regular-expressions.info/ should prove useful. Like Goku said, if it works it works. All of the checks shouldn't be necessary. Unfortunately, I took too long typing this much and now my ear is being pulled. Try the following. Maybe \$. I'll eventually run some tests and let you know for sure, since I need to do something similar soon. $site = preg_replace("/(https?\/\/[^\s$]+)/", "<a href=\"$1\">$1</a>", $row['sitelink']);[/PHP] Quote Understand this lad, fate is a fickle lady. Work with the hand you're dealt and you may just be able to run your flag up the pole. Don't, and well, you may just find your mast cut down. Share this post Link to post Share on other sites
gokuDX7 2,871 Report post Posted June 7, 2008 http? ~ means htt or http did you mean: https? for http or https ya, this was supposed to be for https thats why theres unneeded things in it. Like I said, I was to lazy to take them out and edit it more to her need and since it worked there wasn't really a point for me to spend more time on it since I knew you would probably view the thread and make something smaller . Quote All hail piggy, king of bacon ^)^ Share this post Link to post Share on other sites
Mathias 426 Report post Posted June 7, 2008 Quote Understand this lad, fate is a fickle lady. Work with the hand you're dealt and you may just be able to run your flag up the pole. Don't, and well, you may just find your mast cut down. Share this post Link to post Share on other sites