2012-02-02 3 views
2

내가 이전에 사용자가이 게시물에 클릭 링크에 따라 페이지를 채울 질문을 한 여기에서 찾을 :PHP 배열 링크에 따라 데이터를 표시 - 후속

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

그러나 내가 원하는 다른 사람들이 데이터에 접근 할 수 있도록 만들었으므로 이제는 시도해보고 가져 가야합니다. 그러므로 나는 항상 그들에게 if 문을 생성 할 수 있기를 원합니다.

그래서 데이터베이스의 회사 이름 필드에 연결된 배열을 채우는 선을 따라 뭔가를 생각하고있었습니다.이 배열은 이름을 표시하기 위해 이미 수행했지만 직접적으로 배열에 넣는 대신 코드화했습니다. 사용자 입력시).

'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' 

내 시도 :

'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' 

희망이 내 문제를 설명

다음은 생각의 아이디어 나 기차의 나의 종류이다.

******** 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 

********는 **********

다시 수정 됨
$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>"; 

답변

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>" 

나는 당신을 올바르게 이해했다.

+0

고마워요. 당신이 올바르게 이해하고 있다고 생각하지만 작동하지 않는 것 같아요. (논리가 맞지만 그래도 오류가 없습니다. 데이터가 있어야하는 빈 공간이 있습니다. – Nils

+0

가장 먼저 할 일은 "$ companyarray"가 원하는 것인지 확인하기 위해 While 루프 다음에 "print_r ($ companyarray)"를 추가하는 것입니다. $ _GET 링크는 이름입니다 회사의 데이터베이스 열 "회사"와 정확히 일치합니까? –

+0

예, 그렇습니다. 내가 가진 열은 회사, 웹 사이트, ID, 이야기입니다. 그리고 회사 열을 선택하고 나머지는 회사에서 채우고 싶습니다. 링크를 클릭하십시오. 지금 인쇄를 시도하십시오. 감사합니다. – Nils

관련 문제