Link generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ionicle
Forum Newbie
Posts: 5
Joined: Mon Jun 05, 2006 12:00 pm

Link generation

Post by ionicle »

OK I have a link:

http://www.blabla.com/u=?

and a file, containing the usernames, like:

blabla
blablabla
blablablabla
..........

I want to be able to generate multiple links by substituting the ? with the respective username from the list. Can it be done? PHP maybe?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Link generation

Post by Chris Corbyn »

ionicle wrote:OK I have a link:

http://www.blabla.com/u=?

and a file, containing the usernames, like:

blabla
blablabla
blablablabla
..........

I want to be able to generate multiple links by substituting the ? with the respective username from the list. Can it be done? PHP maybe?
Yes. It's a fairly simple loop really.
ionicle
Forum Newbie
Posts: 5
Joined: Mon Jun 05, 2006 12:00 pm

Post by ionicle »

Could anyone help me out with the code? I am a noob in PHP...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ionicle wrote:Could anyone help me out with the code? I am a noob in PHP...
I don't know why, but for some odd reason I feel compelled to offer this much :wink: ...

Code: Select all

<?php
$user_names = array("blabla", "blablabla", "blablablabla");

foreach ($user_names as $name)
{
    echo '<a href="http://www.blabla.com/u=' . $name . '">Click here for ' . $name . '</a><br />';
}
?>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Code: Select all

$users = array('lorem', 'ipsum', 'dolor', 'sit', 'amet');

foreach ($users as $user)
{
    print '<a href="?u='.$user.'">View '.$user.'\'s profile?</a>';
}
Edit:

Errps. I guess you already got it ^^

was afk for a minute :P
ionicle
Forum Newbie
Posts: 5
Joined: Mon Jun 05, 2006 12:00 pm

Post by ionicle »

OK, thanx a lot but as soon as the usernames will be in a separate file, should I specify the URL of the array, or... how would it go?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ionicle wrote:OK, thanx a lot but as soon as the usernames will be in a separate file, should I specify the URL of the array, or... how would it go?
Include the file the usernames are in. Or just put the array in the file you are coding in. It depends on the need you have for the array data. If it will be used more than once, include it. If it will be used solely within the page you are coding, keep it there.
ionicle
Forum Newbie
Posts: 5
Joined: Mon Jun 05, 2006 12:00 pm

Post by ionicle »

mmmmm, could you gimme an example pls:) how would the file look with users


bla
blabla
blabalbla
........
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

If you define the array in a separate page, include the page using the include() function.

Code: Select all

include('users.list.php');

foreach ($users as $user) 
{ 
    print '<a href="?u='.$user.'">View '.$user.'\'s profile?</a>'; 
}
Also, RTFM at http://www.php.net.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What exactly are you trying to accomplish. If you are really new to PHP you may want to start off by trying to understand the basic concepts of arrays, loops and the like. At this point I would suggest some learning, then trying, then asking if things fail.
ionicle
Forum Newbie
Posts: 5
Joined: Mon Jun 05, 2006 12:00 pm

Post by ionicle »

I got it, but I did not get what the format of the included file should be, how should the users be arranged...

bla
blabla
blablabla
.......
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

users.php:

Code: Select all

<?php
$user_names = array("blabla", "blablabla", "blablablabla");
?>
showusers.php:

Code: Select all

<?php
include 'path/to/users.php';

foreach ($user_names as $name)
{
    echo '<a href="http://www.blabla.com/u=' . $name . '">Click here for ' . $name . '</a><br />';
}
?>
Post Reply