PHP for循环复制页面刷新

时间:2015-03-17 09:29:43

标签: php arrays foreach

我有一个2d数组,我在for循环中显示。这个代码在这里:

foreach($products as $id => $product) {
        echo "<tr>
            <td style='border-bottom:1px solid #000000;'><a href='./index.php?view_product=$id'>" . $product['Book_Code'] . "</a></td>
            <td style='border-bottom:1px solid #000000;'>$" . $product['Title'] . "</td> 
            <td style='border-bottom:1px solid #000000;'>" . $product['Author'] . "</td>
        </tr>";
    }
    echo "</table>";
}

我目前在我的2d阵列中有两个项目(最终会有20个)。当我加载页面时,我的表正确地显示了两个项目,#1&amp; #2。如果我刷新页面#1&amp;#2仍然存在,但也在下面复制,所以表格看起来像

#1
 #2
 #1
 #2

这会在每次刷新时反复出现。如何在每次打开新页面时重置此项?我的表目前有26个项目并且还在不断增长,而且我的代码的其余部分很难被测试。

编辑:

我正在填充这样的数据库:

$db = sqlite_open ("products.db", 0666, $error);

@sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);


sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");




$result=sqlite_query($db,"SELECT * from Books");
$products = array();
while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
{
    $products[] = $row;
}

sqlite_close($db);

1 个答案:

答案 0 :(得分:1)

答案很简单。

删除行:

@sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);


sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");

或评论它们以备将来使用。您要插入数据库的每个请求。 创建表前面的@符号会抑制表Books已存在的错误。因此,表格不会再次被创造出来。并且记录仍会插入

您还可以在检查表是否存在时封装整个块。如果它不存在,则创建一个包含2行的填充,以便进行测试。

如何检查表是否存在

$tableCheck = sqlite_array_query($db, "SELECT name FROM sqlite_master WHERE type='table' AND name='Books'");

if(1 > count($tableCheck)) {

     sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);

    sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
    sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");
}