2

I have previously asked a question to populate a page depending on which link the user clicked on this post, found here:

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

However I want to try and take it further now as I could make it so that other people are allowed to data therefore I want always be able to create the if statement for them.

So I was thinking something along the lines of populating an array which links to the company name field in the database (which I have already done it to display their name but have coded it manually instead of putting in an array which depends on user input).

Here is my kind of idea or train of thought:

'Declare Array/s

for (user click - use array item relating to the same name as the user clicked link -> [link]==COMPANYNAME-Which is in the database) {

    Display other info relating to that company'

My attempt:

'while($row = mysql_fetch_array($result)) {

    $companyarray[] = $row["company"]; // Declare array to store list of company names inserted to database by each company

    $varcompany[] = $row["company"]; //runs through the company column and populates the array varcompany with those names
    $varwebsite[] = $row["website"]; //runs through the website column and populates the array varwebsite with those names
    $varstory[] = $row["story"]; //runs through the story column and populates the array varstory with the text
}

for ($_GET['link'] == '$companyarray[i]') { // Thanks to Johnny Craig, Crashspeeder and Kolink for the help on this part (I have this working but by manually inserting the company names and creating a seperate if statement for each company) I want to be able to automatically populate this list with each new company added

    echo "<div id='companyname'><a href='http://$varwebsite[0]' />" . $varcompany[0] . "</a></div>";// Displays companies name with link to their website
    echo "<div id='website'><a href='http://$varwebsite[0]' />" . $varwebsite[0] . "</a></div>";// Displays companies website with link
    echo "<img src='images/example.jpg' class='profilePic' />";// At the moment manually entering image link (hopefully will be automatic in future)
    echo "<div id='story'>" . $varstory[0] . "</div>";// Displays a text field from database'

Hope this explains my problem.

********EDITED*********

while($row = mysql_fetch_array($result)) {
    $varcompany[] = $row["company"]; //runs through the company column and populates the array varcompany with those names
    $varwebsite[] = $row["website"]; //runs through the website column and populates the array varwebsite with those names
    $varstory[] = $row["story"]; //runs through the story column and populates the array varstory with the text
}

if($_GET['link']=='miiniim'){
    //print company1 details on single.php page
    echo "<div id='website'><a href='http://$varwebsite[0]' />" . $varwebsite[0] . "</a></div>"; //MIINIIM 1st Company in database
    echo "<div id='companyname'><a href='http://$varwebsite[0]' />" . $varcompany[0] . "</a></div>"; //MIINIIM 1st Company in database
    echo "<img src='images/example.jpg' class='profilePic' />";
    echo "<div id='story'>" . $varstory[0] . "</div>"; //MIINIIM 1st Company in database
}elseif($_GET['link']=='other'){
//print company1 details on single.php page
    echo "<div id='companyname'><a href='http://$varwebsite[1]' />" . $varcompany[1] . "</a></div>"; //MIINIIM 1st Company in database
    echo "<div id='website'><a href='http://$varwebsite[1]' />" . $varwebsite[1] . "</a></div>"; //MIINIIM 1st Company in database
    echo "<img src='images/example.jpg' class='profilePic' />";
    echo "<div id='story'>" . $varstory[1] . "</div>"; //MIINIIM 1st Company in database

********EDITED AGAIN**********

$result = mysql_query("SELECT * FROM ddcompanies WHERE company = {$_GET['link']}");

while($row = mysql_fetch_array($result)) {
    $varcompany = $row["company"];
$varwebsite = $row["website"];
$varstory = $row["story"];
}
print_r($result);
print_r($varcompany);
print_r($varwebsite);
print_r($varstory);

echo "<div id='website'><a href='http://$varwebsite' />" . $varwebsite . "</a></div>";
echo "<div id='companyname'><a href='http://$varwebsite' />" . $varcompany . "</a></div>";
echo "<img src='images/example.jpg' class='profilePic' />";
echo "<div id='story'>" . $varstory . "</div>";
Community
  • 1
  • 1
Nils
  • 57
  • 1
  • 10

1 Answers1

2
(QUERY = SELECT * FROM [table] WHERE company = {$_GET['link']};)
while($row = mysql_fetch_array($result)) {
    $companyarray = $row;
}
echo "<div id='companyname'><a href='http://" . $companyarray['website']  . "' />" . $companyarray['company'] . "</a></div>";
echo "<div id='website'><a href='http://" . $companyarray['website']  . "' />" . $companyarray['website'] . "</a></div>";
echo "<img src='images/example.jpg' class='profilePic' />";
echo "<div id='story'>" . $companyarray['story'] . "</div>"

How I understood you correctly.

  • Thanks mate I think you are understanding it correctly but that doesn't seem to be working :( Although the logic seems correct. There are no errors just blank space where the data should be. So i guess it is running ok just not correctly? Not sure. Any idea? – Nils Feb 02 '12 at 13:08
  • The first thing I would do is add "print_r($companyarray)" after the While loop to make sure that the "$companyarray" is what you want. The $_GET link is the name of the company and it matches the database column "company" correct? – Alexander Corsillo Feb 02 '12 at 14:17
  • Yes thats right. The columns i have are company, website, id, story. And i want the link to pick up the company column and populate the rest from that company link. Going to try the print now. Thanks again – Nils Feb 02 '12 at 14:26
  • Print isn't showing anything so I guess it is not fetching the data properly? – Nils Feb 02 '12 at 14:28
  • I added what I have at the moment - which works but it requires me manually setting each if statement. I basically want it to be done through an array which - accepts the link from the previous page e.g. index.php?link=miiniim and finds that row from database and populates based on this. if a different link is click - index.php?link=other then other row is found and populated. I think you knew this I am just typing again to make sure. Thanks for all your help so far – Nils Feb 02 '12 at 14:35
  • You really should just query for that value i.e. SELECT * FROM [table] WHERE company = {$_GET['link']}. Then you don't have to assigning the correct values to an array – Alexander Corsillo Feb 02 '12 at 15:11
  • So you think this would be better? EDIT ABOVE and Thanks – Nils Feb 02 '12 at 15:22
  • I have got it working for the first link that i click. but it doesnt seem to work for any more than that even though I have this at the top and it displays the correct one everytime $variable = $_GET['link']; echo "$variable"; and then my query is $result = mysql_query("SELECT * FROM ddcompanies WHERE company = '" . $variable . "'"); and the rest of my stuff is the same as earlier. And it works for the first link/company pressed but no others. – Nils Feb 02 '12 at 16:12
  • I got it working thanks so much! The above code does work I just had to change the test company name as it was wrong oops. But its working thanks so much mate I really appreciate all your help! Take Care :) – Nils Feb 02 '12 at 16:17