kindly need your help,
I'm trying to use function to change the name of a PHP variable. I tried to change $name to $graph1, $graph2 ... $graph31. Here's the code I made:
<?php
function bar($name, $label, $value) {
return
$name = new BAR_GRAPH("pBar");
$name->values = $value;
$name->labels = $label;
$name->labelColor = "white";
$name->labelBGColor = "#282828";
$name->barBorder = "0px";
$name->barColors = "white";
$name->barBGColor = "#282828";
$name->showValues = 0;
$name->percValuesColor = "white";
$name->barColors = "white";
echo $name->create();
}
bar("$graph1","ornamen","$totkakiOrnamen;7");
?>
Unfortunately the code doesn't work, it says:
Notice: Undefined variable: graph in C:\xampp\htdocs\app\process.php
on line 56
I don't know what's wrong. How can I change $name to $graph1, $graph2 ... $graph31 ?
PS: The return code looks weird because I used gerd-tentler's script to generate horizontal bar. http://www.gerd-tentler.de/tools/phpgraphs/?page=introduction
This may or may not be what you are asking but maybe....??
<?php
function bar($name, $label, $value) {
$$name = new BAR_GRAPH("pBar");
$$name->values = $value;
$$name->labels = $label;
$$name->labelColor = "white";
$$name->labelBGColor = "#282828";
$$name->barBorder = "0px";
$$name->barColors = "white";
$$name->barBGColor = "#282828";
$$name->showValues = 0;
$$name->percValuesColor = "white";
$$name->barColors = "white";
$$name->create();
return $$name;
}
$graph1 = bar('graph1',"ornamen","$totkakiOrnamen;7"); ?>
if not, the only other guess is that you are trying to do a variable variable:
http://php.net/manual/en/language.variables.variable.php
Related
Partly confused (still) because the variable $investment_type resulting of "Creating default object from empty value" when I follow this previous question of mine Laravel list() with each() function error with deprecated function.
This is the original code.
$assetsData = ClientPropertyManagement::find($assets_id);
$investmentType = Input::get('investmenttype'.$assets_id);
$legalname = Input::get('legalname'.$assets_id);
$ownership = Input::get('ownership'.$assets_id);
$tic = Input::get('tic'.$assets_id);
$entity_id = Input::get('entity_id'.$assets_id);
foreach($investmentType as $investment_type) {
list($key,$value) = each($legalname);
list($key,$valueOwner) = each($ownership);
list($key,$valueTic) = each($tic);
list($key,$valueEntityId) = each($entity_id);
if($valueEntityId == 0) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($valueEntityId);
}
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $value;
$assetEntity->ownership = $valueOwner;
$assetEntity->ticnum = $valueTic;
$assetEntity->save();
}
Here's what I did in my code.
foreach( $investmentType as $key => $investment_type ) {
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $legalname[$key];
$assetEntity->ownership = $ownership[$key];
$assetEntity->ticnum = $tic[$key];
if ( $entity_id[$key] == 0 ) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($investment_type);
}
$assetEntity->save();
}
The problem is that $assetEntity isn't created yet, and you are trying to use as an object. To solve that, you need to change the position where you instantiate the object and create the variable $assetEntity:
foreach( $investmentType as $key => $investment_type ) {
if ( $entity_id[$key] == 0 ) {
$assetEntity = new ClientEntityManagement;
$assetEntity->property_id = $assetsData->property_id;
$assetEntity->client_id = $id;
} else {
$assetEntity = ClientEntityManagement::find($investment_type);
}
$assetEntity->investment_type = $investment_type;
$assetEntity->entity_name = $legalname[$key];
$assetEntity->ownership = $ownership[$key];
$assetEntity->ticnum = $tic[$key];
$assetEntity->save();
}
That way $assetEntity will be created, and then you populate the other attributes.
Also, you may want to use the IoC Container and replace the new ClientEntityManagement with App::make('ClientEntityManagement'). Read more at https://laravel.com/docs/4.2/ioc
I get a little confuse here on how to alternatively replace the each() function since it was deprecated and I'm aware of that and fixed some of the while( list() = each() ) error case in my project. However, what other option should I use for this case:
foreach($new_id as $new_ids) {
list($key,$valueAddress) = each($address);
list($key,$valueCity) = each($city);
list($key,$valueState) = each($state);
if(isset($_POST['publicOnly'])) {
list($key,$valuePublicOnly) = each($publicOnly);
} else {
$valuePublicOnly = 0;
}
$propertyAddress = PropertyAddressManagement::find($new_ids);
$propertyAddress->address = $valueAddress;
$propertyAddress->city = $valueCity;
$propertyAddress->state = $valueState;
$propertyAddress->publicOnly = $valuePublicOnly;
$propertyAddress->save();
}
You're not using the keys so just get the current value and then move to the next one:
foreach($new_id as $new_ids) {
$propertyAddress = PropertyAddressManagement::find($new_ids);
$propertyAddress->address = current($address);
$propertyAddress->city = current($city);
$propertyAddress->state = current($state);
$propertyAddress->publicOnly = isset($_POST['publicOnly']) ? current($publicOnly) : 0;
$propertyAddress->save();
next($address); next($city); next($state); next($publicOnly);
}
However, if the keys are the same in all of the array then I think really this should work:
foreach($new_id as $key => $new_ids) {
$propertyAddress = PropertyAddressManagement::find($new_ids);
$propertyAddress->address = $address[$key];
$propertyAddress->city = $city[$key];
$propertyAddress->state = $state[$key];
$propertyAddress->publicOnly = isset($_POST['publicOnly']) ? $publicOnly[$key] : 0;
$propertyAddress->save();
}
I'm currently facing a problem here, I have many different strings like these:
"he#00ff00llo"
"#cc9200test"
And so on.
I'm outputting this on an HTML page (Grabbing it through my database)
Anyways, what I want to achieve is to read those #00ff00 and output it as the color itself.
EDIT:
these are usernames, and most of the time they go like this: #ff0000#SomeName | Which I then want to turn into <span style="color: #ff0000">#SomeName</span> oh and the there are usernames with several colorcodes.
EDIT:
My friend gave me this code which solved my problem. :)
IT WAS SOLVED IN PHP
ALSO THE CODE BELOW (Posted by Oriol) IS WORKING! :)
THIS CODE WORKS(PHP):
<?php
function colorCodesRenderProperly($name)
{
$name = htmlspecialchars($name);
if(preg_match('/^(#[0-9a-fA-F]{6})+$/', $name) === 1)
{
return $name;
}
preg_match_all('/#[0-9a-fA-F]{6}/', $name, $codes);
$replaced = array();
$codes_original = $codes;
$i = 0;
$count = 1;
foreach($codes[0] as &$code)
{
if(in_array($codes_original[0][$i], $replaced))
{
continue;
}
$code = sprintf('%02s', dechex((hexdec($code[1].$code[2])/255*128)))
.sprintf('%02s', dechex((hexdec($code[3].$code[4])/255*128)))
.sprintf('%02s', dechex((hexdec($code[5].$code[6])/255*128)));
$name = str_replace($codes_original[0][$i], "<span style=\"color: #$code;\">", $name, $count);
$replaced[] = $codes_original[0][$i];
$i++;
$count = 1;
}
while($i > 0)
{
$name .= "</span>";
$i--;
}
return $name;
}
?>
Try this:
var txt = document.createTextNode("he#00ff00llo"),
wrapper = document.createElement('span'),
regExp = /#[\da-f]{6}/i,
pos;
wrapper.appendChild(txt);
while(~(pos = txt.nodeValue.search(regExp))) {
txt = txt.splitText(pos);
var span = wrapper.cloneNode(false);
span.style.color = txt.nodeValue.substr(0,7);
txt.nodeValue = txt.nodeValue.substr(7);
span.appendChild(txt);
wrapper.appendChild(span);
}
// append wrapper to the DOM
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I am getting this notice:
PHP Notice: Undefined index: votefor in /home/.../savevote.php on line 2
How can I fix this?
Here is my line 2 in savevote.php:
<?php
$votefor = $_REQUEST["votefor"];
Thank you
Here is the code:
<?php
$votefor = $_REQUEST["votefor"];
// Returns a random RGB color (used to color the vote bars)
function getRandomColor()
{
$r = rand(128,255);
$g = rand(128,255);
$b = rand(128,255);
$color = dechex($r) . dechex($g) . dechex($b);
echo "$color";
}
//Get the IP of the user
$domain = $_SERVER["REMOTE_ADDR"];
$today = date("m/d/Y");
echo "<table id=\"tblResults\" align=\"center\">";
//If votefor is null, then we're just viewing results, so don't log the IP
if ($votefor != "")
{
//Load the addresses XML file
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/addresses.xml");
$addresses = $doc->getElementsByTagName("address");
$pVoted = false;
$pFound = false;
//Loop through the addresses nodes and see if the person has voted before
foreach( $addresses as $address )
{
$lastvisits = $address->getElementsByTagName("lastvisit");
$lastvisit = $lastvisits->item(0)->nodeValue;
$ips = $address->getElementsByTagName("ip");
$ip = $ips->item(0)->nodeValue;
if ($ip == $domain)
{
$pFound = true;
if ($lastvisit == $today)
$pVoted = true;
else
{
$lastvisits->item(0)->nodeValue = $today;
$doc->save("../vote_dir/xml/addresses.xml");
}
break;
}
else
continue;
}
if ($pVoted == true) //Already voted
{
echo "<tr><td colspan=\"3\" class=\"message\">Έχετε ήδη ψηφίσει!</td></tr>";
}
else //Update the XML files
{
if ($pFound == false) //Add new node for IP and date to addresses.xml
{
echo "<tr><td colspan=\"3\" class=\"message\">Ευχαριστούμε που ψηφίσατε!</td></tr>";
$newAddy = $doc->getElementsByTagName('addresses')->item(0);
$newAddressElement = $doc->createElement('address');
$newLastVisitElement = $doc->createElement('lastvisit');
$newAddressElement->appendChild($newLastVisitElement);
$newIPElement = $doc->createElement('ip');
$newAddressElement->appendChild($newIPElement);
$dayvalue = $doc->createTextNode($today);
$dayvalue = $newLastVisitElement->appendChild($dayvalue);
$ipvalue = $doc->createTextNode($domain);
$ipvalue = $newIPElement->appendChild($ipvalue);
$newAddy->appendChild($newAddressElement);
$doc->save("../vote_dir/xml/addresses.xml");
}
else
{
echo "<tr><td colspan=\"3\" class=\"message\">Ευχαριστούμε για την ψήφο σας.</td></tr>";
}
// Update the vote
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$pollitems = $doc->getElementsByTagName("pollitem");
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
if ($entry == $votefor)
{
$votes = $pollitem->getElementsByTagName("votes");
$count = $votes->item(0)->nodeValue;
$votes->item(0)->nodeValue = $count + 1;
break;
}
}
$doc->save("../vote_dir/xml/results.xml");
}
}
else
{
echo "<tr><td colspan=\"3\" class=\"message\">Αποτελέσματα Ψηφοφορίας Μέχρι Στιγμής</td></tr>";
}
// Get max vote count
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$maxvotes = 0;
$pollitems = $doc->getElementsByTagName("pollitem");
foreach( $pollitems as $pollitem )
{
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
$maxvotes = $maxvotes + $vote;
}
// Generate the results table
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$pollitems = $doc->getElementsByTagName("pollitem");
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
$tempWidth = $vote / $maxvotes;
$tempWidth = 300 * $tempWidth;
$votepct = round(($vote / $maxvotes) * 100);
echo "<tr><td width=\"20%\" class=\"polls\">$entry</td>";
echo "<td width=\"30%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: ";
getRandomColor();
echo "; width: $tempWidth px;\">$votepct%</div></td><td width=\"20%\">($vote votes)</td></tr>";
}
echo "<tr><td class=\"total\" colspan=\"3\">$maxvotes άτομα ψήφισαν μέχρι τώρα.</td>";
echo "</table>";
?>
#Brian
<script language="javascript">
function setVote(voteName)
{
document.getElementById("votefor").value = voteName;
}
function confirmSubmit()
{
if (document.getElementById("votefor").value == "")
{
var agree=confirm("Παρακαλώ επιλέξτε μια απάντηση, για να ψηφίσετε");
return false;
}
}
</script>
The request parameter isn't being passed. If you know why it might be missing and just want to prevent the notice, you can say:
$votefor = isset( $_REQUEST["votefor"] ) ? $_REQUEST["votefor"] : null;
It is not safe to use $_REQUEST because it can be $_GET or $_POST, better is specify which you want.
At second you need to check if the array key exists. This can officially be done with array_key_exists(). But unfortunately this function is a little bit slow. You can replace it by using the isset() function, but this says a null value is not set and returns false when it is null. The best approach is to use them both, first the isset and then the array_key_exists:
<?php
if (isset($_POST['votefor']) || array_key_exists($_POST['votefor'])) {
// do something
}
?>
Or use only isset if you are sure the value won't be null.
If you are almost 100% sure the index votefor exists in the array you need to debug it. var_dump the $_REQUEST array to see which items (and indexes) are in there and look what you did wrong.
1/ Check you have a request parameter named votefor
2/ Check you didn't misspelt it
Handle it like so:
if(array_key_exists("votefor", $_REQUEST)) {
...
}
You may ignore notice reports by setting error_reporting no to show them
error_reporting(E_ALL & ~E_NOTICE);
Is there a quick and easy way to check if any of my $_POST data has the same value?
I need it as a conditional statement...
Example:
$week1 = $_POST['Week_1'];
$week2 = $_POST['Week_2'];
$week3 = $_POST['Week_3'];
$week4 = $_POST['Week_4'];
$week5 = $_POST['Week_5'];
$week6 = $_POST['Week_6'];
$week7 = $_POST['Week_7'];
$week8 = $_POST['Week_8'];
$week9 = $_POST['Week_9'];
$week10 = $_POST['Week_10'];
$week11 = $_POST['Week_11'];
$week12 = $_POST['Week_12'];
$week13 = $_POST['Week_13'];
$week14 = $_POST['Week_14'];
$week15 = $_POST['Week_15'];
$week16 = $_POST['Week_16'];
$week17 = $_POST['Week_17'];
If the values of any of the weeks = equal the value of any of the other weeks, error...
Is there a quick way to do this in PHP?
Thanks!
Chris
first though to pop in to my head:
$r=array_unique(array($week1, ...));
if (count($r) !=17){
//error
}
If the only $_POST values you have are 'Week_1' through 'Week_17' then
if (count(array_unique($_POST)) === count($_POST)) {
//all unique values, do stuff...
}
Just loop through the pairs and compare them:
$weeks=array();
foreach(range(1,17) as $i)
{
array_push($weeks,'Week_' . $i);
}
foreach(range(1,16) as $i)
{
foreach(range($i+1,17) as $j)
{
if($_POST[$weeks[$i]]==$_POST[$weeks[$j]])
{
die("Rut-roh!");
{
}
}
}