显示图像数组 - PHP

时间:2012-05-05 07:39:21

标签: php ajax jquery

我有两个问题: - 问题编号:1(PHP相关)
我无法在PHP页面上显示图像数组。这是我正在尝试的代码。
getUser.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'sulen', '123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("holidayNet", $con);

$sql="SELECT * FROM image WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Picture</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";


  $dir = 'images';
$file_display = array('jpg','jpeg','png','gif');

if (file_exists ($dir) == false) {
    echo 'Directory \'', $dir, '\' not found!';
}
else{
    $dir_contents = scandir($dir);


    foreach($dir_contents as $file) {
        $file_type = strtolower(end(explode('.', $file)));

        If($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == 

true) {
            echo "<td>" . $row ['<img src="', $dir, '/', $file, '" alt="', $file, '" />'] . "</td>";

        }       

    }

}
  /*echo "<td>" . $row['Job'] . "</td>";*/
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 
<br />

one.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Sulman qb</option>
<option value="2">asd asd</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

问题2(与AJAX相关)
您可能已经在上面的代码中看到,在使用AJAX时,我正在从数据库中检索图像目录路径。现在,我将如何给php文件getUser.php或

$dir = 'images'(need that path here !);

$dir = 'images'(need that path here !);

1 个答案:

答案 0 :(得分:0)

这条线错了:

 echo "<td>" . $row ['<img src="', $dir, '/', $file, '" alt="', $file, '" />'] . "</td>";

为什么不呢,为什么你使用$ row []来显示图像,$ row用于检索数据库中对应于表字段的值。 $ row ['table field'];

echo '<td>';
echo '<img src =';
echo $dir."/".$file;
echo  '/>';
echo '</td>';

也试试

$dir = "images/";