Dropdown inputfield with data from database

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
musicscore
Power Poster
Power Poster
Posts: 444
Joined: Wed Jan 25, 2006 11:53 am
Location: Netherlands

Dropdown inputfield with data from database

Post 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)
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Dropdown inputfield with data from database

Post 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;
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
musicscore
Power Poster
Power Poster
Posts: 444
Joined: Wed Jan 25, 2006 11:53 am
Location: Netherlands

[Solved] Dropdown inputfield with data from database

Post 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
musicscore
Power Poster
Power Poster
Posts: 444
Joined: Wed Jan 25, 2006 11:53 am
Location: Netherlands

Re: Dropdown inputfield with data from database

Post 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
musicscore
Power Poster
Power Poster
Posts: 444
Joined: Wed Jan 25, 2006 11:53 am
Location: Netherlands

[Solved] Dropdown inputfield with data from database

Post by musicscore »

YES YES. I did it. Thanks to the advise from calguy1000.

THANK YOU
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1606
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Dropdown inputfield with data from database

Post by DIGI3 »

To help others, perhaps post the solution and mark that post as 'solved'.
Not getting the answer you need? CMSMS support options
musicscore
Power Poster
Power Poster
Posts: 444
Joined: Wed Jan 25, 2006 11:53 am
Location: Netherlands

Re: Dropdown inputfield with data from database

Post 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.
Post Reply

Return to “Modules/Add-Ons”