PHP 数组根据链接显示数据 - FOLLOW UP
我之前曾问过一个问题,根据用户在这篇文章上单击的链接来填充页面,可以在此处找到:
但是,我现在想尝试更进一步,因为我可以做到这一点,以便其他人允许数据因此我希望始终能够为它们创建 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>";
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>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我如何正确理解你。
How I understood you correctly.