Here is the code:
<?php
$_SESSION['currentlocation'] = "combat.php";
if($_SESSION['ambush'] && $_SESSION['flee']) {
for($i = 0; $i < sizeOf($_SESSION['enemies']); $i++) {
$scaling = 20 - $_SESSION['enemies'][$i] -> luck;
if($_SESSION['enemies'][$i]-> rank >= 20 && $_SESSION['enemies'][$i]-> rank <
40) {
$prob = rand(1, 100);
if($prob <= 100) {
// if($prob <= 75 - $scaling * 3.8) {
if($_SESSION['comboAttack'] < 2) {
$_SESSION['comboAttack'] = 2;
}
$_SESSION['enemies'][$i]->comboAttack = 2;
$_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i)."pulls off
a 2-hit combo. \n";
}
}
else if($_SESSION['enemies'][$i]-> rank >= 40 && $_SESSION['enemies'][i]-> rank
< 60) {
$prob = rand(1, 100);
if($prob <= 50 - $scaling * 3.8) {
if($_SESSION['comboAttack'] < 3) {
$_SESSION['comboAttack'] = 3;
}
$_SESSION['enemies'][$i]->comboAttack = 3;
$_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i)."pulls off
a 3-hit combo. \n";
}
else if($prob <= 75 - $scaling * 3.8) {
if($_SESSION['comboAttack'] < 2) {
$_SESSION['comboAttack'] = 2;
}
$_SESSION['enemies'][$i]->comboAttack = 2;
}
}
if($_SESSION['enemies'][$i] != null) {
// $_SESSION['enemies'][$i]->attack($i);
// echo $_SESSION['enemies'][$i]->attackMessage;
}
?>
<script type="text/javascript">
$(document).ready(function() {
$('#combatinfo').append('<?php if($_SESSION['enemies'][$i] != null) echo
$_SESSION['enemies'][$i]->attackMessage; ?>');
});
</script>
<?php
}
?>
<button type="button">Defend</button> <button type="button">Flee</button>
<form action="index.php" method="post">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
As the title states I'm trying to append the text in the text-field of an object(skinhead) to a textarea. The above code does not work. However when I replace some code with this:
<script type="text/javascript">
$(document).ready(function() {
$('#combatinfo').append('<?php if($_SESSION['enemies'][$i] != null) echo
"HELLO WORLD!" ?>');
});
</script>
This works.
The text in the object is not null, so how come it is not displaying?
Thank you for your time.
Replace
$_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i)."pulls off
a 2-hit combo. \n";
with
$_SESSION['enemies'][$i]->attackMessage="Enemy #".strval($i)."pulls off a 2-hit combo.
";
and so on...
Related
I am new to PHP. I am trying to create a simple form where user will submit there name and mark they got in the exam.
<form action="" method="post">
<input type="text" name="name" id="" placeholder="Your name">
<input type="number" name="mark" id="" placeholder="Your Mark">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST["submit"])){
$name = $_POST['name'] ;
$mark = $_POST['mark'];
if( $mark > 80 ){
$result = "A+";
}elseif($mark > 70 && $mark < 80){
$result = "A";
}
elseif($mark > 60 && $mark < 70){
$result = "A-";
}
elseif($mark > 40 && $mark < 60){
$result = "B";
}else{
$result = "Fail";
}
echo $result;
}
?>
But if I echo the result variable it is always showing 'Fail'. What is I am doing wrong? thanks
As mentioned in the comments, when comparing ranges, you need to consider the corner cases:
$mark >= 70 && $mark < 80
Also there are few ways to simplify your code: If you check for marsk >= 80 in the first IF, there is no need to check for that in the next elseif:
if( $mark >= 80 )
{
$result = "A+";
}
elseif($mark >= 70)
{
$result = "A";
}
elseif($mark >= 60)
{
$result = "A-";
}
elseif($mark >= 40)
{
$result = "B";
}
else
{
$result = "Fail";
}
echo $result;
So i have the code to check for Prime numbers with value that is inputted. My question is, how can i turn my code so that the table can be dynamic in sets of 10 and the max is 100? So that the input can only take increments of 10 and the max value you can enter 100?
My current code right now is set to 10 columns and 4 rows so is there a way to make this change (dynamic i guess?) to change with the input increments of 10?
Here is my code:
<form method="post" action="">
<table border="2" width="1180px">
<thead>
<center>
Number Chart</center>
</thead>
<?php
error_reporting(0);
function isPrime($n)
{
if ($n == 1) return false;
if ($n == 2) return true;
if ($n % 2 == 0)
{
return false;
}
$i = 2;
for ($i = 2; $i < $n; $i++)
{
if ($n % $i == 0)
{
return false;
}
}
return true;
}
if (isset($_POST['value']) && $_POST['value'] != 0)
{
/* #var $start type */
$start = $_POST['value'];
}
else
{
$start = 1;
}
$n_cols = 10;
$n_rows = 5;
for ($i = 0; $i < $n_rows; $i++) //
{
$col = '';
for ($j = 0; $j < $n_cols; $j++)
{
$number = ($start - $i - ($j * $n_cols));
if (isPrime($number) == true)
{
//if prime color it red
$col.= '<th style="color:red">' . ($start - $j - ($i * $n_cols)) .
'</th>';
}
else
{
$col.= '<th>' . ($start - $j - ($i * $n_cols)) . '</th>';
}
}
$out.= '<tr>' . $col . $row . '</tr>';
}
echo $out;
?>
<tr>
<thead colspan=10>
<center>
<label for="input">Enter Limit:</label>
<input type="text" name="value" style="width: 60px">
<input type="submit" name="submit" value="Submit">
</center>
</thead>
</tr>
</table>
</form>
if (($_POST['value'] % 10) != 0 || $_POST['value'] >100 ){
echo 'you must enter a valid value';
}else{
//all the code you want to run only if the number posted is valid
}
the % is Modulo, see http://php.net/manual/en/language.operators.arithmetic.php for more details
I have the following code that suppose to change the background color after I enter the RGB color codes and click submit.
I don't know for what reason I get the "Undefined variable" and have a black background color before I click the submit button
< ?php
error_reporting(E_ALL);
ini_set('display_errors',true);
$form = "< form method='post' action=$_SERVER[PHP_SELF] >\n
R: < input type='text' name='r' >
G: < input type='text' name='g' >
B: < input type='text' name='b' >
< input type='submit' name='buton' value='go' >\n";
< /form >
$hexa = array();
$culoareHexa = array();
function &decimal2hexa($valoare) {
$valoriHexa = array('0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9', '10'=>'A', '11'=>'B', '12'=>'C', '13'=>'D', '14'=>'E', '15'=>'F' );
if ($valoare <= 15) {
$numarHexa[] = $valoare;
$numarHexa[] = 0;
} else {
while ($valoare >= 15) {
$catul = $valoare / 16;
settype($catul, 'int');
$restul = $valoare % 16;
$valoare = $catul;
$numarHexa[] = $restul;
}
$numarHexa[] = $catul;
}
krsort($numarHexa);
foreach ($numarHexa as $key => $value) {
if ($value > 9) {
$numarHexa[$key] = $valoriHexa[$value];
}
}
$numarHexa = array_values($numarHexa); //reindexez si pastrez valorile pe pozitia initiala
return $numarHexa;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo $form;
} else {
if (!isset($_POST['r']) || !is_numeric($_POST['r']) || ($_POST['r'] > 255) || ($_POST['r'] < 0) ||
!isset($_POST['g']) || !is_numeric($_POST['g']) || ($_POST['g'] > 255) || ($_POST['g'] < 0) ||
!isset($_POST['b']) || !is_numeric($_POST['b']) || ($_POST['b'] > 255) || ($_POST['b'] < 0)) {
echo "date invalide!";
echo $form;
} else {
$culoareHexaR =& decimal2hexa($_POST['r']);
$culoareHexaG =& decimal2hexa($_POST['g']);
$culoareHexaB =& decimal2hexa($_POST['b']);
var_dump($_POST);
var_dump($culoareHexaR);
var_dump($culoareHexaG);
var_dump($culoareHexaB);
$culoareHexa = array_merge($culoareHexaR, $culoareHexaG, $culoareHexaB);
var_dump($culoareHexa);
$culoareHexaString = "";
for ($i = 0; $i < count($culoareHexa); $i++) {
$culoareHexaString .= $culoareHexa[$i];
}
echo $culoareHexaString;
}
}
? >
< html >
< body bgcolor="< ?php echo $culoareHexaString ? >">
< /body >
< /html >
If I declare the $culoareHexaString outside the if statement, it works just fine but I do not understand why.
in the following example it is not necessary to declade the $c variable outside the if statement.
$a = 5;
$b = 6;
if ($a > $b) {
echo "this will not be print";
} else {
$c = $a+$b;
}
$c variable will have a value of: < ?php echo $c ? >
what I am missing?
thanks!
Here:
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
...
} else {
// code not executed on GET/initial page view
}
You initialize $culoareHexaString in a block that is never executed, because the first view/non-submit is a GET request, and thuse the else condition is ignored.
Try initializing a default value outside that block, like:
$coloareHexaString = '#000000'; // default value?
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
...
} else {
// code not executed on GET/initial page view
}
As for your example, echoing $c would also be undefined if $a < $b, as it was never initialized.
<?php
$a = 7;
$b = 6;
if ($a > $b) {
echo "this will not be print";
} else {
$c = $a+$b;
}
echo $c; // this will be undefined.
?>
Because $culoareHexaString is not setted.
When you use if-else statement, actually there will be code blocks and at the run time according to the statement related block content is handling.
for detecting value setting, use isset() method.
Also you can set default color at the beginning like;
< ?php
error_reporting(E_ALL);
ini_set('display_errors',true);
$culoareHexaString = "#000000";
$form = "< form method='post' action=$_SERVER[PHP_SELF] >\n
R: < input type='text' name='r' >
G: < input type='text' name='g' >
B: < input type='text' name='b' >
< input type='submit' name='buton' value='go' >\n";
< /form >
$hexa = array();
$culoareHexa = array();
.....
i need help to generate series of number and it should be generate series of number when
form is submitted i have code but it not working properly?
For Example I Want Like This
00500
00501
00502
00503 and so on...
Here My Code
$random = rand(500,999);
$new_val = $random+1;
for($i=1;$i<=$new_val;$i++)
{
if( strlen($i)==1 )
{
$say='0000'.$i;
}
elseif( strlen($i)==2 )
{
$say='000'.$i;
}
elseif( strlen($i)==3 )
{
$say='00'.$i;
}
elseif( strlen($i)==4 )
{
$say='0'.$i;
}
elseif( strlen($i)==5 )
{
$say=$i;
}
}
<form method="post">
<input class="input_field" readonly="readonly" type="text"
name="sno" id="sno" value="<?=$say?>" >
<input type="submit" name="test" value="submit" />
</form>
Use sprintf to format it:
<?php
$start = rand(500, 999);
for ($i = 1; $i <= $start; $i++) {
$num = sprintf("%05d", $i);
echo $num;
}
?>
As Blender says, use sprintf to format your numbers
$start = rand(500, 999);
for ($i = $start; $i <= 999; $i++) {
$num = sprintf("%1$05d", $i);
echo $num . '<br>';
}
Note that the form must contain an "action" attribute
I am using ajax to load a php script that shows content based on screen resolution. The script works great, but the url that ajax calls is also called later in the script-- it holds all of the processing information. So, the result is all content is loaded via jquery and then duplicated with php. Example:
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: 'functions/maxresults.php',
type: 'GET',
data: {h: screen.height, w: screen.width}
}).done(function ( data ) {
alert("I am working!");
document.getElementById("container").innerHTML=data;
});
});
</script>
And further down
<?php
include_once("functions/maxresults.php");
?>
<?php
require_once("databasefunctions.php");
?>
<div id="container">
<div id="movieinfo">
<?php include("sidebar.php");?>
<?php
// get the function
include_once ('function.php');
if($_GET['page'])
$page = $_GET['page'];
else
$page = 0;
$maxresults = -1;
if(($_GET['w']) && ($_GET['h'])) {
$w = $_GET['w'];
$h = $_GET['h'];
}
if ($w == 1920) {
$maxresults = 24;
} else if ($w == 1600) {
$maxresults = 21;
} else if ($w == 1440){
$maxresults = 14;
} else if ($w == 1366) {
$maxresults = 21;
} else if ($w == 1024) {
$maxresults = 8;
} else
$maxresults = 6;
echo $maxresults;
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result);
?>
</div>
</div>
I cannot remove the php include or the script won't work at all. Would creating an include_once in the jquery work and if so, how do I code it?
PHP is a preprocessor. So, it doesn't matter that you are including it 'later' in the file. Your include_once will be processed and output before the JavaScript is executed.
I looks like the problem is that your include_once script is also outputting the container div. So, when you remove that line it breaks the javascript. Try this instead:
Remove the container div from you maxresults.php file.
Replace the <?php include_once('functions/maxresults.php);> with an empty div with an id of container: <div id="container"></div>
Your JavaScript should work now:
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: 'functions/maxresults.php',
type: 'GET',
data: {h: screen.height, w: screen.width}
}).done(function(resp) {
alert("I am working!");
$("#container").html(resp);
});
});
</script>
And further down
<div =id="container"></div>
maxresults.php
<?php
require_once("databasefunctions.php");
?>
<div id="movieinfo">
<?php include("sidebar.php");?>
<?php
// get the function
include_once ('function.php');
if($_GET['page'])
$page = $_GET['page'];
else
$page = 0;
$maxresults = -1;
if(($_GET['w']) && ($_GET['h'])) {
$w = $_GET['w'];
$h = $_GET['h'];
}
if ($w == 1920) {
$maxresults = 24;
} else if ($w == 1600) {
$maxresults = 21;
} else if ($w == 1440){
$maxresults = 14;
} else if ($w == 1366) {
$maxresults = 21;
} else if ($w == 1024) {
$maxresults = 8;
} else
$maxresults = 6;
echo $maxresults;
$currentpage = $page;
$page = $page*$maxresults;
$numpages = QuickQuery("SELECT COUNT(id) FROM movies WHERE visible=1");
$numpages = mysql_result($numpages, 0);
$numpages = $numpages/$maxresults-1;
$result = GetMoviesByRangeID($page, $maxresults);//Show <maxresults> pages at a time
DisplayResults($result);
?>
</div>