2013-04-17 4 views
0

다른 테이블에서 제공된 ID에 따라 한 테이블에서 여러 행을 선택하려고합니다.PHP/MySQL 다중 테이블 선택

아래 코드를 사용하면 절반 밖에 안됩니다. 그러나 얼마나 많은 태그가 할당되어 있는지에 따라 각 블로그는 여러 번 반복됩니다. 어떻게하면 여러 태그가 하나의 사본에 표시되는지 알 수 있습니다. 블로그 게시물?

$sqlCommand = "SELECT blogid, blogtitle, content, blogtime, category, blogseourl, author FROM blog ORDER BY blogtime DESC"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$blogDisplay = ''; 
     while ($row = mysqli_fetch_array($query)) { 
     $blogid = $row["blogid"]; 
     $blogtitle = $row["blogtitle"]; 
     $content = $row["content"]; 
     $blogtime = $row["blogtime"]; 
     $category = $row["category"]; 
     $blogseourl = $row["blogseourl"]; 
     $author = $row["author"]; 
     $contentshort = substr($content, 0, 250); 
    $sqlCommand2 = "SELECT tag FROM blogtags WHERE blogid='$blogid'"; 
    $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());   
     while ($row = mysqli_fetch_array($query2)) { 
     $tag = $row['tag']; 

$blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' &#124; Category: ' . $category . ' &#124; Tags: ' . $tag . ' &#124; <a href="/blog/'. $blogseourl .'#disqus_thread"></a>'; 
     } 
     } 
mysqli_free_result($query); 

그래서 각 태그에 대해 여러 개의 $ blogDisplay를 표시하면서 모든 것이 올바르게 작동합니다.

누구나 아이디어가 있습니까?

+0

이 루프 내부 쿼리를 실행하는 가장 좋은 방법은되지 않습니다 : 또는 당신이 taglist를 버퍼 및 $ blogDisplay

첫 번째로 매개 변수로 삽입 할 수있는 가장 쉬운 방법입니다. 따라서 조인을 사용해야합니다. –

답변

0

blogDisplay를 두 섹션으로 나누고 그 사이에 탭을 나열해야합니다.

$sqlCommand = "SELECT blogid, blogtitle, content, blogtime, category, blogseourl, author FROM blog ORDER BY blogtime DESC"; 
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$blogDisplay = ''; 
     while ($row = mysqli_fetch_array($query)) { 
     $blogid = $row["blogid"]; 
     $blogtitle = $row["blogtitle"]; 
     $content = $row["content"]; 
     $blogtime = $row["blogtime"]; 
     $category = $row["category"]; 
     $blogseourl = $row["blogseourl"]; 
     $author = $row["author"]; 
     $contentshort = substr($content, 0, 250); 
    $sqlCommand2 = "SELECT tag FROM blogtags WHERE blogid='$blogid'"; 
    $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());   

/*first part, all the html before the taglist */ 
$blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' &#124; Category: ' . $category . ' &#124; Tags: '; 

     while ($row = mysqli_fetch_array($query2)) { 
     $tag = $row['tag']; 
/**add the taglist*/ 
$blogDisplay .= $tag.' '; 
     } 

/**last part, all the html after the taglist*/ 
$blogDisplay .= ' &#124; <a href="/blog/'. $blogseourl .'#disqus_thread"></a>'; 

     } 
mysqli_free_result($query);