搜索已加载的数据并在数据库中显示类似的项目

时间:2017-09-20 14:21:31

标签: php mysql pdo

我目前有一个名为news.php的页面,此页面显示我的新闻文章。我现在计划允许我的用户搜索数据库,防止不断滚动。在这样做的过程中,我创建了一个名为search.php的第二个文件。我对这个页面的意图是加载文章' LIKE'我在此页面的news.php中提交的表单。我的error.log中没有错误,我无法提交信息并加载search.php显示我的结果。我已经找到了关于mysqli的信息,但没有找到PDO的信息。简而言之,我如何将用户带到search.php并在news.php的表单中向他们的提交显示类似的信息?

php在search.php上显示在news.php上的html格式

<div id="news">
<?php 
    if(isset($_POST['submit-search'])){
    $search = $_POST['search'];
    $q = $handler->prepare("SELECT * FROM articles WHERE headline LIKE '%$search%' OR dateTime LIKE '%$    search%' OR text LIKE '%$search%'");
    $q->execute(array($search));

      if ($q->rowCount() > 0){
        while ($result = $q -> fetch(PDO::FETCH_ASSOC)); {
            echo '<div class="col-md-4 col-xs-12 col-sm-12 height-news">';
            echo '<p class="news-title">'.$results[$i]['headline'].'<br>'.'</p>';
            echo '<img class="news-img" src="data:image/png;base64,'.base64_encode( $results[$i]['logo']).    '"/>';
            echo '<p class="news-time">'.$results[$i]['dateTime'].'<br>'.'</p>';
            echo '<p class="news-body">'.$results[$i]['text'].'</p>';
            echo '<button class="news-btn" id="myBtn" onclick="showFull(this)">Read More</button>'.'</div>    ';
          }
        if ($result == 0) {
            echo '<p class="error-message3">Sorry there is no results!</p>';
        }  
    }
}
?>

<div class="searchPanel">

<div id="x-gon">
  <i class="fa fa-close"></i>
</div>

<form action="search.php" id="content" method="POST">
<input  type="text" name="search" type="text" class="input420"></input>


<submit id="CupidsArrow" class="fa fa-chevron-right fa-4x" type="submit" name="submit-search" value="Submit"></submit>
</form>
</div>

2 个答案:

答案 0 :(得分:0)

问题在于:

<i id="CupidsArrow" class="fa fa-chevron-right fa-4x" name="submit-search"></i></a>

此处您使用i标记提交带有a标记的表单。而</a>也在那里,但它的开头标记丢失了。所以不要这样,试试:

<input type="submit" name="submit-search" value="Submit" />

input类型submit会将表单提交到表单操作中指定的网址。

前:

<form action="search.php" id="content" method="POST">
  <input />
  <input type="submit" value="Submit" />
</form>

答案 1 :(得分:0)

    var modal = document.getElementById('myModal');
    var btn = document.getElementById("myBtn");
    var span = document.getElementsByClassName("close")[0];

    function showFull(button) {
      modal.style.display = "block";
      $('.dimmer').show();
      var newsText = $(button).parent().find(".news-title")[0];
      $(".news-title2").text(newsText.textContent);
      var newsDate = $(button).parent().find(".news-time")[0];
      $(".news-time2").text(newsDate.textContent);
      var newsBody = $(button).parent().find(".news-body")[0];
      $(".news-body2").text(newsBody.textContent);
      var newsImg = $(button).parent().find(".news-img")[0].src;
      $('.news-img2').attr("src", newsImg);
    }

    span.onclick = function() {
        modal.style.display = "none";
    }

    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }

    

         

<div class="searchPanel animated bounceInLeft">

<div id="x-gon">
  <i class="fa fa-close"></i>
</div>

<form action="search.php" id="content" method="POST">
<input type="text" name="search" type="text" class="input420"></input>

<input type="submit" name="submit-search" value="Submit" id="CupidsArrow" class="fa fa-chevron-right fa-4x"></input>
</form>
</div>