Nils
Nils

Reputation: 57

How to populate 1 php page differently depending on link clicked?

Sorry for this question.

Basically I have a page where I have it automatically sorting through categories etc and each block (img and 1 or 2 details) has a link (which takes to a Static Profile page). I also have a database created that with another page that is used to populate it.

Now I could create a page (html or php) for each but this is not what I want as I do not want 100 pages of the same thing/layout just different data and images.

I want to use the same layout on e.g. single.php - but populate it differently depending on which link the user clicked.

I seem to understand the logic I just dont understand how to do it. It's obviously pulling the data from each database entry depending on the one clicked.

Very sorry to annoy you all as I am sure it is easy but I just can't get my head around it.

Thanks for any help you can provide.

Example.

index.php <- which I have working fine. User can get to the desired link using filters (name, category, etc)

single.php <- I want this to be the 1 php page that is populated with different data depending on which link the user clicked. So no.1 is clicked and single.php displays the data for profile1, no.2 is clicked and single.php displays the data for profile2 etc.

The data I am referring to will be static text with 1 or 2 a href links (standard) and also some images.

I know it is meant to be something like this single.php?=somethinghere but I am not sure.

If anyone can help I would be very appreciative.

***EDITED BELOW***

So would I do the following then? As I am trying to pull the data from the database to populate the single.php page.

`if($_GET['link']=='companyname1'){
    //print company1 details on single.php page
    echo '<div>info on companyname1 from database</div>';
    echo '<otherdiv>other info on companyname1 from database</div>';

}elseif($_GET['link']=='companyname2'){
    //print company2 details on single.php page
    echo '<div>info on companyname2 from database</div>';
    echo '<otherdiv>other info on companyname1 from database</div>';

}else{
    //print main index
}`

Upvotes: 2

Views: 1608

Answers (3)

Johnny Craig
Johnny Craig

Reputation: 5002

make your links similar to this

index.php?link=category

then in the php

if($_GET['link']=='category'){
    //print category page

}elseif($_GET['link']=='name'){
    //print name page

}else{
    //print main index
}

edit: here is a decent article. $_POST is the method you use when the variables are passed using a form. $_GET is whats used when people click links or type directly in the url bar.

http://mrarrowhead.com/index.php?page=php_passing_variables.php

Upvotes: 5

Crashspeeder
Crashspeeder

Reputation: 4311

It sounds like you want to send a GET request to single.php in the form of /single.php?userid=1 or /single.php?userid=2. You would access these inside of PHP by using the $_GET superglobal.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Link to single.php?id=123, then access intval($_GET['id']) (to allow only numeric input) and retrieve the row in the database corresponding to it. Then fill the page with the data from that row.

Upvotes: 2

Related Questions