PHPBuilder - Arrays, HTML and PHP Page 3



RSS Twitter
Articles Html

Arrays, HTML and PHP - Page 3

by: Tim Perdue
|
July 30, 2000

This next one is a bit more complex. It builds the multiple select boxes, and since multiple rows can be selected, you have to pass in a name with [] at the end, and pass in an array of checked items.

<?php

function build_multiple_select_box ($result,$name,$checked_array,$size='8') {
    
/*
        Takes a result set, with the first column being the "id" or value
        and the second column being the text you want displayed

        The second parameter is the name you want assigned to this form element

        The third parameter is an array of checked values;

        The fourth parameter is optional. Pass the size of this box
    */

    
$rows=db_numrows($result);

    for (
$i=0$i<$rows$i++) {
        echo 
'
            <OPTION VALUE="'
.db_result($result,$i,0).'"';
        
/*
            Determine if it's checked
        */
        
$val=db_result($result,$i,0);
        for (
$j=0$j<$checked_count$j++) {
            if (
$val == $checked_array[$j]) {
                echo 
' SELECTED';
            }
        }
        echo 
'>'.db_result($result,$i,1).'</OPTION>';
    }
    echo 
'
        </SELECT>'
;
}

?>
This last function is utilitarian. I built it to quickly take a set of selected items and turn it into an array that I could pass to build_multiple_select_box().

<?php

function result_column_to_array($result$col=0) {
    
/*
        Takes a result set and turns the optional column into
        an array
    */
    
$rows=db_numrows($result);
    for (
$i=0$i<$rows$i++) {
        
$array[]=db_result($result,$i,$col);
    }
    return 
$array;
}

?>

« Previous Page
1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Tim Perdue

Comment:



Comment:

(Maximum characters: 1200). You have characters left.