arrays - PHP for loop duplicating on page refresh -
i have 2d array displaying in loop. code here:
foreach($products $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>"; }
i have 2 items in 2d array (eventually there 20). when load page table correctly displays 2 items, #1 & #2. if refresh page #1 still there duplicated underneath table looks like
#1
#2
#1
#2
and occurs again , again on every refresh. how can reset everytime new page opened? table has 26 items , growing , become difficult test rest of code broken.
edit:
i populating databse this:
$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 books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'robin nixon', 'learning php, mysql & javascript: jquery, css & html5','build interactive data-driven websites potent combination of open-source technologies , web standards', 9781491918661, 'o'reilly', '001')"); sqlite_query($db,"insert books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'reiersol et al', 'php in action','this book takes on important challenges of web programming in php', 9781932394757, 'greenwich', '002')"); $result=sqlite_query($db,"select * books"); $products = array(); while($row=sqlite_fetch_array($result,sqlite_assoc)) { $products[] = $row; } sqlite_close($db);
answer quite simple.
remove lines:
@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 books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'robin nixon', 'learning php, mysql & javascript: jquery, css & html5','build interactive data-driven websites potent combination of open-source technologies , web standards', 9781491918661, 'o'reilly', '001')"); sqlite_query($db,"insert books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'reiersol et al', 'php in action','this book takes on important challenges of web programming in php', 9781932394757, 'greenwich', '002')");
or comment them future use. each request inserting database. @
sign in front of create table suppresses error table books
exists. table won't created 'again' , records still insert
you encapsulate entire block in check whether table exists. if not exist, create populate 2 rows can test.
how check if table exists
$tablecheck = sqlite_array_query($db, "select name sqlite_master type='table' , 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 books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'robin nixon', 'learning php, mysql & javascript: jquery, css & html5','build interactive data-driven websites potent combination of open-source technologies , web standards', 9781491918661, 'o'reilly', '001')"); sqlite_query($db,"insert books (author, title, brief_synopsis, isbn_number, publisher, imgnumber) values ( 'reiersol et al', 'php in action','this book takes on important challenges of web programming in php', 9781932394757, 'greenwich', '002')"); }
Comments
Post a Comment