PHP Booosta 3 Tutorial

PHP Booosta 3 Tutorial

Adding fields to the database

Maybe during development of your application you figure out that you want to add an additional field to a database table. And of course your application should work with this new field. This is very easy to achieve. Just add the field to the database table and add a form field to the templates for adding and editing the data. If you do not have additional code that works with the data this is all to do.
In our example we add a field to the table course that determines the maximal number of students - which means in fact maximum number of registrations. First, add the field to the database table:

ALTER TABLE `course` ADD `max_reg` INT NULL ;
Now add the form field to the templates tpl/course_new.tpl and tpl/course_edit.tpl:
{BBOXCENTER}
{BPANEL|paneltitle::New Course}

{BFORMSTART|course.php|onSubmit::return checkForm();}
{HIDDEN action newdo}
{HIDDEN|lecturer|{%lecturer}}

{BTEXT|name|texttitle::Name}
{BTEXT|starttime|texttitle::Starttime}
{BTEXT|endtime|texttitle::Endtime}
{BTEXT|max_reg|texttitle::Max. Registrations}
{BTEXTAREA|description|40|10|textareatitle::Description}

{BFORMSUBMIT class::center-block}
{BFORMEND}

{/BPANEL}
{/BBOXCENTER}
It does not matter where you put this line inside the form tags.

{BBOXCENTER}
{BPANEL|paneltitle::Edit Course}

{BFORMSTART course.php}
{HIDDEN action editdo}
{HIDDEN id {%id}}

{BTEXT|name|{%name}|texttitle::Name}
{BTEXT|starttime|{%starttime}|texttitle::Starttime}
{BTEXT|endtime|{%endtime}|texttitle::Endtime}
{BTEXT|max_reg|{%max_reg}|texttitle::Max. Registrations}
{BTEXTAREA|description|40|10|textareatitle::Description
{%description}}

{BFORMSUBMIT class::center-block}
{BFORMEND}
{/BPANEL}
{/BBOXCENTER}

Make sure that the name of the field and the variable are the same as the name of the database field! Now go to the course administration in your admin area and save some numbers in the new field in your existing courses. Also create new courses with this field. You see it works despite we have not altered any PHP code for that!


Previous - Next