Open Source & Stuff  
  |     Main Site     |      Forum     |     Contact     |
Ajax Linked Select boxes :  forum.Salix.gr forum.Salix.gr
Comments, questions, usage, bugs. 
Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
messagedescr instead of id in <option value="
Posted by: bunker (IP Logged)
Date: May 10, 2007 01:08PM

Hello again,

Is it possible to put descr instead of the id number in the output html?

like:

<option value="apple">Apple</option>

instead of

<option value="23">Apple</option>

if it is possible can you give a clue?

Thanks, regards..

messageRe: descr instead of id in <option value="
Posted by: panos (IP Logged)
Date: May 11, 2007 03:39PM

Welcome back smiling smiley

asume that you already know that description should be unique to work as key....

add_select_box(db_table_name, key_field_name, description_field_name, order_field_name, selected_value, tag_name, tag_id, [extra_tag_attributes] , [extra_where])

tried to use key_field_name same as description_field_name ?

havent tested... but logicaly it should work.

Let me know.

Cheers!

messageRe: descr instead of id in <option value="
Posted by: bunker (IP Logged)
Date: May 14, 2007 04:57AM

I thought those two IDs should be unique...

messageRe: descr instead of id in <option value="
Posted by: bunker (IP Logged)
Date: May 14, 2007 05:16AM

it doesnt work that way..

the problem is that, when i record something to the database using these linked select boxes, the recorded value is a number that represents the selection, but later when i try to call that value it should be somehow converted into the description cause numbers do not make sense to the end users.

example:

when the user selects: Fruits ---> Apples

and when it is saves to the database and when it is called to show to the user what he had selected this happens:

12 ---> 234

and that is not useful for the end user.

I hope we can solve this..

any clues?

messageRe: descr instead of id in <option value="
Posted by: bunker (IP Logged)
Date: May 14, 2007 05:30AM

I edited the part below, now it does what I want but the link action is not working properly

When I select an option from the first box, it fills the next box with the values of the first box instead of its own values.



foreach($list as $key => $text) {
$line = '<option class="liste" value="'.$text.'" ';
if( $this->_is_key_selected($key,$selected) )
$line .= ' SELECTED ';
$line .= '>'.$text.'</option>';
echo $line."\n";
}
echo '</select>'."\n";
} // end function add_select_box -------------------------------------

messageRe: descr instead of id in <option value="
Posted by: panos (IP Logged)
Date: May 14, 2007 07:39AM

One sec....

you want to show stored values in html right?
like:
"User A selected Fruits and prefers Apples"

in that case you should use a helper function to get the text for the number value from the db.

Some thing like i do. I have class named DB_helper which holds functions like that.


    	function get_link_table_values($val, $table, $key_fld, $val_fld, $link_type, $list_format=0) {
    		$out = '';
    		if( $link_type=='single' ) {
    			$cmd= "SELECT $val_fld FROM $table WHERE $key_fld=$val";
    			$row = $this->getRecords($cmd);
    			if( count($row)!=0 )
    				$out = $row[0][0];
    		}
    		else {
    			if( trim($val)!='' ) {
    				$vals = explode(',',$val);
    				$cmd= "SELECT $val_fld FROM $table WHERE";
    				if( count($vals)!=0 ) {
    					for($i=0;$i<count($vals); $i++ ) {
    						$cmd .= " $key_fld=".$vals[$i];
    						$cmd .= ' OR ';
    					}
    					$cmd = substr($cmd,0,strlen($cmd)-4);
    					$rows = $this->getRecords($cmd);
    					if( count($rows)!=0 ) {
    						for($i=0;$i<count($rows); $i++ ) {
    							if( $list_format==0 ) {
    								$out .= '- '.$rows[$i][0].'<br>';
    							}
    							else {
    								if( $out!='' )
    									$out.=',';
    								$out .=$rows[$i][0];
    							}
    						}
    					}
    				}
    			}
    			
    		}
    		return($out);
    	}   // ----------------------------------------------------------------

$val = selected value
$table = db table
$key_fld = key field, the number in our case
$val_fld = the field with the text
$link_type = 'single' or 'multi', if you want to get many values from the table, comma separated
$list_format= 0 or 1, if 0 returns the results like
- Text text <br>
- Text text <br>
.....

if 1 comma separated.

for a single value stored in a table named food with columns food_id, food_description
to get food_id=12, call it like

echo get_link_table_values(12, 'foods', 'food_id', 'food_description', 'single', 0);

messageRe: descr instead of id in <option value="
Posted by: panos (IP Logged)
Date: May 14, 2007 07:49AM

hmmm, or you can do some sql powered thinky winking smiley

eg if record stored in table named user_prefs and linked fields are main_food_id, sub_food_id then you can do some joins like

select up.user_pref_id, f1.food_description as main_food, f2.food_description as sub_food
from user_prefs as up
left join foods as f1 on up.main_food_id=f1.food_id
left join foods as f2 on up.sub_food_id=f2.food_id
where user=1231

helped?

messageRe: descr instead of id in <option value="
Posted by: bunker (IP Logged)
Date: May 15, 2007 10:34AM

Yes helped, thank you

Cheers!

messageRe: descr instead of id in <option value="
Posted by: KursadOlmez (IP Logged)
Date: January 11, 2010 06:02PM

Hi Panos,

I'm using your script, it works well and it's great. You saved my days. Really thank you!

But, I want to use description in the value instead of id, too. I read your suggestions and I do not understand anything because of I'm not a PHP Coder sad smiley

Can you explain me, how to change the codes and where (which file) to change the codes to work your script like that.

Thank you so much.

Kür&#351;ad Ölmez from Türkiye

messageRe: descr instead of id in <option value="
Posted by: panos (IP Logged)
Date: February 21, 2010 12:23PM

Hi,
you dont realy have to modify code of the forms what you have to modify is the code that shows the results.

select up.user_pref_id, f1.food_description as main_food, f2.food_description as sub_food
from user_prefs as up
left join foods as f1 on up.main_food_id=f1.food_id
left join foods as f2 on up.sub_food_id=f2.food_id
where user=1231

what that query does is to "merge" tables into a result with texts

Little sql reading will help



Sorry, only registered users may post in this forum.

All Rights Reserved 2006 Salix.gr  |  Design by Ades Design |  Hosting by e-emporio |  forum.Salix.gr powered by Phorum