[英]Printing out each row from a MYSQL table individually in PHP
I am trying to individually print out each line from my SQL database in PHP. 我试图从PHP的SQL数据库中单独打印出每一行。 I am trying to do this so each line that is retrieved, it can act like a link which will direct the user to another page.
我正在尝试执行此操作,以便检索到的每一行都可以像一个链接一样将用户定向到另一个页面。 For example, the current SQL query will output the Category names from the database Category, i would like it to output all the values from that table but have it so each one has a different redirect link to another page which clicked on.
例如,当前的SQL查询将从数据库类别中输出类别名称,我希望它输出该表中的所有值,但有该值,因此每个人都有一个不同的重定向链接,该链接指向另一个单击的页面。
$query = "SELECT CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = @mysqli_query ($conn, $query);
$numrows = mysqli_num_rows($results);
if ($results) {
if ($numrows >0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td>' . $row['CATEGORY_NAME'] . '</td>
</tr>
';
}
mysqli_free_result ($results);
Such as, 如,
Category
__________
PS4
XBOX
i can click on PS4 and it would take me to another page, i know how to do this with a href and then print out the row in sql however, i'm not sure how do print out each row individually without printing them out using $row['CATEGORY_NAME']. 我可以单击PS4,它将带我到另一页,我知道如何使用href进行此操作,然后在sql中打印出该行,但是,我不确定如何单独打印出每一行而不用将它们打印出来$行[ 'CATEGORY_NAME'。
Thank you for any help 感谢您的任何帮助
Something like? 就像是? (I'm not 100% sure if I understand your question)
(我不确定100%是否理解您的问题)
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="somelink.php?cat=' . $row['CATEGORY_NAME'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
UPDATE If a category is named playstation, playstation.php would be linked to... 更新如果一个类别名为playstation,playstation.php将链接到...
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="' . $row['CATEGORY_NAME'] . '.php">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
Though above would work a wild guess is that you're looking for something completely different. 尽管上面的方法很有效,但是您可能正在寻找完全不同的东西。 The thing you are looking for is essentially a way to present information for playstation, a way to present information about PS4 etc depending on what user clicks on!?
您正在寻找的东西本质上是一种呈现Playstation信息的方式,一种呈现PS4等信息的方式,具体取决于用户点击的内容!? In that case you should do something like this instead:
在这种情况下,您应该执行以下操作:
//Include ID of category in your sql-statement
$query = "SELECT ID, CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
....
....
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="presentinfo.php?id=' . $row['ID'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
and create presentinfo.php
file where you fetch information about the category, based on the id given in the url. 并根据URL中给定的ID创建
presentinfo.php
文件,在其中获取有关类别的信息。 (eg select from categories id={id given in url}
) (例如,
select from categories id={id given in url}
)
You could just store the link to which it needs to be redirected in a new column CATEGORY_URL
along with the CATEGORY_NAME
and then print it out like so: 您可以将需要重定向到的链接与
CATEGORY_URL
一起存储在新的列CATEGORY_NAME
,然后像这样打印出来:
$query = "SELECT CATEGORY_NAME, CATEGORY_URL FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($results);
if ($numrows > 0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '
<tr>
<td>
<a href="'.$row['CATEGORY_URL'].'">'.$row['CATEGORY_NAME'].'</a>
</td>
</tr>';
}
mysqli_free_result ($results);
} else {
// no results found
}
OR if you just want to pass it as a url parameter, you just need to modify if like so: 或者,如果您只想将其作为url参数传递,则只需进行如下修改:
<a href="https://www.google.com/#q='.$row['CATEGORY_NAME'].'">'.$row['CATEGORY_NAME'].'</a>
OR 要么
<a href="https://www.yoursite.com/'.$row['CATEGORY_NAME'].'.php">'.$row['CATEGORY_NAME'].'</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.