Results 1 to 7 of 7

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered 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>

  2. #2
    ! vbTheKing's Avatar
    Join Date: Sep:2003
    Location:
    Posts: 4,138
    . - (.. , ).
    ''? * *
    - !
    ...

  3. #3
    TEKIL4O's Avatar
    Join Date: Feb:2007
    Location: Rousse/Sofia
    Posts: 341
    Quote Originally Posted by vbTheKing View Post
    . - (.. , ).
    , ... ,
    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

  4. #4
    Registered User
    Join Date: Oct:2005
    Location: ,
    Posts: 122
    Quote Originally Posted by TEKIL4O View Post
    , ... ,
    :

    Book 1 Author 1, 2,
    book 2 author 2, 3

    :

    Book 1 Author 1, 2,
    book 2 author 2, 3
    book 3 author 1, 3

    - :

    Book 1 Author 1, 2,
    book 2 author 2, 3
    book 3 author 1, 3
    book 4 author 4

    .. :

    Book 1 Author 1, 2,
    book 2 author 2, 3
    book 3 author 1, 3
    book 4 author 4

    , -.

  5. #5
    Undead user Oldman's Avatar
    Join Date: Oct:2003
    Location: outside
    Posts: 432
    "hint": foreach.
    .

  6. #6
    TEKIL4O's Avatar
    Join Date: Feb:2007
    Location: Rousse/Sofia
    Posts: 341
    Quote Originally Posted by Oldman View Post
    "hint": foreach.
    !


    :
    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

  7. #7
    Registered User
    Join Date: Oct:2005
    Location: ,
    Posts: 122
    Oldman, TEKIL4O, ! ok!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |