简体   繁体   English

需要将关联的ID添加到While循环(php)

[英]Need Associated ID Added to a While Loop (php)

Been trying to get my head around while loops for the last few days but the code seems very inefficient for what I'm trying to achieve. 最近几天一直在尝试让while循环前进,但是对于我要实现的目标,代码似乎效率很低。 I'm assuming I'm over-complicating this though nothing I've tried seems to work. 我假设我过于复杂了,尽管我尝试过的任何方法似乎都没有效果。

Each topic in my forum can have related topic IDs stored in a separate table. 论坛中的每个主题都可以将相关主题ID存储在单独的表中。 A post ID is also stored in this table, as that specific post references why they are considered related. 帖子ID也存储在此表中,因为特定帖子引用了为什么将其视为相关帖子。

DB Table contains only: topic_id , related_id , post_id 数据库表中只包含: topic_idrelated_idpost_id

// Get related IDs and post IDs for current topic being viewed
$result = $db->query('SELECT related_id, post_id FROM related_topics WHERE topic_id='.$id.'');
    // If related topics found, put both of the IDs into arrays
    if ($db->num_rows($result)) {
        while($cur_related = mysql_fetch_array($result)){
            $reltopicarray[] = $cur_related['related_id'];
            $relpost[] = $cur_related['post_id'];
        }

        // If the first array isnt empty, get some additional info about each related ID from another table
        if(!empty($reltopicarray)) {
            $pieces = $reltopicarray;
            $glued = "\"".implode('", "', $pieces)."\"";
            $fetchtopics = $db->query('SELECT id, subject, author, image, etc FROM topics WHERE id IN('.$glued.')');
        }

        // Print each related topic         
        while($related = mysql_fetch_array($fetchtopics)){ ?>

    <a href="view.php?id=<?php echo $related['id']; ?>"><?php echo $related['subject']; ?></a> by <?php echo $related['author']; ?>

    // Id like to show the Post ID below (from the array in the first while loop)
    // The below link doesnt work as Im outside the while loop by this point.
    <br /><a href="view.php?post_id=<?php echo $cur_related['post_id']; ?>">View Relationship</a>

<?php } ?>

The above currently works, however I'm trying to also display the post_id link below each related topic link, as shown above. 上面的代码当前有效,但是我试图也在每个相关主题链接的下方显示post_id链接,如上所示。

if you change the second while loop to something like this: 如果将第二个while循环更改为如下所示:

 
 
 
  
  <?php $i = 0; while($related = mysql_fetch_array($fetchtopics)){ //show view link // [...] //show the view related link ?> <a href="view.php?post_id=<?php echo $relpost[$i]['post_id']; ?>">View Relationship</a> <?php //increment the i so that you can get the next post in the next iteration of the loop $i++; } ?>
 
  

[sidenote] You probably should not be doing database queries in the same location you are generating the html for future-you's sanity. [sidenote]您可能不应该在为将来生成html的位置进行数据库查询,这很明智。 [/sidenote] [/边注]

[edit] [编辑]

You could also do it all as one query: 您也可以将其作为一个查询来完成:

SELECT related_topics.related_id, 
       related_topics.post_id, 
       related_topics.topic_id, 
       topics.subject, 
       topics.author, 
       topics.image, 
       topics.etc 
FROM related_topics 
LEFT JOIN topics ON topics.id = related_topics.topic_id
WHERE topic_id= $id

Then you only have to loop through it once for all of the links. 然后,您只需为所有链接循环一次即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM