Closed Thread Icon

Topic awaiting preservation: links in PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12768" title="Pages that link to Topic awaiting preservation: links in PHP (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: links in PHP <span class="small">(Page 1 of 1)</span>\

 
CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 05-31-2003 18:15

Alrighty. A few months ago I (with the assistance of you kind folks and the people at devshed) developed this PHP/MySQL database for my client where people can search dealers that sell his product. My client is inquiring about how to make the email, webpage, and mapquest map links people can click on. How can I do this? Just point me in the right direction and I will make my best effort:

print $row["Contact_Name"]."<br>\r";
print "Phone: ";
print $row["Phone"]."<br>\r";
print "Fax: ";
print $row["Fax"]."<br>\r";
print "Email: ";
print $row["Email"]."<br>\r";
print "Homepage: ";
print $row["Web_Site"]."<br>\r";
print "See map for directions: ";
print $row["Map"]."<br>";

} while($row = mysql_fetch_array($result));

Thanks.
CRO8

[This message has been edited by CRO8 (edited 05-31-2003).]

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 05-31-2003 18:31

It's not all that difficult. All you'll have to do is inserting <a> tags around the data you already have.

This would make the e-mail address a link:

print '<a href="mailto:' . $row['Email'] . '">' . $row['Email'] . '</a><br>\r';

To avoid having to escape the double quotes of the href="" attribute I replaced your double quotes with single quotes...


[edit: Whoops, forgot the mailto: part... ]

[This message has been edited by kuckus (edited 05-31-2003).]

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 05-31-2003 19:18

hey! Yup. I caught that "mailto:" as well. Thanks so much Now, I am trying to use what you gave me and apply it to the website link. Go back to the database drop down menu and choose "California." From the retrieved info, click on any of the homepage links. What is happening is that PHP is assuming the URL is on the same server. I was thinking of using a target=_blank somewhere . . . what do you think?

Thanks again.

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 05-31-2003 20:03

That's because the URLs you have there don't start with "http://" - if you're sure that it's the same with all other links in the db you could just let PHP add it.

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 05-31-2003 20:14
quote:
if you're sure that it's the same with all other links in the db you could just let PHP add it.



Yes. Thanks again!

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 05-31-2003 20:18

Putting \r characters in your code will put carriage-returns in the site source, not the browser output; for best results, use "<br />\r\n" if you're on a Windows server, or "<br />\n" if you're on Unix/Linux. My advice is to use just "\n" exclusively; since the web is pretty much built on Unix, that'll give you accurate results across the board. ("\n" is UNIX-style, "\r\n" is Windows style, and just "\r" is Mac style.)

Why put newlines in dynamic output? No real reason... it just looks prettier if someone happens to View Source. And it's easier to read your own code when you're debugging, if you need to say "what the heck kind of HTML did my code make, anyway?"

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 05-31-2003 20:53

Thanks PF- I will follow your suggestion.

Another question. I am fiddeling around with this code:


print "See map for directions: ";
print '<a href="' . $row['Map'] . '">' . $row['Map'] . "</a><br>\n";

OK. So this works fine. It retreives a long link to mapquest which I copy/paste into the dbase. Now, instead of printing the actual link, I want to print Link to Map.

This is what I thought would work:

print '<a href="' . $row['Map'] . '">' 'Link to Map' "</a><br>\n";

but of course it doesn't work . . . and the reason I am back! Solution seems to be right in my face, but . . .alas . .

Thanks.
CRO8

[This message has been edited by CRO8 (edited 05-31-2003).]

[This message has been edited by CRO8 (edited 05-31-2003).]

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 05-31-2003 21:45

You'll have to realise what exactly the single quotes and dots do in that line...

