How can i solve this error: llegal string offset 'text'? - php

I have this code :
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];}
I read about this error on this site but i have no ideea how to apply the fix on my particular code. I`m sorry if i repost the problem.

Try doing: var_dump($userdata->yim); to verify if yim does in fact exist and contains the key 'text'.
Or even just var_dump($userdata);

$userdata->yim['text'] is not set, so you should check for that first:
if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
However, if that section of code relies on that value, something is wrong before that and this will just hide that problem.

In order to debug this you will need to utilize print_r() so that you can see the contents of your object or array:
// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';
// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];
}

Related

What does this do in PHP/Laravel?

I have been trying to figure out some code in Laravel by reading the docs and stuff. I came across a piece of code that I cant quit figure out what it is doing. Could someone explain this? Here is the piece of code in the project I am working on:
// Create placeholders for the mobile app's video settings
$publicVideosEnabled = ( $settings['publicVideosEnabled'] == 1 );
I am specifically asking about $settings['publicVideosEnabled'] == 1 section.
Thanks everyone!
EDIT: For more information, here is where some of the variables are coming from:
$location = Location::find($location_id);
$settings = $this->apiHelper->getFormattedLocationSettings($location);
$publicVideosEnabled = ( $settings['publicVideosEnabled'] == 1 );
I am trying to add this line of code:
$notificationsEnabled = ( $settings['notificationsEnabled'] == 1);
But I am getting this error:
Undefined index: notificationsEnabled at $notificationsEnabled = ( $settings['notificationsEnabled'] == 1);
If $settings['publicVideosEnabled'] equals 1 then $publicVideosEnabled is true, otherwise it is false.

Or operand not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
The error is probably something stupid, but I can't find the mistake to save my life.
I have this piece of code in my program:
$validate = (
$num_rows > 1 ||
$num_rows == 0);
if ($validate) {
$data['code'] = 2;
} else {
$data['code'] = 1;
while ($a = $resultados->fetch_assoc()) {
$data['response'] = $a;
}
}
I 'believe' its written correctly, however when ran, I'm getting the error 500 and the logs read:
Syntax error: Unexpected '$num_rows' (T_VARIABLE) on line 21...
Line 21 is the second validation for $num_rows.
Any suggestions?
Thanks in advance!
I think you just forgot to put a semicolon at the end of the code line that comes before your line starts with $validate = ...
Would be awesome to see the lines above this code snippet.
$validate = ($num_rows > 1||$num_rows == 0);
Try this, sometimes the space between || and the second condition make a strange syntax error. I'm curious, which IDE/text editor are you using.
I think it isn't a PHP core problem, but it come from something else, please give us more context :)
By the way, you could replace your condition by
$validate = $num_rows >= 0;
it's exactly the same and you avoid your problem

Can't use Get Method in PHP

i'm not good enough at english and PHP. So I have a question: How to get result by 'GET' request?
i have an url like this:
laporanpemesanancetak.php?TANGGAL_PERIODE1=2016-05-01&TANGGAL_PERIODE2=2016-05-23
and my GET method like:
if (isset($_GET["$TANGGAL_PERIODE1"])) {
$TANGGAL_PERIODE1 = $_GET["TANGGAL_PERIODE1"];
$TANGGAL_PERIODE2 = $_GET["TANGGAL_PERIODE2"];
$query = mysql_query("select p.PURCHASE_ORDER_PEMESANAN,p.TANGGAL_PEMESANAN,s.NAMA_SUPPLIER,i.NAMA_BAHAN_BAKU,p.JUMLAH_PEMESANAN,i.SATUAN,k.NAMA_KARYAWAN
from pemesanan p,supplier s,inventori i,karyawan k
where p.NPWP_SUPPLIER = s.NPWP_SUPPLIER and p.ID_BAHAN_BAKU = i.ID_BAHAN_BAKU and p.NIP_KARYAWAN = k.NIP_KARYAWAN and p.TANGGAL_PEMESANAN between '$TANGGAL_PERIODE1' and '$TANGGAL_PERIODE2' order by p.PURCHASE_ORDER_PEMESANAN");
while ($row = mysql_fetch_array($query)) {
$pdf->Cell(27,8,$row["PURCHASE_ORDER_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["TANGGAL_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_SUPPLIER"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_BAHAN_BAKU"],1,0,"C");
$pdf->Cell(27,8,$row["JUMLAH_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["SATUAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_KARYAWAN"],1,0,"C");
$pdf->Ln();
}
}
and the result is show nothing, i've tried using
if (isset($_GET["$TANGGAL_PERIODE1"])&&$_GET["$TANGGAL_PERIODE2"])
but the result is same, i've tried my sql query into mysql and it works.
Can someone help me please? it would be great :)
$_GET["$TANGGAL_PERIODE1"]
Note the $ in the key. This will try to evaluate the variable $TANGAL_PERIODE1 rather than look a key of the $_GET array named TANGAL_PERIODE1
Be sure to not ignore E_NOTICE and E_WARNING messages when developing. These are important clues, this would generate a notice of an undefined variable.

php echo not working after elseif

I am trying to extract data from an XML file using simplexml. I am running a series of while and foreach loops to dig down to what I need.
However, I have found that after the elseif is finished I can no long display additional data to the screen (echo and print_r don't work). I have attached a code snippet for the elseif loop through the end of the end of the script. Please excuse the lousy code, I am a relatively new programmer.
elseif ($select_vars[1]==-1) {
$sub1=0;
$sub2=0;
$temp_count = 1;
$modnum=intval($select_vars[0]);
$sub1m = $xmls->module->{
intval($select_vars[0])}
->count();
while ($sub1 < $sub1m) {
foreach($xmls->module->{
$modnum}
->sublist1->{
$sub1}
as $hit2){
$sub2m = $xmls->module->{
$modnum}
->sublist1->{
$sub1}
->count();
while ($sub2 < $sub2m) {
foreach($xmls->module->{
$modnum}
->sublist1->{
$sub1}
->sublist2->{
$sub2}
as $hit3){
$template_string .= $temp_count . " - " . (string)$hit3 . "<br/>";
$temp_count = $temp_count+1;
$template_list[] = (string)$hit3;
}
$sub2 = $sub2+1;
}
$sub1 = $sub1+1;
$sub2 = 0;
}
}
echo "sub1 = " . $sub1 . "<br/>";
}
echo "here <br/><br/>".$template_string;
print_r ($template_list);
$template_list = array_unique($template_list);
sort($template_list);
print_r ($template_list);
I know the loops extract the right information because I can echo anywhere in the various loops except after the esleif closing bracket.
A live copy of the code is available at: http://www.aquilasolutions.us/software/templates/pages/filter-list.php
EDIT:
The output SHOULD be 3 selects and when the top box is filled when the others are empty a series of line (up to 400+) in the format ### - ####-####-####-#### should appear.
EDIT 2:
To View the error:
1. set select "Module " to be anything
2. Set select sublist 1 and sublist 2 to be empty
3. Make sure the array at the top has 3 keys (it represents a session cookie called filter-list and represents the selected items)
EDIT 3:
I have tracked down the error but not been able to resolve it.
There is a fatal error in my log (Fatal error: Call to a member function count() on null) on the line:
$sub2m = $xmls->module->{$modnum}->sublist1->{$sub1}->count();
I tried putting an isnull catch in but then there was a fatal error on the isnull....
I tracked down the issue and as expected it was my own stupidity!
I typed the wrong variable for the while ($sub2 < $sub2m) loop.
It SHOULD have been while ($sub2 < $sub2num)

variable sphinx filter with PHP

i m sorry for my english, i m french, i need your help for one thing please :
It s about filtering in php sphinx
this my code :
$filtres= array();
if(isset($_POST['Pharmacie']) and $_POST['Pharmacie'] ="1" ){ $filtres[]= 1;}
if(isset($_POST['Autres']) and $_POST['Autres'] ="8" ){ $filtres[] = 8;}
$varfiltres = 'array('.implode(" , ",$filtres).')';
if($filtres != array()){
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
}
i have error :
Warning: assert(): Assertion failed in \sphinxapi.php on line 850
in case only one variable isset (pharmacie or Autres) that work!
also if i do $sphinx->SetFilter('Type', array(1,8)) it work!
thanks for help.
SetFilter, takes an array directly. With:
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
you are first converting the Array to a String, and then putting it in an new array. Don't do that :)
This is all that is needed:
$sphinx->SetFilter('Type', $filtres);

Categories