0

我正在尝试从 mysql 获取信息,包括一个 blob 图像,我将用 php 回显该图像,并将在 php 中有一个 onclick 事件,将其重定向到另一个页面。onlick 事件将包含一个 mysql 结果,它会随身携带,如下面的代码所示。

我的主要问题是代码的语法,或者是否有另一种方法可以一起完成。请记住,脚本运行时的输出与谷歌图片、必应图片等类似。谢谢。

<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM movies ORDER BY  `movies`.`title` ASC");

echo "<table border='3' style='margin: auto; text-align: left;background: white;      padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<th><b>Language</b></th>
</tr>";

while($row = mysqli_fetch_array($result))
 {
  echo "<tr>";
  echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' .  base64_encode( $row['image'] ) . '"  width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
 echo "</tr>";
 }
echo "</table>";

mysqli_close($con);
?>  
4

2 回答 2

0

未经测试,但这是一种可以将代码移动样式和 javascript(并使用一些 jquery)清理到头部的方法:

<?php
    $con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
    $result = mysqli_query($con,"SELECT * FROM movies ORDER BY  `movies`.`title` ASC");
?>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $('img').each(function() {
                    var img = $(this);
                    img.click(function() {
                        window.location="lookup.php?pattern=" + img.attr('title');
                    });
                });
            });
        </script>
        <style>
            table {
                margin: auto;
                text-align: left;
                background: white;
                padding: 3em;
                border: 2px solid #000000;
            }
            table tr td {
                padding-right: 2em;
            }
            table tr td img {
                width: 160px;
                height: 200px;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th>Movie Title</th>
                <th>Language</th>
            </tr>
            <?php
                while($row = mysqli_fetch_array($result)) {
                    echo "
                        <tr>
                            <td>
                                <img src=\"data:image/jpeg;base64," .  base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
                            </td>
                        </tr>
                    ";
                }
            ?>
        </table>
    </body>
</html>
<?php mysqli_close($con); ?>

或者,如果您不想使用 javascript,则可以始终将图像包裹在锚标记周围:

<td>
  <a href='lookup.php?pattern={$row['title']}'>
    <img src=\"data:image/jpeg;base64," .  base64_encode( $row['image'] ) . "\">
  </a>
</td>
于 2013-08-26T20:40:55.797 回答
0

您可以进一步分离 PHP 和 HTML 代码:

<?php
    $con=mysqli_connect("localhost","root","*******","media");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        die();
    }
    $result = mysqli_query($con,"SELECT * FROM movies ORDER BY  `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white;      padding: 3em;'>
    <tr>
        <th><b>Movie Title</b></th>
        <th><b>Language</b></th>
    </tr>
<?php
    while($row = mysqli_fetch_array($result)) {
        $img = 'data:image/jpeg;base64,'
             . base64_encode( $row['image'] );
?>
<tr>
  <td style='padding-right: 2em;'>
      <img src="<?php echo $img; ?>"
          style="width: 160px; height: 200px;"
          onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
      />
  </td>
</tr>
<?php } ?>
</table>

<?php
    mysqli_close($con);
?>

您也可以使用某种模板引擎来执行此操作,但结果几乎相同 - 我认为写作没有多大意义,比如说,

     <strong>{{ title }}</strong>

代替

     <strong><?php echo $title; ?></strong>
于 2013-08-26T20:45:21.257 回答