Jump to content

Help with writing basic code


stoneheart

Recommended Posts

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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.