Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old July 12th, 2003, 05:01 PM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
A Framework for Persisting Data Relationships

Can anyone help me modify Jakes script from the article.

I have changed what I think is necessary to make it work, the form works and tells me to check the database but no records are added.

My database contains the following tables etc.

cd_artists
==========
Artist_ID
CompactDisc_Artist

cd_compactdiscs
===============
CompactDisc_ID
CompactDisc_Title

cd_artist2cd
============
Artist_ID
CompactDisc_ID

I think the problem is to do with the arrays.

php Code:
Original - php Code
  1.  
  2. <?php
  3. //cd.php
  4. if (isset($_POST['submit'])) {
  5. echo "submitting <br>";
  6. include_once("cd_classes.php");
  7.  
  8. // new Compactdisc:;
  9. $newCompactdisc = new Compactdisc;
  10. $newCompactdisc->title = ($_POST['title']);
  11. $newCompactdisc->PersistNew();
  12.  
  13. // new Artist
  14. $newArtist = new Artist;
  15. $newArtist ->artistname = ($_POST['artist']);
  16. $newArtist->PersistNew();
  17.  
  18. // new Link
  19. $newCompactdiscArtistLink = new CompactdiscArtistLink;
  20. $newCompactdiscArtistLink ->artist = $newArtist->id;
  21. $newCompactdiscArtistLink ->cd = $newCompactdisc->id;
  22. $newCompactdiscArtistLink->PersistNew();
  23.  
  24. echo "done, check out the database";
  25. } else {
  26. ?>
  27. <html>
  28. <form method="POST" action="cd.php">
  29. <table>
  30.   <tr>
  31.     <td>Compact Disc Title:</td>
  32.     <td><input name="title" /></td>
  33.   </tr>
  34.   <tr>
  35.     <td>Compact Disc Artist:</td>
  36.     <td><input name="artist" /></td>
  37.   </tr>
  38.   <tr>
  39.     <td><input type="submit" name="submit" value="submit" /></td>
  40.     <td><input type="reset" /></td>
  41.   </tr>
  42. </table>
  43. </form>
  44. </html>
  45. <?php
  46. }
  47. ?>
  48. =======
  49.  
  50. <?php
  51. // cd_classes.php
  52.  
  53. // A database persitable object, pretty useless unless you extend it.   
  54. class Persistable {
  55.  
  56.       // Unique database identifier.
  57.       var $id;
  58.       // Table name that is used in the database
  59.       var $table;
  60.       // Map of column labels in the database to object variables
  61.       var $columns = array();
  62.  
  63.       // Persist a new object in the database.
  64.       function PersistNew() {
  65.         mysql_connect('localhost', 'xxx', 'xxx')
  66.                   or die("Could not connect: " . mysql_error());
  67.         mysql_select_db('jamesmcl_cms')
  68.               or die ("Unable to select database");
  69.        
  70.         $sql = "INSERT INTO `".$this->table."` ";
  71.         $labels = "(`id` ";
  72.         $values = ") VALUES ('' ";
  73.  
  74.         foreach ($this->columns as $column) {
  75.           $labels .= ", `".$column[0]."` ";
  76.           $temp = '$this->'.$column[1];
  77.           $values .= ", '"."$temp"."' ";   
  78.           unset($temp);
  79.         }
  80.  
  81.         $sql .= $labels.$values.")";
  82.         eval ("$sql = "$sql";");
  83.         $result = mysql_query($sql);
  84.         $this->id = mysql_insert_id();
  85.       }
  86. }// End Class Persistable
  87.  
  88. class Book extends Persistable {
  89.  
  90.       var $title;//String of book title.
  91.  
  92.       function Book() { //Constructor for Book.
  93.         $this->table = 'test_books';
  94.         $this->columns = array( array('title','title')
  95.                               );
  96.       }
  97. } // End Class Book
  98.  
  99. class Author extends Persistable {
  100.    
  101.       var $fname; // String of author first name. 
  102.       var $lname; // String of author last name.
  103.    
  104.       function Author() { //Constructor for Author.
  105.         $this->table = 'test_authors';
  106.         $this->columns = array( array('firstname','fname'),
  107.                                 array('lastname','lname')
  108.                               );
  109.       }
  110. } // End Class Author
  111.  
  112. class BookAuthorLink extends Persistable {
  113.    
  114.       var $book; // integer book unique id. 
  115.       var $author; //  integer author unique id.
  116.    
  117.       function BookAuthorLink() { //Constructor for Author.
  118.         $this->table = 'test_bookauthorlink';
  119.         $this->columns = array( array('book','book'),
  120.                                 array('author','author')
  121.                               );
  122.       }
  123. } // End Class BookAuthorLink
  124.  
  125. class Compactdisc extends Persistable {
  126.  
  127.       var $title;//String of book title.
  128.  
  129.       function Compactdisc() { //Constructor for Compactdisc.
  130.         $this->table = 'cd_compactdiscs';
  131.         $this->columns = array( array('CompactDisc_Title','title')
  132.                               );
  133.       }
  134. } // End Class Compactdisc
  135.  
  136. class Artist extends Persistable {
  137.    
  138.       var $artistname; // String of artist name. 
  139.      
  140.    
  141.       function Artist() { //Constructor for Artist.
  142.         $this->table = 'cd_artists';
  143.         $this->columns = array( array('CompactDisc_Artist','artistname')
  144.                                );
  145.       }
  146. } // End Class Artist
  147.  
  148. class CompactdiscArtistLink extends Persistable {
  149.    
  150.       var $cd; // integer cd unique id. 
  151.       var $artist; //  integer artist unique id.
  152.    
  153.       function CompactdiscArtistLink() { //Constructor for CompactdiscArtist.
  154.         $this->table = 'cd_artist2cd';
  155.         $this->columns = array( array('cd','cd'),
  156.                                 array('artist','artist')
  157.                               );
  158.       }
  159. } // End Class CompactdiscArtistLink
  160. ?>

