0

Im trying to get links form a video database, the videos are displayed from a search form that have a clickable anchor tag, the source is saved into the database under 'link'. I can display the videos but i am having problems with displaying the link in the link for that video in the database. here is the code

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

Update full error:

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

Any help is super appreciated.

8
  • which line is throwing the notice ? Commented Jul 19, 2015 at 1:00
  • 79 sorry I forgot to mention that Commented Jul 19, 2015 at 1:02
  • as there is no line numbers here, could you please refer by code lines. Commented Jul 19, 2015 at 1:04
  • CTRL + F '<td><a href="' .$link. '"><strong><h6><u>Watch!</u></h6></strong></a></td>' thats the code that's having the problem Commented Jul 19, 2015 at 1:06
  • $link is array right ? then try to change it to <td><a href="' .$link[0]. '"><strong><h6><u>Watch!</u></h6></strong></a></td> Commented Jul 19, 2015 at 1:08

3 Answers 3

1

This will solve the problem :

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>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think that you forgot that the $link var is an array, where you have saved your videos links, that's why you got that error.

So to avoid that, you can use a for loop to get everytime the name and the link of the video like this :

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

Or you can use just one array to save your data, like this :

// ...

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']];
}

And then

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

Hope that can help.

1 Comment

Im sorry im super confused, could you make this one section of code and ill implement it all as one? please
0

try to change the for loop this way,

<?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++;
    }

?>

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.