Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want create sort of quiz. But questions will be different, depend on what options were selected in drop-down menu.
Main problem is where I need to store my html blocks with questions?
All questions will be like this pattern:
<tr>
<td>Question</td>
<td>Answer 1, Answer 2, ...</td>
</tr>
Total amount of questions - 50, full "question-table" have 10 questions and submit button.
Total amount of different combinations - over 100 "question-tables".
Question in most cases will be repeated.
At first, I try to store my questions in variables:
$question01 = '<tr>
<td>Question 01</td>
<td><input type="radio" name="v2" checked="" >Answer 1 <input type="radio" name="v2">Answer 2</td>
</tr>';
And then I compose them in table, like:
$fullTable = $question01 . $question02 . $question03;
But I don't feel right about this. Maybe you can at least point at which framework/pattern I should look?
Examples are highly appreciated.
You can store your html blocks in variables right inside your script, but that's not necessarily the most maintainable route. I like to use a templating engine, where your HTML is stored in a separate file with special placeholders that the PHP script will fill in. Personally, I'm a fan of MiniTemplator. It supports defining blocks of HTML you can repeat over and over with different substitutions each time.
For example, using MiniTemplator syntax, your HTML would look like this:
<!-- $beginBlock Question -->
<tr>
<td>{$question_text}</td>
<td>{$answer_options}</td>
</tr>
<!-- $endBlock Question -->
Then, in your code, you'd call routines on the MiniTemplator class like so (probably in a loop over all question/answer sets). Where $t is an instance of the MiniTemplator class and has your template loaded:
$t->setVariable("question_text", "What is your name?");
$t->setVariable("answer_options", "A: Pudnintame, B: Murgatroyd, C: Ethyl");
$t->addBlock("Question");
Each time those three lines are executed (with different questions and answer options filled in), the templating engine will add another block of HTML.
i can translate it to PHP for you if you like but you should have something like:
question = {id:"123", question:"", answers:[{text:"",id:"2"}], correct:"2"}
then you can iterate over all questions and populate the possible answers from an array... When they submit an answer, you can look up the correct answer on the id
$ans = $_POST['ans'];
$id = $_POST['id'];
if ($ans == $question[$id]['correct'] )
{
echo "correct!";
}
storing on the client though, you most likely wouldnt want it to be displayed none. I would agree that if you have a script that makes a question answer pairs, then you would just call the php script as needed. something like:
for (var i = 10;i;i--){
//calls 10 times
$.ajax("mygenscript.php", function(){
//replace previous question block with this new question.
});
}
then you arent storing things on the client side.
could then have something like:
and in the success call say:
var newQ = "result from ajax call";
$("div#question").empty().append(newQ);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have to evaluate the dynamic expressions and based on the conditions, I need to show the other div.
My expression is:
{ [appln.module.name.VALUE] == 1 && [appln.module.name.VALUE] != EFT }
In case both of these expressions satisfy conditions, then the div must be shown. The value will be calculated based on the changes made.
Could someone help on this, of how to parse and evaluate the conditions dynamically?
It sounds like the question boils down to "How do I show a DIV tag using PHP based on a conditional" To do this in PHP you could do something like the following...
<div id="YourDIV" <?php if ($ApplnModuleName != 1 || $EFT == 1){?>style="display:none"<?php } ?>>Your Content Goes Here</div>
The example above includes a negated implementation of your conditional. Both tests are required since $EFT could equal 1 which would need to hide the DIV.
An even better/cleaner way that would keep the contents of the DIV from being sent to the client would look like this...
<?php if ($ApplnModuleName == 1 && $EFT != 1) { ?>
<div id="YourDIV">Your Content Goes Here</div>
<?php } ?>
The former example would allow you to turn the DIV on using Javascript in the client. The later example would save you bandwidth and prevent potentially sensitive data from showing up in the source of the HTML in the client.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a option dropdown where I need to echo selected if a value is equal to the search variable.
Right now I am using PHP if statements to decide this, but I would like clean it up a little by using a method that doesn't take up so many lines in my code.
Here is an example of my php if statement:
<option value="antiques" <?php if($_POST['category'] == 'antiques'){echo 'selected';}?>>Antiques</option>
While this works, it just takes up quite a bit of space in my code as I have close to 100 of these selection options.
Is there a better way to check if a post variable is equal to the value of this option and then echo selected?
A simple way to do this is using the PHP ternary operator (more info available here: http://php.net/manual/en/language.operators.comparison.php [scroll down below the big warning about floats])
<option value="antiques"<?php echo ($_POST['category'] == 'antiques')?' selected':'';}?>>Antiques</option>
It doesn't save much room in this instance, but it is a cleaner way to write it inline with HTML in my opinion.
Edit with code from #crazymoin
The idea of moving it into a function is great to make it easy too.
I would modify the function like this:
function abc($post,$value,$label){
echo '<option value="'.$value.'"'.($_POST[$post] == $value)?' selected':''.'>'.$label.'</option>';
}
Then you can call it with this:
<?php abc('category','antiques','Antiques'); ?>
Note that if for some reason the specified index in the $_POST array doesn't exist, PHP will throw some warnings about an undefined index. You may want to expand the function to do some checking with isset(), etc. as well.
create a function and use it as many times you want:
function abc($postData, $thisData) { if($postData == $thisData) { echo ' selected';} }
Now do this:
<option value="antiques" <?php abc($_POST['category'], 'antiques'); ?> >Antiques</option>
hope it help!
Change using Justin Turner response:
Function is:
function abc($postData, $thisData) {
echo '<option value="'.$thisData.'">'.($_POST['category'] == 'antiques')?' selected':''.$thisData.'</option>';
}
Now just use the function as many times you want:
<?php abc($_POST['category'], 'antiques'); ?>
I like doing all my logic processing in one area of the code.
A clean way to do this would be to create an array with all the categories. Then fill in the one matching the category with selected. In each menu option, print out the contents of the array key matching the name of the option.
<?php
$selected=array(
'antiques'=>'',
'horses'=>'',
'umbrellas'=>''
);
$selected[$_POST['category']]='selected';
?>
<option value="antiques" <?php echo $selected['antiques'];?>>Antiques</option>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values ββto a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am unsure what I am to search for this question so not much research has been done.
I have a MySQL table that I store scores for a pool team as 1 for a win and 0 for a loss
i am displaying the values on a table but would like to change the output into words maybe different colours depending on the value.
the value 1 i like to read Win in green and value 2 to read lose in red.
how can i do this? or can u link me to a basic tutorial
Perhaps you need a conditional operator:
http://davidwalsh.name/php-shorthand-if-else-ternary-operators
$output="Result:".( $score ? "<font color=green>$score</font>":"<font color=red>$score</font>");
You should really make an attempt before asking a question on here. Giving you the benefit of the doubt, I'd say look into enumerations (since you seem to not want to use a simple conditional for some reason)
You simply can put a condition in the table, to check the score value. Supposing you have an array of scores:
<table>
<? foreach($score in $scores) ?>
<tr>
<td>some data</td>
<td>some data</td>
...
<td>
<?
if($score == 1) { echo "Win" } else { echo "Lose" }
?>
</td>
</tr>
</table>
There been a lot of great answers on this question of mine but if anyone looking for a quick fix in this case the following works for me
<?php
if ($fs1 > 0) {
echo "<p style='color:#0F3'>Win<p>";
} else {
echo "<p style='color:#F00'>Lose<p>";
}
?>
with $fs1 being the row that i am getting the value from
Without the links within the answeres i would never of found out how to do it
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem β and include valid code to reproduce it β in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have javascript function and it returns a sum variable. But i couldnt reach it from php code.
my function is :
function add()
{
var sum = 0; // sum initially equals to zero.
var newNumber = 0; // Since textfields are initially text format, I convert them into integer and equalled to newNumber variable.
if(document.RodeoForm.checkbox.checked == true) // If checkbox changed to true,
{
for(var i=0;i<<?php echo $_SESSION['us']; ?>;i++) // Since we have 3 numbers, loop will work 3 times.
{
newNumber = parseInt(document.getElementById("fiyat"+i).value); // Take the number from field and convert it into an integer.
sum += newNumber; // Add the numbers into each other.
}
}
document.RodeoForm.tf.value = sum; // Print the sum onto the screen.
}
JavaScript runs on client side, whereas PHP runs on server side. They can't communicate directly with each other.
Once the page is rendered (and sent to the user), all PHP code was executed and is no longer visible in the source. The JavaScript however is sent along with the HTML and will be executed in the browser of the client. If you want PHP to be aware of a value generated by JavaScript, you have to manually send the data using AJAX.
You can only reach PHP from JavaScript using AJAX. Google how to do AJAX calls from Javascript.
Make sure you have session_start(); to the beginning of that php file, or in the php file which includes that code.
You can add PHP into Javascript but not the other way around because PHP is server side, so it can echo information into client slide.
Depending on what you're using this for , you can use AJAX to send the information to a PHP page.