3

I have two database tables, released_movies and upcoming_movies. I need to display the field rel_movies in release_movies table and up_movies field in upcoming_movies table inside a single dropdown list. Below is my code.

<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>

I have no idea how to display up_movies field into it. Is this possible .Thanks in advance.

1
  • 1
    display same as where you have done as in first dropdown.. Commented Nov 30, 2015 at 5:04

3 Answers 3

4

You may use UNION in mysql for getting records from more than one table.
In your case you may use UNION for getting released as well as upcoming movies, no need of execution of two MySQL query.

<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>

More info about UNION: UNION

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

    <?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>

Comments

0

There are more ways then one to accomplish this.

If you like to have the upcoming movies first or last in the array you could do something like this. First get your result from the database back in an array, and not in single instances. Only then you can do something with them

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.