Referencing a dynamic variable? - php

for ($count = 1; $count <= 5; ++$count) {
$test = ${'node->field_aw_score_' . $count}[LANGUAGE_NONE][0]['value'];
echo $test;
}
Throws the error:
Notice: Undefined variable: node->field_aw_score_1
Notice: Undefined variable: node->field_aw_score_2
Notice: Undefined variable: node->field_aw_score_3
Notice: Undefined variable: node->field_aw_score_4
Notice: Undefined variable: node->field_aw_score_5
However, the variables do exist. I'm trying to reference:
$node->field_aw_score_1[LANGUAGE_NONE][0]['value']
then
$node->field_aw_score_2[LANGUAGE_NONE][0]['value']
etc. A dynamic variable. What am I doing wrong? Thanks.

Try this instead:
$node->{'field_aw_score_' . $count}

Related

how to access array in array when inserting json data to database?

I have json file that contains of 50 lines of similar
"results":[{"score":0,"team_id":126266},{"score":0,"team_id":125798}].
I need to insert this to database, but I keep getting errors:
Notice: Undefined index: results[0]:score[0] in D:\xampp\htdocs\json\index.php on line 26
Notice: Undefined index: results[1]:score[0] in D:\xampp\htdocs\json\index.php on line 27
Notice: Undefined index: results[0]:team_id[0] in D:\xampp\htdocs\json\index.php on line 32
Notice: Undefined index: results[1]:team_id[0] in D:\xampp\htdocs\json\index.php on line 33
Here's the code:
$pirmo_oponento_rezultatas = $row['results[0]:score[0]'];
$antro_oponento_rezultatas = $row['results[1]:score[0]'];
$fk_Komandosid_Komandos = $row['results[0]:team_id[0]'];
$fk_Komandosid_Komandos1 = $row['results[1]:team_id[0]'];
$sql="INSERT INTO rungtynes (pirmo_oponento_rezultatas,antro_oponento_rezultatas,fk_Komandosid_Komandos,
fk_Komandosid_Komandos1) VALUES ('$pirmo_oponento_rezultatas','$antro_oponento_rezultatas',
'$fk_Komandosid_Komandos','$fk_Komandosid_Komandos1')";
You can access as below
$pirmo_oponento_rezultatas = $row['results'][0]['score'];
$antro_oponento_rezultatas = $row['results'][1]['score'];

$_GET not being set

When printing $_GET there is an error:
Notice: Undefined variable: _GET
Testing code:
echo "query string: ".($_SERVER['QUERY_STRING']);
print_r($_GET);
The testing URL:
http://localhost/?foo=bar
The resulting output is this:
query string: foo=bar
Notice: Undefined variable: _GET in C:\Users\DGmoN\Google Drive\Code\Web\CMS2\index.php on line 3

Notice: Undefined index: category

