开发者

Search server for image and display it

The user on my site can currently search a mysql db via PHP, and the results are displayed on the same page inside a DIV, thanks to ajax..开发者_如何学Python.

What I need now is to display an image that is associated with the mysql results... Say the result has ID=250, then I want a piece of code to search in a folder for an image with the name 250.jpg on the server...

And then display this image in a table column using php script...

Here is my php script that displays the results as well as where I want the picture to appear...

Please help me...

   $qry_result = mysql_query($query) or die(mysql_error());

}
// Build Result String
$display_table = "<table align='center'>";

// Insert a new row in the table for each result
while($row = mysql_fetch_array($qry_result)){
$display_table .= "<tr>";
$display_table .= "<td class='ad_container' colspan='4'></td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td width='110' rowspan='2'>----- IMAGE HERE -----</td>";
$display_table .= "<td width='377' height='15'>$row[headline]</td>";
$display_table .= "<td width='67' rowspan='2'>$row[insert_date]</td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td height='15'>$row[price]:-</td>";
$display_table .= "</tr>";
}

$display_table .= "</table>";
echo $display_table;


This solution checks to ensure the image actually exists prior to display:

// absolute path to image directory
$path = '/var/www/vhosts/path/to/dir';

// basepath image directory
$basepath = '/path/to/dir';

// assuming you store JPEGs
$image = $row['id'] . '.jpg';

// start column output
$display_table .= '<td width="110" rowspan="2">';

// make sure image exists
if (file_exists($path . $image)) {
   $display_table .= '<img src="' . $basepath . $image . '" />';
}

// end column output
$display_table .= '</td>';


I think something like this would work:

$display_table .= "<td width='110' rowspan='2'><img src='/image/folder/" . $row["id"] . ".jpg'/></td>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