dongwupei7803 2014-11-19 02:01
浏览 13
已采纳

PHP动态数组显示双

I am currently working on a media playlist site, two separate pages for audio and video.

This is playlist.php that is dynamically loaded into the page using javascript:

$sql = new mysqli('localhost', 'root', '', 'bunnytwitchy');
if (mysqli_connect_errno()) {
  printf("Connect failed: %s
", mysqli_connect_error());
  exit;
}

$query = "SELECT * FROM `playlist` WHERE `playlistname`='playlist 1'";
$result = $sql->query($query);     
if (!$result) {
  printf("Query failed: %s
", $mysqli->error);
  exit;
}

while($row = $result->fetch_row()) {
  $rows[]=$row;
}

foreach($rows as $row) {
  $mediaid = $row[1];
  $query = "SELECT * FROM `media` WHERE `id`='$mediaid'";
  $result = $sql->query($query);
  if (!$result) {
    printf("Query failed: %s
", $mysqli->error);
    exit;
  }

  while($playlistlist = $result->fetch_row()) {
    $playlist[]= $playlistlist;
    print_r($playlist);

  }
}

The reason I have select * from both tables, I'm collecting information from both the playlist table and the media table.

While getting both tables information, im trying to compare the playlist song id's with the media id's so it knows which songs and info go in the playlist.

My DB structure is:

db: bunnytwitchy
tables: playlist, media
playlist: playlistid, mediaid, playlistname
media: id, type, band, song, album, path

In the above code, playlistid is compared with the media id's to know which songs are in the playlist. Media type is to determine video / music. Path is the media file location.

Right now my array of the media information is working great, other than it displays the first matched id twice:

Array
(
    [0] => Array
        (
            [0] => 3
            [1] => video
            [2] => band2
            [3] => song2
            [4] => album1
            [5] => video/3.mp4
        )

)
Array
(
    [0] => Array
        (
            [0] => 3
            [1] => video
            [2] => band2
            [3] => song2
            [4] => album1
            [5] => video/3.mp4
        )

    [1] => Array
        (
            [0] => 1
            [1] => video
            [2] => bandname
            [3] => songname
            [4] => album
            [5] => video/2.mp4
        )

)

The reason id 2 is missing is because it's a music file rather than video.

I was hoping someone out there could help me figure out why it's displaying the first matched id twice in the array and possibly how I can fix it

All help is appreciated! Thanks

  • 写回答

1条回答 默认 最新

  • doumou3883 2014-11-19 02:10
    关注

    It's because you have print_r($playlist); within the loop. If you had 3 playlists, it would show 3 arrays instead (1, 1+2, 1+2+3).

    Move it outside:

    while($playlistlist = $result->fetch_row()) {
        $playlist[]= $playlistlist;
    }
    print_r($playlist);
    

    Or even better, move it outside the foreach($rows as $row) { .. } loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算