2015-11-30 70 views
3

我有兩個數據庫表格released_moviesupcoming_movies。我需要在release_movies表中顯示字段rel_moviesupcoming_movies表中的up_movies字段表內的單個dropdown列表。以下是我的代碼。使用php顯示下拉列表中的兩個不同表格的值

<select name="film" class="form-control"> 
<?php 
include "connection.php"; 
$sql="select rel_movies from released_movies"; 
$sqlr=$con->query($sql); 
while($row=$sqlr->fetch_assoc()) 
{ 
?> 
<option value="<?php echo $row['rel_movies'];?>"><?php echo $row['rel_movies'];?></option> 
<?php 
} 
?> 
</select> 

我不知道如何在其中顯示up_movies字段。這是可能的。提前感謝。

+1

顯示與你在第一次下拉完成的位置相同.. –

回答

1

試試這個

<?php include "connection.php"; ?> 
    <select name="film" class="form-control"> 
    <?php 
     $sqlr="select rel_movies from released_movies"; 
     $sqlrr=$con->query($sqlr); 
     while($row=$sqlrr->fetch_assoc()) { ?> 
    <option value="<?php echo $row['rel_movies'];?>"> 
<?php echo $row['rel_movies'];?> 
</option> 
    <?php } 
     $sqlu="select up_movies from upcoming_movies"; 
     $sqluu=$con->query($sqlu); 
     while($rowu=$sqluu->fetch_assoc()){ ?> 
    <option value="<?php echo $rowu['up_movies'];?>"> 
<?php echo $rowu['up_movies'];?> 
</option> 
    <?php } ?> 
    </select> 
+0

謝謝:)它工作:) – Rose

4

您可以使用UNION在MySQL從多個表中獲取記錄。
在你的情況下,你可以使用UNION獲得發佈以及即將上映的電影,不需要執行兩個MySQL查詢。關於聯盟

<select name="film" class="form-control"> 
<?php 
include "connection.php"; 
$sql="select rel_movies as movies_name from released_movies 
UNION select up_movies as movies_name from upcoming_movies"; 
$sqlr=$con->query($sql); 
while($row=$sqlr->fetch_assoc()) 
{ 
?> 
<option value="<?php echo $row['movies_name'];?>"><?php echo $row['movies_name'];?></option> 
<?php 
} 
?> 
</select> 

更多信息:UNION

0

有不止一個方面做到這一點。

如果你喜歡即將上映的電影首先或最後在陣列中,你可以做這樣的事情。首先將數據庫中的結果返回到數組中,而不是單個實例中。只有這樣你才能和他們做點什麼

function fetch_all($result, $resulttype = MYSQLI_ASSOC){ 
    if (method_exists($result, 'fetch_all')){ // Compatibility layer with PHP < 5.3 
     $res = mysqli_fetch_all($result, $resulttype); 
    } 
    else{ 
     for ($res = array(); $tmp = $this->fetch_array($resulttype);) $res[] = $tmp; 
    } 

    return $res; 
} 


$sqlr=$con->query($sql); 
$relMovieArray = fetch_all($sqlr,'MYSQLI_NUM'); 

// for putting the array's one after the other. 

for($x = 0; $x < count($upMovieArray); $x++){ 
    $relMovieArray[count($relMovieArray)] = $upMovieArray[$x]; 
} 


//Or you could use array_merge. That just pushes two array's into each other. 

$movieArray = array_merge($relMovieArray, $upMovieArray); 
相關問題