-1

我正在开发一个 php 画廊。我正在使用 php 显示来自 mysql db 的图像,但我的图像正在一张一张地显示。这意味着第一行中的第一张图像和第二行中的第二张图像。但我想将我的图像显示为每行 3 或 4 个。我可以进行哪些编码更改。我的 php 代码如下所示。

<?php 
    include_once("config.php");
    $result=mysql_query("SELECT * FROM images");
    while($res=mysql_fetch_array($result)){ ?>
        <table width='200'>
            <tr>
                <td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
            </tr>
            <tr>
                <td>
                    <div id="news-image">
                        <?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
                        <?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
                    </div>
                </td>
            </tr>
        </table>
 <?php } ?>   
4

7 回答 7

1

array_chunk()是一个函数,用于将数组拆分为 X 项的集合,以便您循环遍历而不必保留计数器(如果需要填充,您可以在列表中的最后一项上使用 array_pad())

if($array = array_chunk(mysql_fetch_assoc($result),4))
{
  foreach($array as $row)
  {
    echo '<div class="row">';
    foreach($row as $col)
    {
      echo '<div class="item">' . $col['image'] . '</div>';
    }
    echo '</div>';
  }
}
于 2012-05-10T04:47:54.607 回答
0

按照这个例子

<table>
<tr>
<?php
$i = 1;
do{
    echo "<td>" . $i . "</td>";
    //Num of Columns
    if( $i%3 == 0 ){
        echo "</tr><tr>";
    }

    $i++;
}while($i<=10);
?>
</tr>
<table>

将返回您想要的结果..

在此处输入图像描述

于 2012-05-10T04:49:25.107 回答
0
<?php 
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
    <?php 
         $cnt = 0;
         while($res=mysql_fetch_array($result))
         { 
        if($cnt == 3){
        echo "</tr><tr>";
        }
          ?>
        <td>
    <table width='200'>
        <tr>
            <td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
        </tr>
        <tr>
            <td>
                <div id="news-image">
                    <?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
                    <?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
                </div>
            </td>
        </tr>
    </table>
</td>
 <?php
$cnt++;
  }
 ?>
</tr>
</table>
于 2012-05-10T04:50:18.130 回答
0

您可以改为将图像输出为 div 列表,或者只是 div,然后使用 CSS 在两列中显示图像。你的布局不应该是硬连线的。

<style>
    div.gallery {
        width: 650px;
    }
    div.gallery ul li {
        list-style: none;
        float: left;
    }
    div.image {
        height: 500px;
        width: 300px;
    }
</style>
<div class="gallery">
    <ul>
        <li>
            <div class="image">
                <span class="image_title">Some title</span><br/>
                <img src="foo.png"/>
            </div>
        </li>
        <li>
            <div class="image">
                <span class="image_title">Another title</span><br/>
                <img src="bar.png"/>
            </div>
        </li>
        <li>
            <div class="image">
                <span class="image_title">Another title</span><br/>
                <img src="foo.png"/>
            </div>
        </li>
        <li>
            <div class="image">
                <span class="image_title">Another title</span><br/>
                <img src="bar.png"/>
            </div>
        </li>
        <li>
            <div class="image">
                <span class="image_title">Another title</span><br/>
                <img src="foo.png"/>
            </div>
        </li>
    </ul>
</div>

结果:

在此处输入图像描述

您的代码应类似于:

<div class="gallery">
    <ul>

<?php 
include_once("config.php");
$result = mysql_query("SELECT * FROM images");
while($res = mysql_fetch_array($result)) {
?>
        <li>
            <div class="image">
                <a class="image_title" href="indimage.php?imageid=<?php echo $res['imageid']?>"><?php echo $res['imagename']?></a><br/>
                <a href="indimage.php?imageid=<?php echo $res['imageid']?>"><img src="<?php echo $res['image']?>" /></a>
            </div>
        </li>
<?php
}
?>
    </ul>
</div>
于 2012-05-10T04:59:26.827 回答
0

您正在为每个图像输出一个表格。至少,您的代码应该更像这样:

<table>
<tr>
<?php while($res etc...) { ?>
<td>
   <img src="<?php echo ......?>" />
</td>
<?php } ?>
</tr>
</table>

现在,您将在单个表格的单行中获取所有图像。使其具有多行留给 OP 作为练习。

于 2012-05-10T04:46:14.280 回答
0

使用以下代码。

<?php 
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
<?
$varcount=0;
while($res=mysql_fetch_array($result))
{
 $varcount++;
 if($varcount == 4) // Count of images per row. 3 or 4 
 {
      $varcount=0;
 ?>
  </tr><tr>
 <?
 } 
?>

 <td> 
 <table width='200'>
 <tr>
 <td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
 </tr>
 <tr>
 <td>
 <div id="news-image">
 <?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
 <?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
 </div>
 </td>
 </tr>
 </table>
 </td> 
 <?php
 }
?> 
</tr>
</table>
于 2012-05-10T04:46:18.150 回答
0

这是我认为您想要的解决方案:共有三页。1. index.php(有上传图片的表格) 2.upload.php(保存图片在目录和数据库中的路径) 3. showimage.php(最后显示图片)

这是代码 (index.php)

<form method="post" action="upload.php" enctype="multipart/form-data">

<label>Choose File to Upload:</label><br />

<input type="hidden" name="id" />

<input type="file" name="uploadimage" /><br />
<input type="submit" value="upload" />

</form>

(上传.php)

<?php
$target_Folder = "upload/"; // directory where images will be saved



$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

    $file_name = $_FILES['uploadimage']['name'];

    if(file_exists('upload/'.$file_name))
{
    echo "That File Already Exisit";
    }
    else
    {

        // Database 

        $con=mysqli_connect("localhost","user_name","pasword","database"); // Change it if required
        //Check Connection
        if(mysqli_connect_errno())
        {
            echo "Failed to connect to database" .     mysqli_connect_errno();
        }

        $sql = "INSERT INTO image (id,image, image_name)
                    VALUES ('$uid','$target_Folder$file_name','$file_name') ";

        if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
        echo "1 record added successfully in the database";
        echo '<br />';
        mysqli_close($con);

        // Move the file into UPLOAD folder

        move_uploaded_file( $_FILES['uploadimage']['tmp_name'],     $target_Path );

        echo "File Uploaded <br />";
        echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
        echo '<br />';  
        echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
        echo'<br />';
        echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
        echo'<br />';
        echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

    }
?>

<a href="showimage.php">Show Image</a>

(showimage.php)

<?php


$con=mysqli_connect("localhost","user_name","password","database_name"); // Change it if required

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM image " );


while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['image'] . '" width="200" />';
echo'<br /><br />';  
}


mysqli_close($con);

?>

特征

如果该名称文件已经存在,它将检查文件的名称,它不会上传文件并提醒用户。

数据库结构

id int(4) 自动增量 - image varchar(100) - image_name varchar(50)

于 2013-11-14T09:11:44.490 回答