【问题标题】:PHP Array to display data depending on link - FOLLOW UP根据链接显示数据的 PHP 数组 - 跟进
【发布时间】:2012-02-25 01:11:12
【问题描述】:

我之前曾问过一个问题,以根据用户单击此帖子的链接来填充页面,可在此处找到:

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'

希望这能解释我的问题。

********已编辑*********

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

【问题讨论】:

    标签: php mysql database arrays


    【解决方案1】:
    (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>"
    

    我是如何正确理解你的。

    【讨论】:

    • 谢谢伙计,我认为您理解正确,但这似乎不起作用:(尽管逻辑似乎正确。没有错误只是数据应该存在的空白。所以我猜它运行正常,只是不正确?不确定。有什么想法吗?
    • 我要做的第一件事是在 While 循环之后添加“print_r($companyarray)”,以确保“$companyarray”是您想要的。 $_GET 链接是公司名称,它与数据库列“公司”匹配正确吗?
    • 是的,没错。我的栏目是公司、网站、id、故事。我希望该链接能够获取公司列并从该公司链接填充其余部分。现在去试试打印。再次感谢
    • Print 没有显示任何内容,所以我猜它没有正确获取数据?
    • 我添加了我现在拥有的东西 - 这很有效,但它需要我手动设置每个 if 语句。我基本上希望它通过一个数组来完成 - 接受来自上一页的链接,例如index.php?link=miiniim 并从数据库中找到该行并基于此填充。如果单击不同的链接- index.php?link=other 则找到并填充其他行。我想你知道这一点,我只是再次打字以确保。感谢您迄今为止的所有帮助
    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2015-08-28
    • 2020-03-24
    相关资源
    最近更新 更多