Reply With Quote
  #2  
Old July 14th, 2003, 05:18 AM
drevele drevele is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Medina, Ohio
Posts: 233 drevele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to drevele Send a message via Yahoo to drevele
RE: A Framework for Persisting Data Relationships

Im not the smartest at arrays but array(array(
dosent seem right.
Why not just use: array('bla', 'bla');
Instead of having:
$my_array = array(array('Test1,'Test2'););
Array -
1. array-
1. Test1
2. Test2
Why not have
$my_array = array('Test1', 'Test2');
Array-
1. Test1
2. Test2

It is alot simpler

Reply With Quote
  #3  
Old July 14th, 2003, 05:49 PM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: A Framework for Persisting Data Relationships

drevele,
the original code with the arrays works fine.

I have tried to change it to work with my cd tables and I cant see where I'm going wrong. I presume that its to do with the variables and database fields.

In the example

php Code:
Original - php Code
  1.  
  2. class Author extends Persistable {
  3.    
  4.       var $fname; // String of author first name. 
  5.       var $lname; // String of author last name.
  6.    
  7.       function Author() { //Constructor for Author.
  8.         $this->table = 'test_authors';
  9.         $this->columns = array( array('firstname','fname'),
  10.                                 array('lastname','lname')
  11.                               );
  12.       }
  13. } // End Class Author
  14.  


firstname and lastname are database fields

Any ideas

Reply With Quote
  #4  
Old July 14th, 2003, 10:40 PM
drevele drevele is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Medina, Ohio
Posts: 233 drevele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to drevele Send a message via Yahoo to drevele
RE: A Framework for Persisting Data Relationships

php Code:
Original - php Code
  1.       function PersistNew() {
  2.         mysql_connect('localhost', 'xxx', 'xxx')
  3.                   or die("Could not connect: " . mysql_error());
  4.         mysql_select_db('jamesmcl_cms')
  5.               or die ("Unable to select database");
  6.        
  7.         $sql = "INSERT INTO `".$this->table."` ";
  8.         $labels = "(`id` ";
  9.         $values = ") VALUES ('' ";
  10.  
  11.         foreach ($this->columns as $column) {
  12.           $labels .= ", `".$column[0]."` ";
  13.           $temp = '$this->'.$column[1];
  14.           $values .= ", '"."$temp"."' ";   
  15.           unset($temp);
  16.         }

When is $this->table defined?

Reply With Quote