简体   繁体   中英

PHP- assign value variable to each answer

THIS IS THE ORIGINAL, EDITED IN MY NEXT ANSWER BELOW: I have sent the original code in next answer with very few changes. Ask me for changes or clarifications if i missed something.

What I want to do: A scoring system that connects to a database, gives some choices for the user, eg age, education.

What I have done so far: connect to database, and echo values accordingly to each choice. However, it is not convenient to make too many "if && if && if, then" statements,

What I want to improve: It is much better to build a "foreach" statement, so I have a variable that gives points accordingly to each answer. If age>20, 5 points, if age<20, 10 points. Then: if education = highschool, 5 points. If education = university, 10 points.

Which would be the best way to build such a "foreach" statement?

<?php

// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

// Select all records from the user profile table.
// Order it by the ordering field.
$query->select($db->quoteName(array(FieldValue)));
$query->from($db->quoteName('table'));
$query->where($db->quoteName('SubmissionId') . ' = '. $db->quote('2'));
$query->and($db->quoteName('FieldName') . ' = '. $db->quote('age'));

// (Extra, but for later: I currently have submission id = 2. It should become submission id = the same as the last user submited).

// Reset the query using our newly populated query object.
$db->setQuery($query);

rows 5,6 are age, education, etc, more will be added but i need to find a way to improve this after i fix the "foreach" statement.

$row = $db->loadObjectList();
echo nl2br("\n");
echo $row['5']->FieldValue;
echo nl2br("\n");
echo $row['6']->FieldValue;

//this is my statement so far, which i need to improve. instead of echoing the value, i better assign variables to it.

echo nl2br("\n");

if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="university" )
{
    echo "15 points";
}

//should be 5+10 from the variables, not just echo value.

else if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="high school" )
{
    echo "10 points";
}

//should be 5+5 from the variables, not just echo value.

else
{
    echo "not variables given";
}

echo nl2br("\n");

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>
<?php

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);

    $query->select($db->quoteName(array(FieldValue)));
    $query->from($db->quoteName('table'));
    $query->where($db->quoteName('SubmissionId') . ' = '. $db->quote('2'));
    $query->and($db->quoteName('FieldName') . ' = '. $db->quote('age'));

    $db->setQuery($query);

    $row = $db->loadObjectList();
    echo nl2br("\n");
    echo $row['5']->FieldValue;
    echo nl2br("\n");
    echo $row['6']->FieldValue;
    $total = 0;

    if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="university" )
    {
        $total = $total + 15;
    }else if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="high school" )
    {
        $total = $total + 10;
    }


    echo $total; //You will get 25 total points here.
    $results = $db->loadObjectList();
    ?>

If I understood your question right this script will work for you. Let me know if you are looking for something else?

Update

Build an array for education.(Where you have fix values ie school = 5)

$education = array("University"=>10,"School"=>5);

Now make a foreach loop from your result.

$total = 0;

    foreach($row as $key=>$value)
    {
    if($key->age >= 20)
    {
    $total = $total + 15;
    }else{
    $total = $total + 10;
    }
    $total = $total + (int)$education[$key->education]; //it will add 10 points if education will be University and so on....
    }
echo $total;

So basically you can simply create an array for fix values and get the value from that array using foreach loop and for variable conditions ie age you can use if else. Hope this will help you. Please ignore syntax error if any as I have not tested this code.

EDIT COMMENT: I am sending the original code with my mistaken method. Values 27,28,29,30 are the age, education, years of work, "yes or no" for "has a science degree". If i missed something, please comment for me to change it or clarify better. As said, each value should give specific points. It is not important how many for now, if i see the method i will modify it accordingly.

<?php

// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

// Select all records from the user profile table.
// Order it by the ordering field.
$query->select($db->quoteName(array(FieldValue)));
$query->from($db->quoteName('table'));
$query->where($db->quoteName('SubmissionId') . ' = '. $db->quote('2'));
$query->and($db->quoteName('FieldName') . ' = '. $db->quote('age'));


// Reset the query using our newly populated query object.
$db->setQuery($query);


//27: age. 28: education 29: years of experience 30: has science degree.
$row = $db->loadObjectList();
echo nl2br("\n");
echo $row['27']->FieldValue;
echo nl2br("\n");
echo $row['28']->FieldValue;
echo nl2br("\n");
echo $row['29']->FieldValue;
echo nl2br("\n");
echo $row['30']->FieldValue;
echo nl2br("\n");

echo nl2br("\n");

echo nl2br("\n");



echo nl2br("\n");

//this method is wrong, because it does not calculate score, only echoes value
//each fieldvalue should be equivalent to a number of points. if you can show me for one,
//i can do it for the rest

if($row['27']->FieldValue=="over 20 years" && $row['28']->FieldValue=="High school" &&
    $row['29']->FieldValue=="up to 10 years" && $row['30']->FieldValue=="Yes")
{
    echo "50 points";
}
elseif($row['27']->FieldValue=="over 20 years" && $row['28']->FieldValue=="High school" &&
    $row['29']->FieldValue=="up to 10 years" && $row['30']->FieldValue=="Yes")
{
    echo "50 points";
}
elseif($row['27']->FieldValue=="over 20 years" && $row['28']->FieldValue=="No degree" &&
    $row['29']->FieldValue=="up to 10 years" && $row['30']->FieldValue=="Yes")
{
    echo "45 points";
}
else
{
    echo "value not set yet";
}

echo nl2br("\n");

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>

Try this:

$total = 0;
switch ($row['5']->FieldValue) {
    case '>20':
         $total += 5;
         break;
    // ... other cases ...
}
switch ($row['6']->FieldValue) {
    case 'university':
        $total += 10;
        break;
    case 'high school':
        $total += 5;
        break;
    // ... other cases ...
}
echo $total;

For your edited code, you would replace $row['5'] and $row['6'] with $row['27'] and $row['28'] and add two new switch statements for $row['29'] and $row['30'] with points scores as required (you didn't specify them in the edit)

You can do a switch statement to help you with this if I understand it correctly the script below it will help:

switch($row[5]) {
    case '>20':
        if ($row[6]->FieldValue == 'university') {
            $total = 15;
        }
        break;
    case '>20':
        if ($row[6]->FieldValue == 'high school') {
            $total = 10;
        }
        break;
    case default:
        $total = 0;
        break;
}

echo $total;

And if you want to make it more compact because you just checking for equals you can use ternary operators inside switch like this:

switch($row[5]) {
    case '>20':
        $total = ($row[6]->FieldValue == 'university') ? 15 : 0;
        break;
    case '>20':
        $total = ($row[6]->FieldValue == 'high school') ? 10 : 0;
        break;
    case default:
        $total = 0;
        break;
}

echo $total;

You can check this question for more info about ternary operators and here for how to use switch in php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM