Page 1 of 1
Dropdown inputfield with data from database
Posted: Sun Aug 25, 2019 10:49 am
by musicscore
Hi you all
I have (for you a small) problem.
I'm creating my first module. This module has two database tables.
1 named categorys and one name questions.
Now I want to make a input form for the questions.
In that form there should be a dropdown field and that field should be filed with the categories from the category table.
I search the forum but did not find a solution.
I'm now using a text field :
Code: Select all
<div class="pageoverflow">
<p class="pagetext">{$mod->Lang('category')}:</p>
<p class="pageinput">
<input type="text" name="{$actionid}category" value="{$question->category}"/>
</p>
</div>
But this field should be a dropdown and the values in this dropdown should be all the category's from the category table.
Please help.
TIA
(PS. If the module is ready I want to upload it to CMSMadeSimple module section)
Re: Dropdown inputfield with data from database
Posted: Sun Aug 25, 2019 1:00 pm
by calguy1000
Now you need to get into general programming.
CMSMS Provides a library to allow querying and inserting data into the database. You do not need to worry about establishing a new connection if you are going to use the same database that CMSMS is using, and just add tables. This is what we normally do.
The steps are:
Query the database,
parse the results.
The query depends on your tables and table layouts... queries can be simple or very complex.
But here's generally how I get a list of categories and their ids:
Code: Select all
$sql = 'SELECT id, name FROM '.CMS_DB_PREFIX.'my_table_suffix ORDER BY name ASC';
$arr = $db->GetArray($sql);
if( !$arr ) return;
$out = null;
foreach( $arr as $row ) {
$out[(int)$row['id']] = trim($row['name']);
}
return $out;
[Solved] Dropdown inputfield with data from database
Posted: Tue Aug 27, 2019 5:15 pm
by musicscore
calguy1000
Thank you for your response.
I was not shure if there was a special CMSMS command.
I will use your advise and do my best toi get it to work.
(I'm learning)
Thanks again
Re: Dropdown inputfield with data from database
Posted: Wed Aug 28, 2019 7:21 pm
by musicscore
Hey you all
I'm almost there but after trying and trying the fiekd is not filling correctly
I used the code :
Code: Select all
$sql = 'SELECT SQL_CALC_FOUND_ROWS H.* FROM '.CMS_DB_PREFIX.'mod_msfaq_category H ORDER BY category_name';
$arr = \cms_utils::get_db();
if( !$arr ) return;
$out = null;
$out = array();
foreach( $arr as $row ) {
$out[(int)$row['id']] = trim($row['catgeory_name']);
}
$categorylist = array();
$smarty->assign('categorylist',$out)
The part of my template looks like this :
Code: Select all
<div class="pageoverflow">
<p class="pagetext">{$mod->Lang('category')}:</p>
<p class="pageinput">
<select class="selectCategory" name="category">
{html_options options=$categorylist selected={$question->category}}
</select>
</p>
</div>
Please help. What is wrong ?
TIA
[Solved] Dropdown inputfield with data from database
Posted: Fri Aug 30, 2019 9:41 am
by musicscore
YES YES. I did it. Thanks to the advise from calguy1000.
THANK YOU
Re: Dropdown inputfield with data from database
Posted: Fri Aug 30, 2019 4:25 pm
by DIGI3
To help others, perhaps post the solution and mark that post as 'solved'.
Re: Dropdown inputfield with data from database
Posted: Sat Aug 31, 2019 8:35 am
by musicscore
Thank you DIGI3 for this advise. Together we can make cmsms better.
Here is my solution:
The part is the dropdown in my template looks like this :
Code: Select all
<div class="pageoverflow">
<p class="pagetext">{$mod->Lang('category')}:</p>
<p class="pageinput">
<select class="selectCategory" name="{$actionid}category">
{html_options options=$categorylist selected={$question->category}}
</select>
</p>
</div>
And the part in the php file 'action.edit_questions.php' looks like this :
Code: Select all
#Fill category selection field array
$sql = 'SELECT id, category_name FROM '.CMS_DB_PREFIX.'mod_msfaq_category ORDER BY category_name ASC';
$arr = $db->GetArray($sql);
if( !$arr ) return;
$out = null;
foreach( $arr as $row ) {
$out[(int)$row['id']] = trim($row['category_name']);
}
$smarty->assign('categorylist',$out);
I'm a real newby in making modules for cmsms. I'm not a programmer and have to learn a lot about making these modules.
I will make a document about the things a learned and add some examples. After I finish the module I will post this document. Maybe this will help other new module developer because I think they will face the same problems.