transform the numbers to letters using php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I store the number 1 2 3 4 as the answer to multiple choice questions in MySQL database,
But I want to display them in a b c d on web page.
I'm using PHP, how can I transform the numbers to letters using PHP?
echo "
<h2>question</h2>
<p>".$row[0]."</p>
<h2>answer</h2>
<p>".$row[1]."</p>";
The row[1] is {1,2,3,4} now, I want it turn to {A,B,C,D}.
Can any one please have a look and tell me how to do it?

It is very easy:
<?php
$row = array(1, 2, 3, 4);
echo "
<h2>question</h2>
<p>".chr(64 + $row[0])."</p>
<h2>answer</h2>
<p>".chr(64 + $row[1])."</p>";
Test it: http://3v4l.org/uojsK

you can define an array first:
$letters = array_combine(range(1,26), range('a', 'z'));
//now just use:
echo $letters[$row[1]];

Create the alphabet array and use the numbers to get the right letter by index (num-1)
$letters = range('a','z');
$num = 3;
var_dump($letters[$num-1]);//"c"
i.e.
$letters = range('a','z');
echo "
<h2>question</h2>
<p>".$letters[$row[0]-1]."</p>
<h2>answer</h2>
<p>".$letters[$row[1]-1]."</p>";

<?php
/// the $row is the array you can get from db
$row = array(1,2,3,4,5);
// Here you can add your option up-to z
$alpha = range('A','E');
$i = 0;
/// Instead of foreach loop you can run while loop
foreach($row as $rows){
echo $alpha[($row[$i]-1)].'<br/>';
$i++;
}
?>

Related

PHP function to get N random values (as array) which's sum result in X [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 think the subject hits it better than I was expecting.
I need a function which returns random numbers which result in a given value (X) when they are summed up.
Something like this:
getRandomTo(10); // result for example: array(2,3,5)
getRandomTo(10); // result for example: array(4,3,3)
getRandomTo(12); // result for example: array(5,1,6)
I could not find a generic algorithm function for solving that requirement. Further more I cannot imagine a FAST and performant way to create something like this my self.
Please help me out
function getRandomTo($num)
{
$x = Array();
$i = 0;
do
{
$x[$i] = rand(1,$num);
$num = $num - $x[$i];
$i++;
}while($num > 0);
print_r($x);
}
Maybe do this:
create a random value between 0 and X; say this one is called r1. save this in your array.
create another random value between 0 and (X-r1), name it r2. save it also.
do these steps as often as you need it (or as long as r1+...+rn is lower than X)
Another solution:
function randomTo($numIn) {
$numOut = 0;
$numbers = array();
do {
$add = rand(1, $numIn);
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
} while( $numOut != $numIn );
return $numbers;
}
$result = randomTo(15);
var_dump($result);

Mathematical operation of CHARACTER [not numeric] in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
is there any way to sum/substrate character in php?
For example if
$var1 = 'a';
$var2 = 'b';
$var3 = 'a';
$calculation = $var1 - $var2 + $var3;
echo $calculation;
I want the output as 2a-b
Just like we did in high school algebra?
I wrote a simple function to make something like what you want.
It's just an example, you will have to improve it a lot if you really want to use it, but is a good start.
Limitations:
Only works with letters (Won't work propely if you add numbers, you will have to add that functionaliy).
ALL the letters must have their plus or minus.
You must use spaces before a plus or minus.
This is definitely not the best way to do it, as I said you have to improve it. I wrote it fast but I tested it a bit.
<?
function calc($str){
$data = preg_split("/ /", $str);
$used = Array();
$buffer = "";
foreach ($data as $pos=>$letter){
foreach ($data as $pos2=>$letter2){
if ($letter[1] == $letter2[1] && !in_array($pos, $used) && !in_array($pos2, $used) && $pos != $pos2){
$first = $letter[0] == '+' ? 1 : -1;
$second = $letter2[0] == '+' ? 1 : -1;
$buffer .= ($first+$second).$letter[1];
$used[count($used)] = $pos;
$used[count($used)] = $pos2;
}
}
}
foreach ($data as $pos=>$letter){
if (!in_array($pos, $used)){
$buffer .= $letter;
}
}
return $buffer;
}
echo calc("+a -b +a");
?>
Output:
2a-b

How to create automatic array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to PHP and would like to create a drop down box which displays every year from xxxx to the present year, I can handle the implementation into the drop down box but would like to know if I can have php automatically create an array with all of these years in to save typing them in manually.
You can use the function range (see the range docs) to accomplish this
$years = range(2013, 2050);
The optional third parameter is the step (standard is 1).
If you want to use an array:
$array_years = range($start_year, $end_year, $steps); //$steps is optional
foreach($array_years AS $years) {
//output options
}
Documentation here: http://php.net/manual/en/function.range.php
Alternative: use a for loop (see other answers for example code)
You can also use simple for loop to generate it.
echo "<select>";
for($i = from; $i <= to; $i++){
echo "<option>" . $i . "</option>;
}
echo "</select>";
<select>
<?php
$startYear = "";
$endYear = date("Y");
while($startYear != $endYear){
echo '<option>'.$startYear.'</option>';
$startYear++;
}
?>
</select>
should do it
<select name="year">
<?php
$start = 1960;
for($i=$start; $i < date("Y"); $i++) {
echo '<option>' . $i . '</option>';
}
?>
</select>

Separating numbers processed with a foreach conditional into specific category of numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a scenario: The $numbers array in the code below contains one thousand numbers(0 - 1000). I need to count how many of these numbers fit into each of the following categories:
how many of them are between 1 and 1000 inclusive,
how many of them are less than 1, and
how many of them are greater than 1000.
I have created a foreach loop to look at each number one after the other, but right now it's treating every number like it belongs in all three categories.
How do I get the counts correct? ie., how many numbers fit into the "Less than 1", "Between 1 and 1000", and "Greater than 1000" categories, respectively.
The current code:
$numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;
}
Very simply just include the conditions. You can just use if
foreach ($numbers as $number) {
if ($number < 1) $count_less_than_one += 1;
else if ($number >= 1 && $number <= 1000) $count_between_one_and_thousand += 1;
else $count_greater_than_thousand += 1;
}

Looking for improvement on PHP split and combination function [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
Suppose I have a string such as this:
ABC.DEF.GHI.JKL (This string could have any length and amount of characters between dots)
I want to add the following combinations into an array.
ABC
ABC.DEF
ABC.DEF.GHI
ABC.DEF.GHI.JKL
So basically the string should be split with the dot character and then the individual sub-strings should be combined.
I have a function already, but in my opinion this seems too complicated to achieve the result.
$myarray = array();
$split = explode('.', 'that.string.with.dots');
$string = '';
for ($i = 0; $i < count($split); $i++) {
$string .= $split[$i];
myarray[] = $string;
$string .= '.';
}
Any suggestions on improving this?
Thanks
Michael
Perhaps this:
$split = explode('.', 'that.string.with.dots');
for ($i = 1; $i < count($split); $i++) {
$split[i] = $split[$i-1] . '.' . $split[i];
}
It just concats the current with the previous.
I have the feeling that you are asking if there is a PHP function that will achieve this result with a single function call, but I don't think there is one.
You can rewrite the code as pritaeas did in his answer, to remove two lines of code, but it will still be the same concept.

Categories