eval and logical expression check [closed] - php

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.

Related

line break conversion from html to php [closed]

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 have html code loaded in array $gh[0] and i am coonverting it to plain text
if i compare with same text loaded into new variable it doesn't works
can someone help with the issue, what should be exact value in $a to match the content in $gh[0]
i want comparsion to be working , what will be output if $gh[0] is converted into plain text especially line break
<?php
$gh[0]="Net inventory (used in)/from<br>Production Activities"
$fg=$gh[0]->plaintext;
$a="Net inventory (used in)/from Production Activities"
if($a == $fg)
{
echo "match";
}
else
{
echo "No match";
}
?>
What about just stripping all elements from the input?
$gh[0]= strip_tags($gh[0]);
If you want to know what the value (of $fg) is, why don't you check it?
There's echo, print_r and var_dump.

What is a better way to write an if statement using PHP [closed]

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>

php - inline font style for print statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I know its possible to apply inline styles to echo statements but can this be done with print statements as well?
In my pagination I have links to previous and next records using the greater and less than symbols <> and a running count of the current record against the total number of records. e.g.
<
1/2
or
>
2/2
I have styled them in css but want to decrease the size of the count only. If I make changes to the css the font size for the previous and next links and count all change, I only want to target the count.
<div class="nextcard"><?php if($nextlink != ""){ print ($nextlink."<br/>".$next."/".$count); } ?></div>
I have tried :
<div class="nextcard"><?php if($nextlink != ""){ print 'style=font:50px' ($nextlink."<br/>".$next."/".$count); } ?></div>
But get syntax errors.
One approach would be to add a class rather than inline styles. However, you have forgotten to append the string correctly.
Here is what I would personally do: (Note I replaced print with echo, TBH, there would be no different)
<div class="nextcard">
<?php
if($nextlink != "")
echo '<span class="mark">'.($nextlink."<br/>".$next."/".$count).'</span>';
?>
</div>
If you still want to style it inline, you should simply do:
<div class="nextcard">
<?php
if($nextlink != "")
echo '<span style="font-size:50px;">'.($nextlink."<br/>".$next."/".$count).'</span>';
?>
</div>

How to acces the second value [closed]

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)

How to build page with pattern-like elements [closed]

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

Categories