Notice: Undefined index: category in /opt/lampp/htdocs/content/cron/check_deposits.php on line 18
$deposits=mysql_query("SELECT * FROM `deposits`");
while ($dp=mysql_fetch_array($deposits)) {
$received=0;
$txid='';
$txs=$wallet->listtransactions('',2000);
$txs=array_reverse($txs);
foreach ($txs as $tx) {
if($tx['category']!='receive') continue;
if ($tx['confirmations']<1) continue;
if ($tx['address']!=$dp['address']) continue;
$received=$tx['amount'];
$txid=$tx['txid'];
break;
This is how it came out of the box. I contacted support about the issue and they told me to make sure I was using PHP 5.3.1 which I am. What is wrong with this?
Here is a link to the full code:
http://diceking.tk/deposit.txt
You need to change from
if($tx['category']!='receive') continue;
to
if(isset($tx['category']) && $tx['category']!='receive') continue;
Check if the variables are set.
So, for the error about confirmations, you can use this:
if(isset($tx['confirmations']) && $tx['confirmations'] < 1) continue;
For the error about the undefined index: Category you can use this:
if(isset($tx['category']) && $tx['category']!='receive') continue;
I hope I'll helped you.
If this helped, please vote my answer.
Notice: Undefined index: amount in /opt/lampp/htdocs/content/cron/check_deposits.php on line 21
Notice: Undefined index: txid in /opt/lampp/htdocs/content/cron/check_deposits.php on line 22
Notice: Undefined index: amount in /opt/lampp/htdocs/content/cron/check_deposits.php on line 21
Notice: Undefined index: txid in /opt/lampp/htdocs/content/cron/check_deposits.php on line 22
Ahh so we have made it this far.. I tried this trick and it hated me for it:
if(isset($received=$tx['amount']) && $received=$tx['amount']) continue;

Notice: Undefined index: qnews in

<?php
_setView ( __FILE__ );
_setTitle ( $langArray ['add'].' '.$langArray['qnews'] );
Please give me some instruction for fix this problem:
Notice: Undefined index: qnews in
/home/market/apps/qnews/admin/add.php on line 3

'Undefined' Notice while populating arrays

While populating an array with data from a SimpleXML call, PHP throws exception to what it believes as 'Undefined' keys, however, the output is actually correct.
$doc = new SimpleXmlElement($http_result, LIBXML_NOCDATA);
$result = array();
$x = 0;
foreach($doc->users->user as $item) {
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
print json_encode($result);
This actually outputs what I expect, i.e. {"user":[{"id":"4843977","name":"Test New User","email":"test#newuser.com"}]}
However, the following errors are also present, and I'm not totally sure why - this doesn't appear in 5.2.6 but does for 5.2.10
Notice: Undefined index: user in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined offset: 0 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 1 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
Notice: Undefined offset: 2 in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: id in /var/vhosts/sys-dev/docs/file.php on line 36
Notice: Undefined index: name in /var/vhosts/sys-dev/docs/file.php on line 37
Notice: Undefined index: email in /var/vhosts/sys-dev/docs/file.php on line 38
I think You must change just ".=" to "=" in lines:
$result['user'][$x]['id'] = $item->id;
$result['user'][$x]['name'] = $item->name;
$result['user'][$x]['email'] = $item->email;
You don't define what are $result['user'] and $result['user'][$x].
You need to instantiate them as array so you won't get that error.
$result['user'] = array();
foreach($doc->users->user as $item) {
$result['user'][$x] = array();
$x++;
}
For the undefined indexes in the fields, the problem is similar. You use ".=" when the variable doesn't exists yet. So you should instantiate it first with an empty string.
$result['user'][$x]['name'] = '';
You need to initialize the $result array first:
$result = array('user' => array());
And since you’re using the string concatenation and assignment operator .=, that would also apply to the $result['user'][$x] arrays:
foreach($doc->users->user as $item) {
$result['user'][$x] = array(
'id' => null,
'name' => null,
'email' => null
);
$result['user'][$x]['id'] .= $item->id;
$result['user'][$x]['name'] .= $item->name;
$result['user'][$x]['email'] .= $item->email;
$x++;
}
But that’s not necessary since you can also write it like this:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id,
'name' => $item->name,
'email' => $item->email
);
}
Edit    Since we elaborated that the attributes of $item are SimpleXMLElement objects too, $item->attr[0] is required to address the string value itself. Thus:
$result = array('user' => array());
foreach($doc->users->user as $item) {
$result['user'][] = array(
'id' => $item->id[0],
'name' => $item->name[0],
'email' => $item->email[0]
);
}
It happens, because you're not just setting the array values, but you're concatenating to the current value:
$result['user'][$x]['id'] .= $item->id;
This line means "take the current value of $result['user'][$x]['id'] and add $item->id to it". The notice is then thrown, because the current value is not yet existent.
Amend the code to this
$result['user'][$x]['id'] = $item->id;
and you should be safe. No idea though, why 5.2.6 is not throwing the errors, maybe you should check with the error_reporting setting in the php.ini.

Categories