Converting php number value to words with colour [closed] - php

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

Related

Merge <tr> <td> in php foreach [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I have a dynamic table where a foreach loop generate every tr and the first td values
My problem is that every tr has 2nd 3rd 4th and 5th td column. These are also generated by a new foreach.
What is the smartest way to synchronise these loops to show data correctly?
I think this is very simple:
<?php
$rows = '5'; // Number of rows that you want
echo "<table>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
echo "<td>{$tr}*1</td>";
for($td=2;$td<=5;$td++){
echo "<td>{$tr}*{$td}</td>";
}
echo "</tr>";
}
echo "</table>";
?>

working with nested if query [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Hi I have a field which is supposed to display 1 of 3 icons based on a 2 conditions.
The key field here is VisitPlanRequired, if the answer to this is a "No" then the the icon should be na.png, however if it a "Yes", it then depends on another variable.
So if VisitPlanRequired = yes, then the two options are either ok.jpg or notok.jpg, the actual decider is whether the field VisitPlanIssued is null or contains a date, so if it contains a date, it should go to "ok", if it doesnt, it should go to notok.
This is the code I have so far, but I am struggling to get it to work for all three conditions, I would appreciate your help:
if ($data["VisitPlanRequired"]==='No')
$value="<img src=images/na.png id='image'>";
elseif
($data["AuditPlanIssued"])
{ $value="<img src=images/ok.jpg id='image'>";}
else
$value="<img src=images/notok.jpg id='image'>";
Something like this (really the basics of programming actually...
if ($data["VisitPlanRequired"] == 'No'){
$value="<img src=images/na.png id='image'>";
}else{
if( !empty($data["AuditPlanIssued"]) ){
$value="<img src=images/ok.jpg id='image'>";
}else{
$value="<img src=images/notok.jpg id='image'>";
}
}
Success

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>

Auto increment the alphanumeric characters Id [closed]

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 want to auto increment the alphanumeric character's ID and i want to savi it in to my database.
For example:
Example-001
Example-002
Example-003
Example-004
Example-005
You don't really want to store it like that -- bad idea.
Instead, just have your ID INT AUTO_INCREMENT in your MySQL and do something like
<?php echo "$dbRow['name']."-".$dbRow['id'];
Or, if you're OCD -
while(strlen($dbRow['id'] < 3) {
$dbRow['id'] = "0".$dbRow['id'];
}
If you are looking to increment the number at the end of those strings, and you have to do it in PHP, try this:
$str1 = "Example-001";
$parts = explode("-",$str1);
echo sprintf($parts[0] . '-%03d', $parts[1]+1); // Example-002
However I would guess there's a better way, possibly at the database level to accomplish what you need. You would need to explain more and post more code.
Example: http://3v4l.org/V2R5m
I'm not entirely sure what you mean, but since you tagged this as PHP and you're asking how "to auto increment an alphanumeric character's ID," here's a solution:
for ($i=0; $i<=10; $i++){
$number = str_pad($i, 3, "0", STR_PAD_LEFT);
echo "Example-$number <br />";
}
Outputs:
Example-000
Example-001
Example-002
Example-003
Example-004
Etc...
Not only is does it increment the number, but it does it automatically-ish.

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