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();
.....
Related
I don't want to create a big if list for all the story chapters of my website.
if $commentformname[$a] is equal to the number of the current chapter, then I make an array $commentonchapter1[$a] saying that a comment was inserted in the chapter 1, and chapter 2, and so on.
So if a comment was posted in the chapter 2, a array called commentonchapter2 will be created.
if ($sitecomments == true) {
$c = 0;
$i = 0;
$z = 0;
#defines the comment form name, name of who commented, the comment text and time (to be converted, example: 2020-04-09T15:53:07.580Za > 12:53 09/04/2020)
while ($c <= $commentsnumb) {
$commentformname[$z] = $comments[$i];
$i++;
$commenttername[$z] = $comments[$i];
$i++;
$commenttext[$z] = $comments[$i];
$i++;
$commenttime[$z] = $comments[$i];
$i++;
$c++;
$z++;
}
#shows the text of the comments tab
echo '<hr class="'.$sitehr2.'" />'."\n";
echo $divzoomanim.'<'.$n.'><p></p><br /><b>'.$tabnames[7].': '.$orangespan.$commentsnormalnumb.$spanc.' '.$icons[12].'</b><br /><br /><p></p></'.$n.'>'.$divc.'<hr class="'.$sitehr.'" />'."\n";
$i = 0;
$z = 0;
#converts the comment date and time
#example
#from 2020-04-09T15:53:07.580Za
#to 12:53 09/04/2020
while ($z <= $commentsnumb) {
$commenttime[$z] = substr($commenttime[$z], 0, -1);
$commenttime[$z] = date("H:i d/m/Y", strtotime($commenttime[$z]));
$z++;
}
$a = 0;
$z = 0;
$i = 0;
#styles the form name
#example
#from "pequenata-comment-7"
#to '<b>Commented on form "Pequenata-comment-7"</b>'
while ($a <= $commentsnumb) {
if ($lang == $langs[0] or $lang == $langs[1]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt5.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
if ($lang == $langs[2]) {
$commentformname[$i] = '<b>'.$commenttxt3.' '.$commenttxt4.' '.strtolower($formtxt).':</b> "'.ucwords($commentformname[$i]).'"';
}
$a++;
$z++;
$i++;
}
$a = 0;
$a2 = 1;
$i = 0;
$z = 0;
$c = 0;
$v = 0;
$b = 0;
$commentschapternumb = 0;
#Generates the array of the website comments
while ($c <= $commentsnumb) {
#Generates the comments of the website if the strlen of $commentformname[$a] is equal to 45, String: <b>Commented on form "Pequenata-comment-7"</b>
if (strlen($commentformname[$a]) == 45) {
$cmntsgeral[$b] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Dispalyer disabled because of showing the wrong array (using $z, so it was tring to print a number on the array that didn't existed ($cmntschapter was incrementing on $z)
#echo $cmntsgeral[$z];
$a2++;
$b++;
}
#Generates the array of the chapter comments
if (strlen($commentformname[$a]) == 47) {
$cmntschapter[$v] = '<'.$m.' class="'.$textstyle2.'" style="text-align:left;border-width:3px;border-color:'.$bordercolor.';border-style:solid;"><div style="margin-left:5%;margin-right:5%;">'.'<br /><b>'.$a2.' - '.$commenttername[$i].'</b> - '.$commenttime[$i].'<br />'.$commentformname[$i].' '.'<hr class="'.$sitehr3.'" />'.$commenttext[$i].'<br /><br /><br /><br /><br />'.$divc.'</'.$m.'>'."<br />"."\n";
#Doesn't show the chapter comments because they shouldn't be showed in the website comments tab
#echo $cmntschapter[$z];
$v++;
$commentschapternumb++;
}
$a++;
$i++;
$z++;
$c++;
}
#Shows the website comments on the comments tab of the site
$z = 0;
while ($z <= $commentsnormalnumbtowrite) {
echo $cmntsgeral[$z];
$z++;
}
}
I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.
$continuetill = count($vendorsname);
for ($x = 0; $x < $continuetill; $x++)
{
echo $x;
echo $vendorsname[$x];
$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$low = $row[0];
$mid = $row[1];
$high = $row[2];
$verlow = $row[3];
$vermid = $row[4];
$verhigh = $row[5];
if(($low > $mid) && ($low > $high))
{
likely295Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high < 15))
{
possibly300Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high >= 15))
{
likely300Message($vendorsname[$x]);
}
elseif (($mid > $low) && ($mid > $high))
{
likely296Message($vendorsname[$x]);
}else
{
unknownMessage($vendorsname[$x]);
}
if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
{
if(($verlow > $vermid) && ($verlow > $verhigh))
{
verified295Message($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($verhigh > $verlow) && ($verhigh > $vermid))
{
verified300($vendorsname[$x]);
changeBackground($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($vermid > $verlow) && ($vermid > $verhigh))
{
verified296($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
}
}
mysqli_free_result($result);
}
}
Make sure you have error displaying turned on. Add at the beginning of your script:
ini_set('display_errors', 1);
to make sure you don't have any errors.
<?php
$hp = 0;
while($hp < 50) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp += 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp += 10;
} else {
echo "<p>Punch</p>";
$hp += 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
This is a PHP code. When I run it, it works fine. However, when I change it to this code below it doesn't.
<?php
$hp = 50;
while($hp > 1) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp -= 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp -= 10;
} else {
echo "<p>Punch</p>";
$hp -= 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
Please help. tHE CHANGES I MADE ARE THE HIGHLIGHTED ONES.
You never created $hp properly in the second version:
50;
doesn't do anything. It just tells php "here, have a 50", and php goes "gee, thanks, ok, whatever" and moves onwards. Then you have
while($hp > 1) {
Since $hp is undefined, it's null, and the code parses/executes as:
while($hp > 1) {
while(null > 1) {
while(0 > 1) {
FALSE -> exit loop
You never created $hp properly in the second version:
50;
If you do change it to $hp = 50;
I'm having trouble making a simple count work. Right now 0 is constantly displayed when I run the code and I know it's because I set count to 0. But it should be displaying the number of times "Fizz" is displayed.
I'm sure it's simple but I just can't see what!
public function __construct($firstParam, $secondParam, $firstSound = "Fizz", $secondSound = "Buzz", $numbers = 100) {
$this->firstParam = $firstParam;
$this->secondParam = $secondParam;
$this->firstSound = $firstSound;
$this->secondSound = $secondSound;
$this->numbers = $numbers;
$this->numsArray = $numsArray;
}
public function __toString() {
$count = 0;
for ($i = 0; $i < count($this->numsArray); $i++){
$val = $this->numsArray[$i];
if ($val == $this->firstSound) {
$count++;
}
}
$print = "Number of Fizzes: ".$count;
return $print;
}
public function execute() {
$this->numsArray = array();
if ($this->secondParam > $this->firstParam) {
for ($i = 1; $i <= $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->firstSound.$this->secondSound."\n";
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[] = "\n".$this->firstSound."\n";
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[] = "\n".$this->secondSound."\n";
} else {
$this->numsArray[] = "\n".$i."\n";
}
echo $this->numsArray[$i-1];
}
} else {
echo "\n".' First Number Bigger Than Second '."\n";
}
}
In your execute you are not assigning the values to numsArray[i] also you inject new line characters that will not match the equality you just when checking $val. Also I notice you use zero index to check them and index 1 to load it. Change execute to:
for ($i = 0; $i < $this->numbers; $i++){
if ($i % $this->firstParam == 0 && $i % $this->secondParam == 0) {
$this->numsArray[i] = $this->firstSound.$this->secondSound;
} elseif ($i % $this->firstParam == 0) {
$this->numsArray[i] = $this->firstSound;
} elseif ($i % $this->secondParam == 0) {
$this->numsArray[i] = $this->secondSound;
} else {
$this->numsArray[i] = $i;
}
echo $this->numsArray[$i];
This is a better binary string comparison for php...
if (strcmp($val, $this->firstSound) == 0)
How do I get this PHP code to output HTML in order to enter the player's number and generate the tournament?
<?php
class RoundRobin
{
var $MaxTeams;
var $MaxCombinations;
var $tourn;
var $mList;
var $cList;
var $cUsed;
function RoundRobin($max)
{
$this->MaxTeams=$max;
$this->MaxCombinations=($this->MaxTeams/2)*($this->MaxTeams-1);
}
function ShowSchedule($players,$totalChecks)
{
echo $players.' players'."\n";
for ($r=1; $r <= $players/2; $r++) echo 'Game'.$r;
echo "\n";
echo" +-";
for ($r=1; $r <= ($players/2)*6-2; $r++) echo '-';
echo "\n";
$index = 1;
for ($r=1; $r <= $players-1; $r++)
{
echo 'Week '.$r. '|';
for ($m=1; $m <= $players/2; $m++)
{
echo $this->tourn[$index]['one'].'&'. $this->tourn[$index]['two'];
$index++;
}
echo "\n";
}
echo "\n".$totalChecks,' combinations tried'. "\n\n";
}
function array_copy(&$dest,$source)
{
if(count($source)>count($dest)) {echo 'fatal'; exit;}
//for($a=0;$a<count($source);$a++)
$dest['one']=$source['one'];
$dest['two']=$source['two'];
}
function ClearArrays()
{
for ($i=0; $i <= $this->MaxCombinations; $i++)
{
$this->tourn[$i]['one']=0;
$this->tourn[$i]['two']=0;
$this->cList[$i]['one']=0;
$this->cList[$i]['two']=0;
$this->cUsed[$i]=0;
if($i<=$this->MaxTeams/2)$this->mList[$i] = 0;
}
}
function Scheldule($players)
{
while ($players <= $this->MaxTeams)
{
$combinations = $players/2 * ($players-1);
$totalChecks = 0;
$this->ClearArrays();
/* set up list of all combinations */
$m=1;
for ($a=1; $a<$players; $a++)
for ($b=$a+1; $b<=$players; $b++)
{
$this->cList[$m]['one'] = $a;
$this->cList[$m]['two'] = $b;
$m++;
}
$roundCount=1;
$index=1;
while ($roundCount <= $players-1)
{
$matchCount = 1;
$round_set = 0;
for ($i=0; $i<=$this->MaxTeams/2; $i++) $this->mList[$i] = 0;
$startC = $roundCount;
while ($matchCount <= $players/2)
{
$c = $combinations + 1;
while ($c > $combinations)
{
$c = $startC;
/* find an unused pair that would be legitimate */
while (
($c <= $combinations)
&&
( //
($round_set & (1 << $this->cList[$c]['one'])) ||
($round_set & (1 << $this->cList[$c]['two'])) ||
(!empty($this->cUsed[$c]))
)
) $c++;
if ($c > $combinations)
{
do {
$this->mList[$matchCount] = 0;
$matchCount--;
$index--;
$round_set &= ~(1 << $this->cList[$this->mList[$matchCount]]['one']);
$round_set &= ~(1 << $this->cList[$this->mList[$matchCount]]['two']);
$this->cUsed[$this->mList[$matchCount]] = false;
$this->tourn[$index]['one'] = 0;
$this->tourn[$index]['two'] = 0;
}
while ($this->cList[$this->mList[$matchCount]]['one'] != $this->cList[$this->mList[$matchCount]+1]['one']);
$startC = $this->mList[$matchCount]+1;
}
}
$this->array_copy(&$this->tourn[$index],$this->cList[$c]);
$totalChecks++;
if (($totalChecks % 1000) == 0) printf("%d\033A\n", $totalChecks );
$this->cUsed[$c] = true;
$this->mList[$matchCount] = $c;
$startC = 1;
$round_set |= (1 << $this->cList[$c]['one']);
$round_set |= (1 << $this->cList[$c]['two']);
$index++;
$matchCount++;
}
$roundCount++;
}
/* yahoo!, scheduled all the rounds */
printf(" " );
$this->ShowSchedule($players,$totalChecks);
/* try and make a schedule using two more teams */
$players += 2;
}
}
}
?>
It's a PHP class. Reading the manual might help you.
Something along the lines of this might work:
$rr = new RoundRobin( 5 );
$rr->Scheldule( $_POST['PlayerCount'] );
But I can't be bothered deciphering the code to work out if it really will.
Also, if you haven't yet learnt HTML forms, read a tutorial like w3schools.