Adding Custom Fields to BellaBiblio1
Adding Custom Fields to BellaBiblio1
There's more to books than the fields that are included in BellaBiblio by default, so by following this tutorial you can add whatever fields you like! If you're not confident with editing PHP scripts I'd recommend backing up your files before starting — in fact, back up anyway, it's always good to have a spare copy.
Firstly, it will help to understand how BellaBiblio works... In the text file ("books.txt") each field is separated by a comma (no spaces either side). When reading this file, we can split the file into parts based on this comma. This means that we have to have the right amount of commas to get the right amount of corresponding fields.
Preparing books.txt
If you've already got books in your file, you will need to manually edit the books.txt file to add the additional comma to each row, for each field you need. By default, a row should look a bit like this:
status,isbn,publisher,year,title,author,genre,page-amount,rating,"review"
You should add your custom field at the end of each, separating it from review with — you guessed it — a comma. So, if I wanted to add a field for "book cover" (i.e. the location of an image of the book) I would do so like this:
status,isbn,publisher,year,title,author,genre,page-amount,rating,"review",cover
Displaying our Custom Field
Save books.txt and close the file. Now we need to get down and dirty with a bit of PHP. Open index.php and look for the line that splits each row into its respective parts. This should be approximately line 64, and looks just like this:
list($status,$isbn,$publisher,$year,$title,$author,$genre,$pageamt,$rating,$review) = preg_split("/,(?! )/", $bookfile[$i]);
We need to add a variable (stick to letters and numbers only) to the list to represent our new custom field, separating by a comma just like in the text file. Assuming we're still thinking of adding a book cover field, we can do this like so:
list($status,$isbn,$publisher,$year,$title,$author,$genre,$pageamt,$rating,$review,$cover) = preg_split("/,(?! )/", $bookfile[$i]);
You can now modify the HTML coding to suit, using <?php echo $cover; ?> (or whatever you named your variable as) to display the contents of this field from the books file. If you actually are adding a book cover field, you'll want to add this with an <img /> tag, like so: <img src="<?php echo $cover; ?>" alt="book cover" /> Save index.php and close.
That's the easiest part of the modification — we're now displaying our custom field on our BellaBiblio index. The hard part is modifying the admin control panel so that we can manage our custom fields and add them with new books without having to manually edit books.txt.
Modifying the Add Book Form
Open admin.php and look for the Add Book form. This is near the top of the file, starts with <form action="admin.php?ap=add_book" method="post"><p> on approximately line 59. Somewhere between <- that line of code, and the closing </form> you need to add an input field, select box, checkbox/whatever that we can use to input the data that will be stored in the custom field. Whatever type you use, make sure you name (and id) it the same as your variable in index.php that we created earlier, for consistency.
Assuming we're still adding a cover, we'd want an input field just like this:
<input type="text" name="cover" id="cover" />
You now need to edit the PHP that will process this and I recommend doing this slowly, and carefully. Scroll up admin.php to line 33 where we prepare the data and give it commas before adding it to books.txt — this starts a bit like: $bookFormat = $c['status']. and ends $c['review'].'"'."\n"; It's at the end that we need to put our data, before the new line (\n).
Because the data is automatically cleaned after we press submit, whatever we enter into the form field we created earlier will end up in the $c array, based on the name and id of that field. So, we shall edit $c['review'].'"'."\n"; (mentioned in the previous paragraph) like so: $c['review'].'",'.$c['cover']."\n"; (if you're not using cover, change that to whatever your variable/field is called!) This small modification concatenates (ooh, posh word, means "adds" basically) our new field to the row before the script adds it to the file. Sounds more complex than it is really.
We're now at the stage where we can see our custom field, add data to our custom field without manually editing books.txt but what about modifying it in the future? That's the next step...
Modifying the Edit Book Form
Still in admin.php, scroll down to the Edit Book form. This starts on line 195 with the following line:
<form action="admin.php?ap=edit_book&b=<?php echo $bookid; ?>" method="post"><p>
Just above this line is a familiar bit of code — another list function. Just like index.php we need to add our custom variable to the list:
list($status,$isbn,$publisher,$year,$title,$author,$genre,$pageamt,$rating,$review,$cover) = preg_split("/,(?! )/", $bookfile[$i]);
Now copy and paste your input field from the Add Book form somewhere into the Edit Book form and add a value attribute to it — it's in this value attribute that we'll echo our custom variable that we've just added to the list, that calls the current value of that field from the text field (in case we're editing something else and don't want to edit our custom field). This will look a bit like this:
<input type="text" name="cover" id="cover" value="<?php echo $cover; ?>" />
Once you've done this, scroll back up and look for the line starting $bookarray[$bookid] = $c['status'] (approx. 166). On this line we will concatenate (remember: add) our custom field again, just like our add form processing code. Change $c['review'].'"'."\n"; at the end of the line to $c['review'].'",'.$c['cover']."\n";
Save admin.php and close the file. Upload the modified index.php, admin.php and books.txt because that's it! You're done, and you now have a custom field in your BellaBiblio.
Tags:
bellabiblio, script-modding, php,
Last Updated On: 17th May 07 by Jem
Bookmark At: StumbleUpon, Digg

Handy Stuff
Downloads
Friends of 'TT'
Resources