Page 1 of 1

Link generation

Posted: Mon Jun 05, 2006 12:01 pm
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?

Re: Link generation

Posted: Mon Jun 05, 2006 12:02 pm
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.

Posted: Mon Jun 05, 2006 12:03 pm
by ionicle
Could anyone help me out with the code? I am a noob in PHP...

Posted: Mon Jun 05, 2006 12:06 pm
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 />';
}
?>

Posted: Mon Jun 05, 2006 12:07 pm
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

Posted: Mon Jun 05, 2006 12:08 pm
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?

Posted: Mon Jun 05, 2006 12:12 pm
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.

Posted: Mon Jun 05, 2006 12:15 pm
by ionicle
mmmmm, could you gimme an example pls:) how would the file look with users


bla
blabla
blabalbla
........

Posted: Mon Jun 05, 2006 12:36 pm
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.

Posted: Mon Jun 05, 2006 12:49 pm
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.

Posted: Mon Jun 05, 2006 12:55 pm
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
.......

Posted: Mon Jun 05, 2006 1:03 pm
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 />';
}
?>