The dots are used to connect multiple strings and variable names to form the string you want to print,
the single quotes enclose the text/code you enter directly into the PHP file (they *don't* enclose variable names).

Which means that you don't need the quotes before and after the "Link to map" text in your example.

Another thing I noticed is that you lost the double quotes around the URL in the HREF attribute along the way - although most browsers won't complain and display a working link anyway it's always better to have valid HTML

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 05-31-2003 22:34

Thanks for explaining Kuckus. I still am having problems with this portion of code. I took out the quotes surrounding the "Link to Map" words like you instructed but was unclear about missing double-quotes around URL in href:


my code
print '<a href="' . $row['Map'] . '">' Link to Map "</a><br>\n";

or

print '<a href="' . $row['Map'] . '">' . Link To Map . "</a><br>\n";

your original code
print '<a href="mailto:' . $row['Email'] . '">' . $row['Email'] . '</a><br>\r';


Did I misunderstand you?

Thanks.
CRO8

[This message has been edited by CRO8 (edited 05-31-2003).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-31-2003 23:28

CR08

What Kuckus meant was that you need to understand what the different quotation marks are accomplishing.

He originally told you to surround your print() statement with single quotes so you wouldn't have to escape your double quotes that are needed for the HTML href tags. In PHP there is a distinct difference between what single and double quotes accomplish. While they can both surround a simple string, if you put a PHP variable like $row['Map'] inside single quotes it will treat it like a string of characters and not convert it to the value you expected. That's why when you are working inside a single quoted string and want to use a variable you need to stop the string with a single quote and then concationate the variables value to it using the period '.', and then start the string again with another single quote but only after using another period '.' that tells PHP to continue adding what comes after this to my string. So while you have what is below right now:

print '<a href="' . $row['Map'] . '">' Link to Map "</a><br>\n";


This part starts the print statement, as you can see it's started and stopped with single quotes

print '<a href="'

Then comes your PHP variable between 2 concationation (is that a word) operators

. $row['Map'] .

Now your string starts again with a single quote and you run onto where your having a bit of problem

'">' Link to Map "</

If you'll notice and compare this again to the <a> tag that Kuckus wrote that worked for you before, where you have Link to Map is plain text and not a PHP variable as in the other tag so there's no need to stop your string after the > because your not converting anything and therefore don't need to let the PHP parser get to it. In this case to finish off the string you just need to remove all but the ending quote which should end with a single quote as it started.

print '<a href="' . $row['Map'] . '"> Link to Map </a><br>\n';

I hope my long winded explaination didn't confuse you further.

::edit::

For the \n to work as you intend I believe it needs to be in double quotes so you would actually need to make you tag look something like this:

print '<a href="' . $row['Map'] . '"> Link to Map </a><br>'."\n";

::/edit::

-Butcher-

[This message has been edited by butcher (edited 05-31-2003).]

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 06-01-2003 00:01

Thank you both for taking the time to explain. I think I now beginning to understand. Single quotes surround html strings, double quotes are used only when applying to html (ie <a href=" "> </a> ), and PHP variables are separated from the main string by a dot.

print '<a href="' . $row['Map'] . '"> Link to Map </a><br>' . "\n";

print (1)'<a href="' (2). $row['Map'] . (3)'"> Link to Map </a><br>'. (4)"\n";

(1) html string
(2) PHP variable
(3) html string
(4) carriage return

Correct?

The code works in the case I have a map link, but when the field is null- it still says "Link to Map" and the link sends me to the index page. Check it out and choose California as it has the most entries.

Thanks.
CRO8

[This message has been edited by CRO8 (edited 06-01-2003).]

[This message has been edited by CRO8 (edited 06-01-2003).]

kuckus
Bipolar (III) Inmate

From: Berlin (almost)
Insane since: Dec 2001

posted posted 06-01-2003 10:18

CRO8: Yup, that's looking good


For your next question, you can let PHP check whether $row['Map'] contains a link or not by using the empty() function with an if structure.

if (empty($row['Map'])) {
&nbsp;&nbsp;// do something
}

would execute the following code only if the field was empty. As you want the link (and probably also the "See map..." text) only to be printed if the field is *not* empty you'd start the if condition with an exclamation mark and insert the map link printing code:

if (!empty($row['Map'])) {
&nbsp;&nbsp;print ...
&nbsp;&nbsp;print ...
}

kuckus

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-01-2003 10:18

in that case you'll have to check first, if it's a null field, and only print the link when there is data in it.

like

if ($map != '') //comparing to the empty string should work most of the time... if you use array_fetch($row) you could probably also use isset($row['map'])
{
print "Your link code here";
}
else
{
print 'sorry, no link available.';
}

CRO8
Bipolar (III) Inmate

From: New York City
Insane since: Jul 2000

posted posted 06-01-2003 22:35

Thanks! I am going to hold off for now checking for empty field as all fields will have map links- I will repost if necessary.

Thanks again!

CRO8



[This message has been edited by CRO8 (edited 06-01-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu