How to create Links in results from a MySQL Database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

How to create Links in results from a MySQL Database

Post by rach99 »

Hello Everyone!

I have been searching for the answer to this simple question for ages, and i am guessing it is probably a simple answer. I just can not work this out. I am teaching myself MySQL and PHP and I have a page which work fine as a search. But what i need is to have links to .php files to appear in the search results.

Basically what the website does is advertises. Each advertiser has their own page, which i need to bring up in the search results so users can click on the link and go to their particular page, I also need the search results to bring up other advertisers with similar products etc.

Any advice, tutorials or direction would be GREAT!

Thanks heaps

Rachael
User avatar
boo
Forum Commoner
Posts: 42
Joined: Mon Jul 02, 2007 11:30 am
Location: NY

Post by boo »

Are you looking for something like this

Code: Select all

<?php
$link = 'http://www.somesite.com';
$link_desc  = 'Click here to visit site';
?>

<a href="<?php echo $link?>"><?php echo $link_desc ?></a>
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

Post by rach99 »

Hey Boo,

Thanks for the response, that is close, but what i need is maybe the headings of each result to link to their related page.

So for example:

Gary's Lawn Seed - (this to link to garys_lawn_seed.php)
gary stocks a wide range of lawn seed.....


Bills Lawn Seed - (this to link to bill_lawn_seed.php)
bills lawn seed is better then gary's......

Lawn Seed - (links to main page lawn_seed.php)
listing of lawn seed products and company's.....

So a user would click on the link (title) and it would take them to the page?

Hope i make sense, I dont even know where to start with this! lol
Tarquinius999
Forum Newbie
Posts: 2
Joined: Mon Jul 02, 2007 8:32 am

Post by Tarquinius999 »

Code: Select all

<?php 
$link = 'http://www.somesite.com'; 
$link_desc  = 'Click here to visit site'; 
?> 

<a href="<?php echo $link?>"><?php echo $link_desc ?></a>
This is exactly what you want, you just need to make sure the query is able to put each out...

Code: Select all

$get_data = @mysql_query("SELECT * FROM tbl_users ORDER BY `id` ASC");
        while($data=mysql_fetch_array($data)) 
	{
                       $link = $data['users_link'];
                       $link_title = $data['users_link_title'];

                echo "<a href=\"".$link."\">".$link_title."</a><br>";
                }
This will produce a list based on the mysql table.

in the $data[' '] just enter your column name for each variable. and assuming you know mysql, change your query to match your table :-P
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

Post by rach99 »

Where would I place the link code?

Would I create another column in my database with the .php file link?

Sorry to be a pain, but I have search for tutorials everywhere. Also if you had a moment could you note what the code you posted is doing so i can understand a bit better?

Thank you so much for your time.
Last edited by rach99 on Wed Jul 11, 2007 5:21 am, edited 1 time in total.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

rach99 wrote:Would I create another column in my database with the .php file link?
Yes you have to create a new field if you have custom page for each user. Otherwise you can write an common script and put and user_id as GET parameter
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

Post by rach99 »

So do i just add for example

lawn_seed.php under a 'links' column?
or the full address where the website is upload like http://www.lawnseed.com.au/lawn_advertiser1.php?

Also where is the above link code meant to be placed in my code?

Thanks

Rachael
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

rach99 wrote:So do i just add for example

lawn_seed.php under a 'links' column?
or the full address where the website is upload like http://www.lawnseed.com.au/lawn_advertiser1.php?
First one.
rach99 wrote: Also where is the above link code meant to be placed in my code?
The place where you want to see it :)
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

Post by rach99 »

Code: Select all

$get_data = @mysql_query("SELECT * FROM tbl_users ORDER BY `id` ASC"); 
        while($data=mysql_fetch_array($data)) 
        { 
                       $link = $data['users_link']; 
                       $link_title = $data['users_link_title']; 

                echo "<a href=\"".$link."\">".$link_title."</a><br>"; 
                }
What exactly do i replace in this code with my own info?

And where do i place this piece of code in the php code i posted above that I have? And do i need to remove any code which conflicts with this? Also do i need to add the top bit of the php that was first posted by boo with the one I just asked about in this post?

Thank you very much for you help so far, and your patience.

I am having so much trouble getting this part of it to work! :(
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Sorry, but you should be more specific. We gave you the correct answer, so you should try something instead of asking to fix your code.
rach99
Forum Newbie
Posts: 6
Joined: Tue Jul 10, 2007 8:09 am

Post by rach99 »

I have been working on this and going through tutorials for 2 weeks, I am trying to learn php & mysql I am not just after people to fix my code as I respect the fact that people take the time on forums to help others out for nothing.

I didnt want the answer so to speak, the code doesnt mean much at all to me that was posted, I was just after a tutorial or explaination of how to link my results to individual php pages like how google shows results and has links that follow off to different pages. Even just what i should be looking for, for example should i be reading up on array and query statements etc. (I wanted more of an understanding so to speak)

thanks to everyone who helped me out so far, I do apologise if I have given the impression I am just here for people to do my work for me.

Goodluck with the forums
Post Reply