2

誰かがコードを接続するのを手伝ってくれますか??

私が欲しいのは、これを行うコードです。画像が存在する場合はそれを印刷し、そうでない場合は何も印刷しません。(このコードは画像を見つけるために使用されます)そして、ifステートメントを他のコードのステートメントに接続するのに助けが必要です

<?php
$pathToFileOnDisk = 'inventory_images/' . $id . '.jpg';
if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
<img  src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" />
}
else {
// NO IMG
}
?>

<?php 
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM content ORDER BY id DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){ 
         $id = $row["id"];
         $name = $row["name"];
         $content = $row["content"];
         $date_added = strftime("%d %b, %Y", strtotime($row["date_added"]));
         $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
         <td>
         <div id="left">

                <img  src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" />  <-- !!THIS IS WHAT I WANNA REPLACE!!
            </div> <!-- End Left-->
    </td>
    <div id="right">


      <td width="630px" valign="top"><h2><a id="overskrift" href="product.php?id=' . $id . '">' . $name . '</a></h2>
      <p><b>
        Skrevet den ' . $date_added . '<br />
        ' . $content . '</b></p>
        </td>
        </div>
    </tr>
  </table><br />';
}
} else {
$dynamicList = "Ingen inlegg enda..";
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
    <div id="wrapper">
        <header>
            <?php include_once("template_header.php");?>
        </header>  <!-- End Header -->
        <div id="banner"></div>
        <div class="content test clearfix">


            <?php echo $dynamicList; ?>
            </div> <!-- End Content -->

        <footer>
            <?php include_once("template_footer.php");?>
        </footer> <!-- End Footer -->

    </div> <!-- End Wrapper -->
</body>

4

3 に答える 3

0

The file_exists and is_readable functions are perfect for this purpose.

For example:

<?php
    $pathToFileOnDisk = '/full/system/path/to/image.jpg';
    if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
        // The file exists and can be read
    }
    else {
        // The file doesn't exist (or does exist, but PHP can't read it).
        // Perhaps output a placeholder image here.
    }
?>

Incidentally, I'd recommend a quick glance at all of the filesystem functions, as the time spent on this now will pay dividends in the future.

于 2013-07-09T16:02:56.417 に答える