【问题标题】:Display an image from the databse to results page将图像从数据库显示到结果页面
【发布时间】:2014-11-04 07:22:43
【问题描述】:

用户是否可以输入“红车”,它会在数据库和 php/html 中找到图像,并在搜索结果中显示图像?

我的“搜索”代码

<?php
    $query = $_GET['query']; 


    $min_length = 3;


    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what I'm looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

       if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

    while($results = mysql_fetch_array($raw_results)){
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop



    echo "<div class='successmate'><h2></h2></a>

    </div><br><br><br>";

echo "<div class='search69'><a href='../pages/{$results['page_name']}'><h2>{$results['title']}</h2></a><p>{$results['text']}</‌​p>";



    }


}
    else{ // if there is no matching rows do following
    echo ("<br><br><div class='search1'><h2>No results</h2></br></br>");
}

}

else{ // if query length is less than minimum
echo ("<br><br><div class='search1'><h2>Minnimum Length Is</h2><h2>".$min_length);
}

?>

我想为关键字找到的图像:

http://puu.sh/cCHv1/9f58d770f3.jpg

这些是数据库中图像的名称:

http://puu.sh/cCHwa/a82d2cc7fe.png

谢谢!

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    如果不进行测试,我会说您关闭了搜索功能,显示结果将是一件简单的事情,只需将以下代码放在您想要显示图像结果的位置(这仅用于显示图像,但让它与其他结果一起工作是一个简单的返工)

    <?php
    while($data = $result->fetch_assoc()){
        echo '<img src="'.$data['image_path'].'" alt="" title="" />';
    }
    ?>
    

    然后,如果您愿意,您可以简单地将图像包装在任何容器中,另一种方法是将结果存储到一个'数组中并使用 foreach 循环显示它

    <?php
    while($data = $result->fetch_assoc()){
       $imageArray[] = $data['image_path'];
    }
    
    /*Code below you can place where ever you want, no need to place it directly after the 
     above query execute*/
    
    foreach($imageArray as $imgPATH){
       echo '<img src="'.$imgPATH.'" alt="" title="" />';
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      按照我理解问题的方式,我会这样做

      编辑您的数据库表,以便在包含“ser_pic1.jpg”的图像之后获得“image_name”

      SELECT * FROM {your_database} WHERE image_name = "Red car"

      然后像这样发布

      link here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        相关资源
        最近更新 更多