Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP Choice help.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP Choice help.

Postby BasioCoderz » Wed Mar 13, 2013 1:12 am

Hello Academy.

We got this task in school where we have to make a questionnaire.
My task is to 'create, edit and delete' questions in this questionnaire.
This is not a hard task, i can do them all. My only problem is:
It obviously needs a title (thé question), but i would also like to make 3 opportunity's for the costumer.
For the answer methods i would like the costumer to be able to choose between 'textarea, radio buttons or select boxes'.
So the when the costumer creates a question she can for example do:
-----
Have you ever taken drugs?

<?php
//Be able to choose between 3 things.
?>
----

So it might look like this:

Have you ever taken drugs?
'radio buttons' Yes, No.

or she can choose the select method which would be:

Which of the following have u taken?
'select boxes'
(loads of drug examples).

- BasioCoderz
BasioCoderz
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Mar 13, 2013 1:11 am

Re: PHP Choice help.

Postby seandisanti » Wed Mar 13, 2013 8:18 am

personally, I use functions that generate the markup for the questions. here are 3 functions from a recent project I worked on that had a questionnaire. The 3 types of questions that I used were 1) a tabbed question, for complex questions that can be broken into parts, or where the question may have to be answered by multiple sources. each tab has its own text area, and the question is displayed on the first tab. To keep with the same look once I got to more straightforward freeform answer questions, i made the createQuestion function that presents the question in a a tab above a text area. The checkbox list works kind of the same, it creates a list of checkboxes in tabs above a text area where 'other' values can be entered. All three functions ask for similar parameters; the question obviously, then a section and number, I use roman numerals for the section, and the question number for the number, that is so that logical names and references are created in the markup so that named selectors still work as intended regardless of the number of questions. On the createCheckboxList function, there is an optional 'break' parameter that lets you decide how many checkboxes you want per row. You can fine tune the default depending on the width of your questionnaire, but this provides a mechanism for avoiding ugly line breaks in your checkbox tabs. The tabbed questions require some jquery, but i'll put that in a second code box below the php.

Code: Select all

function createTabbedQuestion($question ''$tabs = array(), $section ''$number 0$height 0) {
    
$output '<div style="margin-top:10px;position:relative;margin-bottom:' . (($height 0) ? $height .45 45 45) . 'px;margin-left:10px;">';
    
$output .='<ul style="margin-left:-20px;">';
    
$output .='<li class="response-tabs" name="question">Question</li>';
    if (
count($tabs) > 0) {
        foreach (
$tabs as $key => $value) {
            
$output .='<li class="response-tabs" name="' $section $number chr($key 97) . '">' $value '</li>';
        }
    } else {
        return 
false;
    }
    
$output .= '</ul><br style="clear:both">';
    
$output .= '<textarea class="question" disabled="disabled" style="z-index:2;width:90%;position:absolute;' . (($height 0) ? 'height:' $height 'px;' '') . '">' $number '. ' $question '</textarea>';
    if (
count($tabs) > 0) {
        foreach (
$tabs as $key => $value) {
            
$output .= '<textarea class="' $section $number chr($key 97) . '" style="width:90%;position:absolute;' . (($height 0) ? 'height:' $height 'px;' '') . '">' $value '</textarea>';
        }
    }
    
$output .= '</div><br style="clear:both;" /></li>';
    return 
$output;
}

function 
createQuestion($question ''$section ''$number 0$height 0) {
    
$output '<div style="margin-top:10px;position:relative;margin-bottom:' . (($height 0) ? $height .45 45 45) . 'px;margin-left:10px;">';
    
$output .='<ul style="margin-left:-20px;">';
    
$output .='<li class="response-tabs" name="question">' $number '. ' $question '</li>';
    
$output .= '</ul><br style="clear:both">';
    
$output .= '<textarea class="' $section $number 'a" style="width:90%;position:absolute;' . (($height 0) ? 'height:' $height 'px;' '') . '"></textarea>';
    
$output .= '</div><br style="clear:both;" /></li>';
    return 
$output;
}

function 
createCheckboxList($category ''$possibilities = array(), $section ''$number 0$break 6) {
    
$lineSpan '<span style="line-height:38px;margin-left:20px;background-color:rgb(221,238,255);border-radius:5px 5px 0px 0px;padding:10px;border:1px solid rgb(170,187,255);">';
    
$output '<div style="margin-top:30px;position:relative;margin-bottom:45px;margin-left:10px;">';
    
$output .= '';
    
$output .= $lineSpan $number '. ' $category ': </span><br />' $lineSpan;
    if (
count($possibilities) == 0) {
        return 
false;
    }

    foreach (
$possibilities as $key => $value) {
        
$output .= '<input type="checkbox" name="' $category $number chr($key 97) . '" value="' $value '" style="margin-left:7px;margin-right:2px">' $value . (((($key 1) % $break) == 0) ? '</span><br />' $lineSpan '');
    }
    
$output .= '</span><br /><textarea style="height:20px; width:90%;position:absolute;height:22px">Other</textarea><br /></div>';
    return 
$output;
}
 

Code: Select all

<script>
    $(document).ready(function(){
        $(".response-tabs").click(function(){
            $(".shown").removeClass("shown");
            $("."+$(this).attr("name")).addClass("shown");
        })
    })
</script>
<style>
    form label {
        text-align: left;
        display: inline-block;
    }
    .shown{
        z-index:3;
    }
    .response-tabs{
        display:block;
        float:left;
        background-color:#def;
        padding:5px;
        margin-right:2px;
        margin-bottom:0;
        border-radius:5px 5px 0px 0px;
        moz-border-radiius:5px 5px 0px 0px;
        border:1px solid #abf;
    }
    .response-tabs:hover {
        background-color:#67f;
    }
</style>
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.