stoneheart Posted January 2, 2012 Share Posted January 2, 2012 I'm using Zebra_Form and I want to populate a select box with some values from a query. How do I do this? I have a returnset from MS SQL Server, but I am a newbie and I am baffled about how to frame it into the required array for Zebra_Form. The below example from the documentation has a hard coded set of values. How can I replace this with my queryset ($stmt printed a little further down) instead? // single-option select box $obj = &$form->add('select', 'my_select2'); $obj->add_options(array( 'v1' => 'Value 1', 'v2' => 'Value 2', 'v3' => 'Value 3' )); //sql code here * $serverName = "serverName\sqlexpress"; $connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" ); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } $sql = "select ticketID, ticketName from tickets order by price desc"; $stmt = sqlsrv_query( $conn, $sql); Quote Link to comment https://forums.phpfreaks.com/topic/254235-help-with-writing-basic-code/ Share on other sites More sharing options...
Psycho Posted January 2, 2012 Share Posted January 2, 2012 Looking at the sample data you need an array where the indexes are the option values and the values will be the displayed option text. So, all you need to do is iterate through the result set from the query and use the data to populate an array - which you would then feed to the form class. Here is an example: //sql code here * $serverName = "serverName\sqlexpress"; $connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" ); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } //Create and run query to get id & name $sql = "SELECT ticketID, ticketName FROM tickets ORDER BY price DESC"; $stmt = sqlsrv_query( $conn, $sql); //Use results to populate array $optionsArray = array(); while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) { $optionsArray[$row['ticketID']] = $row['ticketName']; } // single-option select box $obj = &$form->add('select', 'my_select2'); //Pass options array for the select creation $obj->add_options($optionsArray); Quote Link to comment https://forums.phpfreaks.com/topic/254235-help-with-writing-basic-code/#findComment-1303497 Share on other sites More sharing options...
stoneheart Posted January 3, 2012 Author Share Posted January 3, 2012 That worked perfectly, mjdamato! I appreciate the help. Some good karma is coming your way surely. Quote Link to comment https://forums.phpfreaks.com/topic/254235-help-with-writing-basic-code/#findComment-1303503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.