Sign in to follow this  
Followers 0
Dubird

php help?

18 posts in this topic

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?


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites


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.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

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$])/', '&lt;a href="$1"&gt;$1&lt;/a&gt;', $String);

It may require the select all before and after:

 $String = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1&lt;a href="$2"&gt;$2&lt;/a&gt;$3', $String);

Edit: Egad the colors are psychedelic in Final Fantasy theme.

Edited by Mathias

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

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.


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites

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.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites
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:

&lt;?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); 

 if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
         mysql_error() . "&lt;/P&gt;");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

	echo("&lt;li&gt;" . $row["name"] . " - ");

  $site = mysql_fetch_field($result, $row['sitelink']);

	if($site = "none") {
		print ("hpage");
		}

	else {
		echo("&lt;a href=" .$row["sitelink"]."homepage&lt;/a&gt;");
		}

	echo(" - " . $row["country"] . " | &lt;b&gt;Recieved:&lt;/b&gt; " . $row["recieved"] . " | &lt;B&gt;Sent:&lt;/b&gt;" . $row["sent"] . "&lt;/li&gt;");
  }

?&gt;

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.


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites

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.

&lt;?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
         mysql_error() . "&lt;/P&gt;");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("&lt;li&gt;" . $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&lt;a href="$2"&gt;$2&lt;/a&gt;$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 &lt;a href="http://url"&gt;url&lt;/a&gt;
    else {
  print $site;
        }

    echo(" - " . $row["country"] . " | &lt;b&gt;Recieved:&lt;/b&gt; " . $row["recieved"] . " | &lt;B&gt;Sent:&lt;/b&gt;" . $row["sent"] . "&lt;/li&gt;");
  }

?&gt;


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

no, that didn't do it...it just leaves that spot empty. No error, just outputs everything else.


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites

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.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

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....


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites

oops my bad I forgot to add another =

 &lt;?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
         mysql_error() . "&lt;/P&gt;");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("&lt;li&gt;" . $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&lt;a href="$2"&gt;$2&lt;/a&gt;$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 &lt;a href="http://url"&gt;url&lt;/a&gt;
    else {
  echo $site;
        }

    echo(" - " . $row["country"] . " | &lt;b&gt;Recieved:&lt;/b&gt; " . $row["recieved"] . " | &lt;B&gt;Sent:&lt;/b&gt;" . $row["sent"] . "&lt;/li&gt;");
  }

?&gt; 

</span>

Edited by gokuDX7

sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

try this I changed the regex.

&lt;?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
         mysql_error() . "&lt;/P&gt;");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("&lt;li&gt;" . $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+)?)?)?)@', '&lt;a href="$1"&gt;$1&lt;/a&gt;', $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 &lt;a href="http://url"&gt;url&lt;/a&gt;
    else {
  echo $site;
        }

    echo(" - " . $row["country"] . " | &lt;b&gt;Recieved:&lt;/b&gt; " . $row["recieved"] . " | &lt;B&gt;Sent:&lt;/b&gt;" . $row["sent"] . "&lt;/li&gt;");
  }

?&gt;


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

Super Special Awesome! ^_^

Now, explain to me why it worked. What exactly does 'preg_replace' do and why the long string of characters? ^_^;


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites
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.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

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]


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

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 :P.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites
:P;)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0