Results 1 to 7 of 7
Hybrid View
-
7th May 2010 02:51 #1Registered User
Join Date: Oct:2005
Location: ,
Posts: 122
PHP: multidimensional array MySQL
,
. PHP. , web interface. , .
:
books, authors, categories, authors2books, categories2books...
authors2books categories2books foreign keys books authors books.bookISBN authors.authorID many2many relationship.
, .
SQL query $books array, bookISBN bookTitle subarray Authors bookISBN, authorID name . , loop- array PHP . iteration pass iteration . - , screenshot , , , .
, , .
Code:<?php include_once('../../includes/mysql.pdo.inc.php'); // include_once('../../includes/restrict_admin.inc.php'); include_once('../../includes/corefuncs.php'); // create database connection $conn = dbConnect('admin'); // Get the number of records found $sql = 'SELECT COUNT(*) FROM books'; // submit the query and capture the result $result = $conn->query($sql); $error = $conn->errorInfo(); if (isset($error[2])) die($error[2]); // find out how many records were retrieved $numRows = $result->fetchColumn(); // free the database resources $result->closeCursor(); // prepare second SQL query $sql = "SELECT books.*, authors.authorID, authors.FirstName, authors.surName, categories.categoryID, categories.categoryName FROM books LEFT JOIN authors2books on books.bookISBN = authors2books.bookID LEFT JOIN authors on authors.authorID = authors2books.authorID LEFT join categories2books on books.bookISBN = categories2books.bookISBN LEFT JOIN categories on categories.categoryID = categories2books.categoryID"; // display all feched rows from the database foreach ($conn->query($sql) as $row) { // join the first name and the surname of every author together $name = $row['FirstName'].' '.$row['surName']; // create an array that stores information about all books $books[$row['bookISBN']] ['bookTitle'] = $row['title']; $books[$row['bookISBN']] ['authors'] [$row['authorID']] = $name; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <table border='0'> <tr> <td>ISBN:</td> <td>Title:</td> <td>Authors:</td> </tr> <tr> <?php foreach ( $books as $bookISBN => $book) { ?> <td><?php echo $bookISBN; ?></td> <td><?php echo $book['bookTitle']; ?></td> <td><?php foreach ( $book['authors'] as $authorID => $FirstName) { echo "$FirstName \r\n"; echo $authors['surName']; } ?></td> </tr> <?php } } ?> </table> </body> </html>
-
7th May 2010 04:42 #2
-
7th May 2010 05:19 #3
-
7th May 2010 06:54 #4Registered User
Join Date: Oct:2005
Location: ,
Posts: 122
-
7th May 2010 08:34 #5
-
7th May 2010 10:00 #6
!

:
Code:<tr> <?php foreach ( $books as $bookISBN => $book) { ?> <td><?php echo $bookISBN; ?></td> <td><?php echo $book['bookTitle']; ?></td> <td><?php foreach ( $book['authors'] as $authorID => $FirstName) { echo "$FirstName \r\n"; echo $authors['surName']; } ?></td> </tr> <?php } } ?>
Code:<?php foreach ( $books as $bookISBN => $book) { ?> <tr> <td><?php echo $bookISBN; ?></td> <td><?php echo $book['bookTitle']; ?></td> <td><?php foreach ( $book['authors'] as $authorID => $FirstName) { echo "$FirstName \r\n"; echo $authors['surName']; } ?></td> </tr> <?php } } ?>EP 9NPA+ U (HR-05/IFX)| 4200+X2@2.8GHz (IFX-14) | 2x1GB KINGSTON (DDR430 3-4-3-6-1T@2.7V) | GB 8800GT (512MB) | WD6401AALS | CFT-750-14CS
-
7th May 2010 14:25 #7Registered User
Join Date: Oct:2005
Location: ,
Posts: 122
Oldman, TEKIL4O, ! ok!




Reply With Quote


Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in