Re: printing a one-to-many relationship

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$last_entry = 'an extremely unlikely entry';
while (list($entry, $note) = mysql_fetch_row($entries)){
  if ($entry != $last_entry){
    echo $entry;
    $last_entry = $entry;
  }
  echo $note;
}


On Wed, April 25, 2007 1:35 pm, Thufir wrote:
> There's a one-to-many relationship between px_items.id and
> contacts.id, and I
> want a printout of some data from each.
>
> I want to print out the query results px_items.id field once and once
> only, but
> the contacts.notes field many times, as there could be many entries.
>
> Right now it prints out each row, which is workable for now, but could
> get
> annoying.  I'm trying for something like
>
>
> entry one
>     note a
>     note b
>     note c
>
> entry two
>     note a
>
> entry three
>     notb a
>     note b
>
>
>
> Here's my schema and code:
>
> [root@localhost ~]#
> [root@localhost ~]# cat /var/www/html/items_notes.php -n
>      1  <html>
>      2  <head><title>items and notes</title></head>
>      3  <body>
>      4  <?php
>      5          $user="feeds";
>      6          $host="localhost";
>      7          $password="password";
>      8          $database = "feeds";
>      9
>     10          $connection = mysql_connect($host,$user,$password)
>     11                  or die ("couldn't connect to server");
>     12          $db = mysql_select_db($database,$connection)
>     13                  or die ("Couldn't select database");
>     14
>     15
>     16          $query = "SELECT * FROM contacts, px_items WHERE
> contacts.id=px_items.id ORDER BY px_items.id DESC";
>     17
>     18          $result = mysql_query($query)
>     19                  or die ("Couldn't execute query.");
>     20
>     21          while ($row = mysql_fetch_array($result))
>     22          {
>     23                  extract ($row);
>     24
>     25                  echo "<big><big><b>";
>     26                  echo $id;
>     27                  echo "</b></big></big>";
>     28
>     29                  echo "<br>";
>     30                  echo $title;
>     31
>     32                  echo "<br>";
>     33                  echo $content;
>     34                  echo "<br>";
>     35
>     36                  echo "<br>";
>     37                  echo $timestamp;
>     38                  echo "<br>";
>     39
>     40                  echo "<br>";
>     41                  echo $notes;
>     42                  echo "<br>";
>     43
>     44                  echo "<a href=\"";
>     45                  echo $link;
>     46                  echo "\">";
>     47                  echo $link;
>     48                  echo "</a>";
>     49
>     50                  echo "<br><br>";
>     51
>     52          }//while
>     53  ?>
>     54  </body>
>     55  </html>
> [root@localhost ~]#
> [root@localhost ~]#
> [root@localhost ~]#
> [root@localhost ~]# mysql -u feeds -p
> Enter password:
> Welcome to the MySQL monitor.  Commands end with ; or \g.
> Your MySQL connection id is 25 to server version: 5.0.27
>
> Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
>
> mysql> use feeds;
> Reading table information for completion of table and column names
> You can turn off this feature to get a quicker startup with -A
>
> Database changed
> mysql> show tables;
> +-----------------+
> | Tables_in_feeds |
> +-----------------+
> | contacts        |
> | px_feeds        |
> | px_items        |
> +-----------------+
> 3 rows in set (0.00 sec)
>
> mysql> describe contacts;
> +-----------+-----------+------+-----+-------------------+-------+
> | Field     | Type      | Null | Key | Default           | Extra |
> +-----------+-----------+------+-----+-------------------+-------+
> | id        | int(11)   | YES  | MUL | NULL              |       |
> | timestamp | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
> | notes     | longtext  | YES  |     | NULL              |       |
> +-----------+-----------+------+-----+-------------------+-------+
> 3 rows in set (0.07 sec)
>
> mysql> describe px_items;
> +-----------+--------------+------+-----+-------------------+----------------+
> | Field     | Type         | Null | Key | Default           | Extra
>       |
> +-----------+--------------+------+-----+-------------------+----------------+
> | id        | int(11)      | NO   | PRI | NULL              |
> auto_increment |
> | feed_id   | int(11)      | NO   | MUL | 0                 |
>       |
> | timestamp | timestamp    | NO   |     | CURRENT_TIMESTAMP |
>       |
> | link      | text         | YES  |     | NULL              |
>       |
> | title     | varchar(250) | YES  |     | NULL              |
>       |
> | content   | text         | YES  |     | NULL              |
>       |
> | dcdate    | text         | YES  |     | NULL              |
>       |
> | dccreator | text         | YES  |     | NULL              |
>       |
> | dcsubject | text         | YES  |     | NULL              |
>       |
> | read      | tinyint(4)   | YES  | MUL | NULL              |
>       |
> +-----------+--------------+------+-----+-------------------+----------------+
> 10 rows in set (0.00 sec)
>
> mysql> quit
> Bye
> [root@localhost ~]#
> [root@localhost ~]# date
> Wed Apr 25 19:30:38 BST 2007
> [root@localhost ~]#
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux