2015-07-19 86 views
0

我試圖從一個視頻數據庫獲取鏈接,視頻顯示從一個搜索窗體有一個可點擊的錨標記,源被保存到'鏈接'的數據庫。我可以顯示視頻,但是我在數據庫中顯示該視頻鏈接中的鏈接時遇到問題。這裏是代碼注意:數組到字符串轉換時,與兩個數組

<?php 

    $names = array(); 
    $link = array(); 

    if(isset($_POST['searchterm'])) { 
     mysql_connect("localhost", "root", "Oliver"); 
     mysql_select_db("videos"); 

     $search = mysql_real_escape_string(trim($_POST['searchterm'])); 

     $find_videos = mysql_query("SELECT * FROM `videos` WHERE `keywords` LIKE'%$search%'"); 
     while ($row = mysql_fetch_assoc($find_videos)) { 
      $names[] = $row['name']; 
      $link[] = $row['link']; 
     } 

    } 

    include('session.php'); 

?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <link href="http://vjs.zencdn.net/4.12/video-js.css" rel="stylesheet"> 
    <link rel="icon" type="image/ico" href="images/favicon.ico"> 
<script src="http://vjs.zencdn.net/4.12/video.js"></script> 
<link href="http://vjs.zencdn.net/4.12/video-js.css" rel="stylesheet"> 
<script src="http://vjs.zencdn.net/4.12/video.js"></script> 
<style type="text/css"> 
    .vjs-default-skin .vjs-control-bar { font-size: 125% } 
</style> 
    <meta charset="utf-8"> 
    <title>Network TV | search</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <link rel="stylesheet" href="css/bootstrap.css" /> 
    <link href="css/font-awesome.css" rel="stylesheet" /> 
    <link href="css/3.1.1/animate.css" rel="stylesheet" /> 
    <link rel="stylesheet" href="css/styles.css" /> 
    </head> 
    <body style="overflow-x: hidden"> 
    <nav class="navbar navbar-default navbar-fixed-top"> 
     <div class="container"> 
     <div class="navbar-header"> 
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
      <span class="sr-only">Toggle navigation</span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      </button> 
      <a class="navbar-brand" href="#">Network TV</a> 
     </div> 
     <div id="navbar" class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav"> 
      <li class=""><a href="\1\index.php">Home</a></li> 
      </ul> 
     </div><!--/.nav-collapse --> 
     </div> 
    </nav> 
     <section style="padding-top:100px; width:100%"class="container-fluid" id="section2"> 
        <h1 class="text-center" style="color:white">Network TV</h1> 
       <h3 class="text-center" style="color:white"><u>Search results</u></h3> 



             <table align="center" class="table" style="color: white; width:50%"> 
               <thead> 
                <tr> 
                 <th>Movie name</th> 
                </tr> 
               </thead> 
               <tbody> 
                <tr> 
                 <?php 
                  foreach($names as $name) { 
                   echo '<tr> 
                    <td>' . $name . '</td> 
                    <td><a href="' .$link. '"><strong><h6><u>Watch!</u></h6></strong></a></td> 
                   </tr>'; 
                  } 
                  ?> 
                </tr> 
               </tbody> 
              </table> 
     </section> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 

    <script src="js/scripts.js"></script> 
</html> 

更新 完整的錯誤:

Notice: Array to string conversion in C:\xampp\htdocs\1\search.php on line 79

任何幫助是非常讚賞。

+0

哪一行拋出的通知? – NorthernLights

+0

79對不起,我忘記提及 –

+0

,因爲這裏沒有行號,請問請參考代碼行。 – NorthernLights

回答

1

這將解決這個問題:

for($i=0; $i<count($link); $i++){ 
    echo '<tr><td>' . $name[$i] . '</td><td><a href="' .$link[$i]. '"><strong><h6><u>Watch!</u></h6></strong></a></td> </tr>'; 
} 
0

試圖改變for循環這種方式,

<?php 

    $i=0;  
    foreach($names as $name) { 
     echo '<tr> 
      <td>' . $name . '</td> 
      <td><a href="' .$link[$i]. '"><strong><h6><u>Watch!</u></h6></strong></a></td></tr>'; 
     $i++; 
    } 

?> 
0

我想你忘記了$link變種是一個數組,您保存您的視頻的鏈接,這就是爲什麼你得到了錯誤。

因此,爲了避免這種情況,你可以使用一個for循環得到每次的名稱和視頻的像這樣的鏈接:

for($i = 0; $i < count($names); $i++) 
{ 
    echo '<tr>' 
      .'<td>' . $names[$i] . '</td>' 
      .'<td><a href="' . $links[$i] . '"> Watch! </a></td>' 
     .'</tr>'; 
} 

或者你可以只使用一個數組來保存你的數據,這樣的:

// ... 

while ($row = mysql_fetch_assoc($find_videos)) { 
    $videos[] = ['name' => $row['name'], 'link' => $row['link']]; 
    // if you don't want to use indexes, you can simply do : 
    // $videos[] = [$row['name'], $row['link']]; 
} 

然後

foreach ($videos as $video) { 
    echo '<tr>' 
      .'<td>' . $video['name'] . '</td>' 
      .'<td><a href="' . $video['link'] . '"> Watch! </a></td>' 
     .'</tr>'; 
} 

希望能有所幫助。

+0

Im sorry對於超級困惑,你能否將這一段代碼和惡意代碼實現爲一個整體?請 –

相關問題