PHP图片库

时间:2011-06-22 21:46:13

标签: php mysql image loops galleria

我已经设置了一个新的图库,其中图像存储在/ images /文件夹中的服务器上,相应的细节存储在mysql数据库中(标题,描述,imagesrc)。

我要做的是将服务器上文件夹中存储的所有图像拉回来,而不必在html中列出它们。我已经让php拉回正确的字段并正确填充图库,但是当一个新图像被添加到库或其中一个原始图像被更新时,它显然不会在我的网站上通过。

我的代码是:

<div id="galleria"><!-- Begin Galleria -->
        <div>
                <a href="<?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['imagesrc'];
  }

mysql_close($con);
?>">
                <img src="<?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['imagesrc'];
  }

mysql_close($con);
?>" alt='' title='' />
        </a>
        <strong><?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['title'];
  }

mysql_close($con);
?></strong>
        <span><?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['desc'];
  }

mysql_close($con);
?></span>
        </div>
        <div>
                <a href="<?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['imagesrc'];
  }

mysql_close($con);
?>">
                <img src="<?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['imagesrc'];
  }

mysql_close($con);
?>" alt='' title='' />
        </a>
        <strong><?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['title'];
  }

mysql_close($con);
?></strong>
        <span><?php mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images WHERE id='1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['desc'];
  }

mysql_close($con);
?></span>
        </div>
        <div>

在循环中检索此信息的最佳方法是什么 - 我不是非常熟练的PHP(正如您从我的代码中看到的那样!)所以我将非常感谢有关构建此类循环的任何帮助和指导该库基于文件夹中的所有图像。

谢谢!

JD

3 个答案:

答案 0 :(得分:0)

您不需要再次执行查询只需执行一次,当在while循环中获取thr记录时,使用if(如果需要)根据您的要求放置div。我正在向您展示我最近使用的代码示例。

<?php
$conn=mysqli_connect(DBHOST,DBUSER,"",DB);
$query1="select dev_image,dev_name from developers";
$result=mysqli_query($conn,$query1);
$cnt=0;
while($row=mysqli_fetch_array($result))
    //$dev_name=row['dev_name'];
{               
   $dev_image=$row['dev_image'];
   $dev_name=$row['dev_name'];
    if($cnt%4==0) {
    echo "<div class=\"project_main\">";
}
if($cnt%4==0) { 
        echo "<div class=\"project_img_main\">";
        echo "<div class=\"project_img1\">";
        echo "<img src=\"$dev_image\" alt=\"\" title=\"Project-1\" border=\"none\" />";
        echo "</div>";
        echo "<div class=\"project_img_name\">";
        echo "<p align=\"center\" class=\"txt1\">".$dev_name."</p>";
        echo "</div>";
    echo "</div>";
} else {
        echo "<div class=\"project_img_main1\">";
        echo "<div class=\"project_img1\">";
        echo "<img src=\"$dev_image\" alt=\"\" title=\"Project-1\" border=\"none\" />";
    echo "</div>";
    echo "<div class=\"project_img_name\">";
        echo "<p align=\"center\" class=\"txt1\">".$dev_name."</p>";
    echo "</div>";
        echo "</div>";
}

$cnt++;
if($cnt%4==0) {
    echo "</div>";
}
}

?>              

我希望它对你有用..只是相应的。

感谢
拉杰什

答案 1 :(得分:0)

您正在通过mysql_close()关闭MySQL连接,因此这不起作用。您要执行的步骤是:

mysql_connect
mysql_select_db
mysql_query (or multiple queries, if placement matters)
mysql_close

答案 2 :(得分:0)

对于像这样的简单查询,您可以遍历整个表并在一个while循环中填充您的库,如下所示:

<?php

mysql_select_db("dbname", $con);

$result = mysql_query("SELECT * FROM images");

while($row = mysql_fetch_array($result))
{
    echo '<a href="' . $row['imagesrc'] . '">' . $row['title'] . '</a>';
}

mysql_close($con);
?>

打印哪些:

<a href="imagesrc1">Imagetitle1</a>
<a href="imagesrc2">Imagetitle2</a>
<a href="imagesrc3">Imagetitle3</a>
...

你可以在while循环中混合你需要的任何div,span和其他字段(比如$ row ['desc']),例如。

while($row = mysql_fetch_array($result))
{
    echo '<div>';
    echo '<a href="' . $row['imagesrc'] . '">';
    echo '<img src="' . $row['imagesrc'] . '" />';
    echo '</a>';
    echo '<strong>' . $row['title'] . '</strong>';
    echo '<span>' . $row['desc'] . '</span>';
    echo '</div>';
}

哪个会打印:

<div>
    <a href="imagesrc1"><img src="imagesrc1" /></a>
    <strong>imagetitle1</strong>
    <span>desc1</span>
</div>
<div>
    <a href="imagesrc2"><img src="imagesrc2" /></a>
    <strong>imagetitle2</strong>
    <span>desc2</span>
</div>
<div>
    <a href="imagesrc3"><img src="imagesrc3" /></a>
    <strong>imagetitle3</strong>
    <span>desc3</span>
</div>

希望这有帮助!