Multiple Select Listbox With Indents

I’m fresh to Yii, but learning fast. I am trying to convert my old site over to Yii so it can be expanded. I have a form that uses a list box to allow users to select multiple items.

This list box has:


Lab

  LabEnv

  LabEnv

    Element

Lab

  Labenv



etc… Indented that way.

Here is my dB Query




SELECT element.id, element.name, element_type.id, element_type.name, labenv.id, labenv.lab 

FROM labenv INNER JOIN (element_type INNER JOIN element ON element_type.id = element.element_type)ON labenv.id = element.labenv 

WHERE element.active = 1 

ORDER BY labenv.id, element_type.id

Here is my PHP Code




function eBox()

{

mysql_select_db( $database );

$getElements = mysql_query( $elementSQL ) or die( mysql_error() );

$row_getElements = mysql_fetch_assoc( $getElements );

$totalRows_getElements = mysql_num_rows( $getElements );


$lab=0;

$type=0;

$element=0;

$lastLab = "";

$lastType = "";


	while( $row_getElements = mysql_fetch_array( $getElements ) )

	{

		$currentLab = $row_getElements[5];

		$currentType = $row_getElements[3];

		

		if( "$lastLab" != "$currentLab" )

		{

			printf( "<option value=\"%s\">%s</option>\n\t", $row_getElements[5], $row_getElements[5] );

			$lastLab = $row_getElements[5];

			$lab++;

		}

		if ( "$currentType" != "$lastType" )

		{

			printf( "<option value=\"%s,%s\">&nbsp;&nbsp;&nbsp;-%s</option>\n\t", $row_getElements[5], $row_getElements[3], $row_getElements[3] );

			$lastLab = $row_getElements[5];

			$lastType = $row_getElements[3];

			$type++;

		}

		if ( "$currentType" == "$lastType" )

		{

			printf( "<option value=\"%s,%s,%s\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s</option>\n\t", $row_getElements[5], $row_getElements[3], $row_getElements[1], $row_getElements[1] );

			$lastLab = $row_getElements[5];

			$lastType = $row_getElements[3];

			$element++;

		}

	}

	

	mysql_close( $dbconn );

}



When I execute this, I get a nicely displayed Multiple Select ListBox with great indents. (I created this about 4-5 years ago lol)

Can I perform this type of indentation within Yii easily? Can someone show me something basic so I can get started?

Thanks in advance. The thing that I want to do DIFFERENT is instead of using the NAME of the element,labenv,lab I want to use the ID value so in the event it changes I can just make the dB change.

George

–bump-- maybe someone can help or point me in the correct direction

thanks

– another bump up – Someone else must have had this thought too… :unsure:

You can reuse your code, just change the way you interact with the database from direct functions to active records:




$elements = Elements::model()->findAll();

foreach($elements as $element) {

// old code

}



and retrieve column data using attribute (column) names instead of numeric indexes.

Of course, you need to define your model (active record) but you could use gii to do that.