0

MySQL 表如下所示: 在此处输入图像描述

这是输出的代码:

<li>
    <a class="thumb" href="library/<?php echo $row2['projectname']; ?>/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" title="<?php echo $row2['projectrecordtitle']; ?>">
        <img src="library/<?php echo $row2['projectname']; ?>/thumbs/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" alt="<?php echo $row2['projectrecordtitle']; ?>" />
    </a>
    <div class="caption">
        <div class="image-title"><?php echo $row2['projectrecordtitle']; ?></div>
        <div class="image-desc"><?php echo $row2['projectrecorddesc']; ?></div>
        <div class="download">
            <a href="library/<?php echo $row2['projectname']; ?>/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" target="_blank">Download Original</a>
        </div>
    </div>
</li>

下面是输出的样子:

<li>

    <a class="thumb" href="library/ultima/2.jpg" title="Timbo DISPLACED">

        <img src="library/ultima/thumbs/2.jpg" alt="Timbo DISPLACED" />

    </a>

    <div class="caption">

        <div class="image-title">Timbo DISPLACED</div>

        <div class="image-desc">Made in 3ds Max, background, color and lens flare from Photoshop</div>

        <div class="download">

            <a href="library/ultima/2.jpg" target="_blank">Download Original</a>

        </div>

    </div>

</li>

我的问题是我找不到一种制作表单的方法,它会创建一些用户想要的字段;然后将每个文件信息上传并写入 mysql 数据库(请记住,文件的数量是不确定的)。

4

1 回答 1

1

这不是一个真正的具体问题,但我会尽力为您提供一个起点。

我假设你还没有表格...

仅供参考:不介意用htmlentities()或类似的东西逃避你的输出!

1)我使用了虚拟数据,但您可以将其替换为关联数据库结果集:

$rows = array(
    array(  'projectname' => 'ultima',
            'projectrecordnum' => '1',
            'projectrecordtitle' => 'The ball of destruction'),
    array(  'projectname' => 'ultima',
            'projectrecordnum' => '2',
            'projectrecordtitle' => 'Timbo DISPLACED'),
    array(  'projectname' => 'ultima',
            'projectrecordnum' => '3',
            'projectrecordtitle' => 'Exposure')
);

2) 我创建了一个简单的 HTML 表单,通过 PHP 在结果上循环:

<form method="post" action="#">
<?php foreach( $rows as $row ) { ?>
    <div class="caption">
        <div class="">
            <input type="text" name="projectname[]" value="<?php echo $row['projectname']; ?>">
        </div>
        <div class="">
            <input type="text" name="projectrecordnum[]" value="<?php echo $row['projectrecordnum']; ?>">
        </div>
        <div class="">
            <input type="text" name="projectrecordtitle[]" value="<?php echo $row['projectrecordtitle']; ?>">
        </div>
        <?php /* and so more fields ... */ ?>
    </div>
    <hr />
<?php } ?>
    <input type="submit" name="send" value="send" />
</form>

3)在action你可以重建你的$rows阵列:

if( isset($_REQUEST['send']) ) {
    $rows  = array();
    for( $i = 0; $i < count( $_POST['projectname'] ); $i++) {
        $row = array(
            'projectname' => $_POST['projectname'][$i],
            'projectrecordnum' => $_POST['projectrecordnum'][$i],
            'projectrecordtitle' => $_POST['projectrecordtitle'][$i],
        );
        if( ! empty( $_POST['projectname'][$i] ) ) { 
            array_push( $rows, $row );
        }
        unset($row);
    }

    // Your data array...
    print '<pre>'. print_r($rows, true) . '</pre>';
}
于 2012-07-31T14:49:13.553 回答