What's the best way to achieve this please?

7 posts by 2 authors in: Forums > CMS Builder
Last Post: July 8, 2014   (RSS)

By willydoit - July 3, 2014

I have a section on a website which displays a selection of books by local authors, at present the cmsb section "Literary_corner" is made up of  a record for each book. 

The listing page is called literary_list.php. At present the listing page just shows an overview of each book with a link to a detail page for that book (literary_detail.php). what I would like to do is have an "About the author " link on the books detail page to a page where we provide a biography about the author with a listing and synopsis of all the books by that author in the same format as we have  already have on literary_list.php.

I was initially going to have "about the author" fields within the book record, however I soon realised that that would require authors with multiple books to have to enter this information on each book record which wasn't ideal so I am now thinking that it needs another section called "authors" with just the biography information in it with a record for each record. Having no experience in the design or implementation of databases or php this seems to be the logical setup but am concerned that we are separating this information into two separate sections when logic suggests that in reality it is one section but split into two subsections. So to start with advice on the basic framework I should be employing before I get to far down the wrong path would be appreciated.

If setting up two sections is the correct way to go then that is simple enough but then comes the question of how I create my page so that it shows the authors details taken from the author section followed by a list of their books taken from the section which contains all the books.

I am concerned that because I don't have the experience with cmsb that if I don't configure the sections in the right way I am going to make life ever increasingly difficult for myself when trying to get all the required data onto a single page.

I am assuming that what I want to achieve is well within the capabilities of cmsb but some basic assistance on how the best way to achieve the facility to have information on an author and all the books they have written and be able to present all that on a single page with the authors only having to enter biography info once and add new books when published would be appreciated. 

 I know I could stump up the money to get you to do this for me, but to be honest I have been using CMSB on different sites for years and I need to be able to get a better understanding of the basics so I can sort these things out for myself, I have looked through the forums, and have purchased the cmsb cookbook, but to be honest, perhaps its old age creeping in but I find that they all assume a basic level of knowledge that many of us just dont possess. I feel like I have bought myself a great car but have to employ someone else to drive it cos I cannot drive :-(

Thanks in advance for any help provided.

By claire - July 3, 2014 - edited: July 3, 2014

Hi Willy

First of all - yes, two sections is the right way to do it. A good rule of thumb is to think about how much information you'd be duplicating, if you're not sure how to structure your site. If you've got three books with the same author, for example, keeping it all in one section would mean the same info would be duplicated in each book record, and that's not good if you've got to update that info. It makes a lot more sense to have a section where you store the author info, and then use a few extra queries to connect them when you need to. Here's a step by step.

  • Make a section for the Author stuff, including name, biography and whatnot.
  • Then go to the Book section, and add a List field called Author ID.
  • Go to the Field options, and look at the List Options - choose 'Get options from database (advanced)'.
  • For the three dropdown menus that appear, choose the Author table, then use 'num' for option values and the author name for option labels.
  • Save it.
  • Add the author records there, and use the Book section dropdown to set an author for each book.

What this does is give you access to the identifier for the Author on the book's detail page, and you can use this to get the Author's info on that page from the Authors section.

On the book detail page, just before where you insert the 'About the Author' info, add something like this:

<?php
list($authorsRecords, $authorsMetaData) = getRecords(array(
  'tableName' => 'authors',
  'where' => 'num = ' . mysql_escape($bookRecord['author_id']),
  ));
$authorRecord = @$authorsRecords[0];
?>

Now you'll have the author information in $authorRecord, and you can echo it however you need to on the page. (I'm assuming that the info for this book is called $bookRecord, so just change that to whatever it should be.)

The next thing you'll want to do, which is to get a list of books by that author, means using the getRecords function again. So insert this just before you want to add a list of books by the author:

<?php
list($relatedBooksRecords, $relatedBooksMetaData) = getRecords(array(
  'tableName' => 'books',
  'where' => 'author = ' . mysql_escape($authorRecord['num']),
  ));
?>

So this should give you an array of books written by the author whose info you just pulled using the first chunk of code, and they're stored in $relatedBooksRecords. And now you can use a regular foreach loop to list them under the author's info in whatever format you like.

<?php foreach ($relatedBooksRecords as $record) : ?>
    <h3><?php echo $record['title']; ?></h3>
    <p><?php echo $record['publish_date']; ?></p>

    ... insert other info here...
<?php endforeach; ?>

Does this make sense? The end result of all this is that authors only need to add their record once, and then when they add a new book, they just use a dropdown to select their name. It's got some limitations if you have a large number of authors, but it's a simple implementation.

If there's anything you're not sure about, especially things like variables or loops in PHP, just let me know. I can point you to some good resources.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By willydoit - July 3, 2014

Hi Claire,

Due to the time difference am not in the office at the moment but just had to take the time to say Thank you very much, that info is exactly what I was looking for but didnt dare hope to get such a complete answer.

I will run through everything tomorrow but it looks exactly what I was wanting. Not only will it solve this problem but I can already see it opening doors to so many other possibilities.

Thanks again for all your help.

By willydoit - July 4, 2014

Hi Claire,

I had a couple of issues which basically took me about 4 hours to diagnose which was down to my lack of knowledge more than anything elese but they were resolved by simply changing 

 'where' => 'author = ' . mysql_escape($authorRecord['num']),

to 

'where' => 'author_id = ' . mysql_escape($authorsRecord['num']),

but 4 hours or not I was pleased that I had managed to eventually track the issue down and I generated a better understanding of things in the process.

Thanks for all your help.

By claire - July 4, 2014

No problem. Those variable names are probably the only thing you'll need to watch out for.

If you want to get to grips with more of this in depth, check out the PHP tutorial on W3CSchools: http://www.w3schools.com/PHP/

Take a look at for loops, arrays and the if/else stuff. Everything in the basic section is probably worth reading but those are what you'll use more than anything else. Also take a look at the PHP alternative syntax (just another way of running loops etc, which is easier to read when it's mixed up in HTML): http://www.php.net/manual/en/control-structures.alternative-syntax.php CMSBuilder uses this syntax in the Code Generator, so it's good to understand it.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By willydoit - July 8, 2014

Hi Claire,

Thanks for that, they will be a great source of reference and will probably save hours of head scratching :-)