PHP - Trying To Get Property Of Non-object, Simplexml Question.
So I'm getting a "Notice: Trying to get property of non-object in" error when I'm trying to pull data from a xml. I have a complete xml that has all of the potential node that it could ever have and it loads up fine -no errors, but when I load a different xml that doesn't have all of the possible nodes it returns this error several times here is a sample of my code:
$source = 'TestFile.xml'; $xml = new SimpleXMLElement($source,null,true); $result = mysql_query("SELECT * FROM tblvalues") or die(mysql_error()); //Return the table row by row while($row = mysql_fetch_array($result)) { $curblk = substr($row['fldelement'],0,3); switch ($row['fldelement']){ case "E06_04": case "E06_05": case "E06_07": case "E06_08": $cursub = $curblk."_04_0"; $xmlVal = $xml->Header->Record->$curblk->$cursub->$row['fldelement']; //grab value from xml break; ... ... ... // the cases go on for a while... ... case "E07_05": case "E07_06": case "E07_07": case "E07_08": $cursub = $curblk."_03_0"; $cursub2 = $curblk."_05_0"; $xmlVal = $xml->Header->Record->$curblk->$cursub->$cursub2->$row['fldelement']; //this is the line that the error points to break; ... ... ... And here is the sample of the xml that returns the error: Code: [Select] <E07> <E07_01>-20</E07_01> <E07_02>0</E07_02> <E07_15>-25</E07_15> <E07_16>-25</E07_16> <E07_17>-25</E07_17> <E07_18_0> <E07_18_01> <E07_18>-20</E07_18> <E07_19>-20</E07_19> <E07_20>-20</E07_20> </E07_18_01> .... .... .... The obvious problem (to me) is that my code is trying to return a value from a nonexistent xml node, for example: $xmlVal = $xml->Header->Record->$curblk->$cursub->$cursub2->$row['fldelement']; is looking for: $xmlVal = $xml->Header->Record->E07->E07_03_0->E07_05_0->E07_05 Ok, I get that, so how do I rewrite this so it's not blowing up on something so simple? Quick notes: -This same code works for a more complete xml -Rewriting the xmls is not an option as I have no control over those Edit: Almost forgot, thanks! Similar TutorialsHey all, I want to have an object that has a property which is an object containing instances of other objects. I try this: Code: [Select] class Blog extends Posts { public $has_posts; public function __construct($a,$b,$c){ $has_posts = (object) array_merge((array) $a, (array) $b, (array) $c); } } class Posts { public $b; public function __construct($b){ $this->b = $b; } } $post1 = new Posts(1); $post2 = new Posts(2); $post3 = new Posts(3); $blog = new Blog($post1,$post2,$post3); var_dump($blog->has_posts); //null foreach($blog->has_posts as $post){ //Invalid argument supplied for foreach() echo $post->b; } But as you see, has_posts is null, not an object containing other objects. Thanks for response. Iam trying to access amazon API and iam receiving XML perfectly but now I dont know how to traverse all records as in the below example Iam having 3 records but can't travers the xml using foreach Loop Please help me out and tell me how to use foreach loop ?? Its urgent $search="9781439181799,9780393334296,9781605298627"; $result = $obj->getItemByUpc($search, "Books"); foreach($result->children() as $key ) { $data = $key->Items->Request->ItemLookupRequest->ItemId."<br/ >"; echo $data; } Moderator Edit: added [php] tags Hi all, I have a script which I coded not long ago which works fine but there is one error which says: Notice: Trying to get property of non-object in /home/www/***-*****.com/hts.php on line 374 I carnt see why its showing the error. $fetch2=mysql_query("SELECT crew FROM users WHERE username='$username'") or die (mysql_error()); $fetch1 = mysql_fetch_object($fetch2); $crewrep1=mysql_query("SELECT * FROM crews WHERE name='$fetch1->crew'") or die (mysql_error()); $crewrep = mysql_fetch_object($crewrep1); $result = $rep + $reps; $result55 = $crewrep->rep + $reps; // Line 374 Can anyone else see why its showing the error? Thanks for your help. I've been getting this error message: Trying to get property of non-object in /application/controllers/index.php on line 37 Here is line 37 $this->view->title = $pageInfo[0]->title; any help would be much appreciated. We have a complex, json object which is multiple levels deep. We need to search for a given property/key in the entire object (not just first occurrence, but all), and if we find the property, return where in the object each property exists, so we can modify the properties value. Figure it needs to be recursive. I am trying to run a count query and faced with the error: Notice: Trying to get property of non-object in Not sure how to resolve this error in these lines:
if($result->num_rows > 0){ while($row = $result->fetch_rows()){ Code: <?php $sql = "SELECT Form_Group COUNT(Presence LIKE '/') AS Present, COUNT(Presence LIKE 'N') AS Absent, FROM 'attendance'"; $result = $conn->query($sql); ?> <?php //Fetch Data form database if($result->num_rows > 0){ while($row = $result->fetch_rows()){ ?> <tr> <td><?php echo $row['Form_Group']; ?></td> <td><?php echo $row['Present']; ?></td> <td><?php echo $row['Absent']; ?></td> </tr> <?php } } else { ?> <tr> <th colspan="6">There's No data found!!!</th> </tr> <?php } ?>
Keep getting this error from line 19 (Notice: Trying to get property of non-object in /clientdata/zeus-dynamic-1/s/o/blahblah/www/tyson/onion.php on line 18)... have no idea how to get rid of it. Do I have to use isset? If so.. where??? (obviously I've replaced site address with website) Code: [Select] include('simple_html_dom.php'); $story = array(); getArticles('website'); function getArticles($page) { global $story, $desc, $read; $html = new simple_html_dom(); $html->load_file($page); $items = $html->find('div[id=story_content]'); foreach($items as $post) { $story[] = array($post->children(0)->plaintext, $post->children(2)->next_sibling()->innertext, $post->children(4)->innertext); } } $i = 0; foreach($story as $item) { $syurl = str_replace('href="', 'href="http://website', $item[2]); echo '<p class="storytitle">' . $item[0] . '</p>'; echo '<p class="storyexcerpt">' . $item[1] . '</p>'; echo '<p class="storyread">' . $syurl . '</p>'; if (++$i == 3) break; } Any ideas? This line is causing the issue: $story[] = array($post->children(0)->plaintext, $post->children(2)->next_sibling()->innertext, $post->children(4)->innertext); Likewise, it does post the 3 storys below the 2 error lines...? Hello, I'm currently in the process of trying to learn php. I've been working on an issue with something I am trying to code from a book I purchased "PHP and MySQL Web Development 5th Ed." by Luke Welling and Laura Thomson. So far the book has been great, but I'm trying to figure out using MySQL and php. When I try to run this particular script for a query for a book store, I get the error: Notice: Trying to get property of non-object in C:\php-book\ch11\results.php on line 37. This is the code: Code: [Select] $query = "select * from books where ".$searchtype."like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for($i=0; $i<$num_results; $i++) { $row = $result->fetch_assoc(); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br />Author: "; echo stripslashes($row['author']); echo "<br /> ISBN: "; echo stripslashes($row['isbn']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; } $result->free(); $db->close(); Line 37 is specifically: $num_results = $result->num_rows; I've been bashing my head for a couple of hours here, thank you in advance to whoever can offer any help. Hi Guys Trying to get xml elements to display in a html table using the following code Code: [Select] <?php session_start(); include_once('includes/connect_inc.php'); $file="https://www.cdlvis.com/lookup/getxml?username=username&mode=test&key=password&vrm=".$_POST['veh_reg'].""; $dom=new DOMdocument(); $dom->load($file); $xml=simplexml_import_dom($dom); ?> and Code: [Select] <form name="vehicle_details" method="post" action="add_vehicle.php"> <table> <tr> <td>Registration Number:</td><td><input name="reg_num" type="text" value="<?php echo $xml->result[0]->vrm;?>" readonly="true" /></td></tr> <tr> <td>Make:</td><td><input name="make" type="text" value="<?php echo $xml->result[0]->make;?>" readonly="true" /></td></tr> <tr> <td>Model:</td><td><input name="model" type="text" value="<?php echo $xml->result[0]->model;?>" readonly="true" /></td></tr> <tr> <td>Date of Manufactu </td><td><input name="date_man" type="text" value="<?php echo $xml->result[0]->date_manufactured;?>" readonly="true" /></td></tr> <tr> <td>Date of First Registration:</td><td><input name="date_reg" type="text" value="<?php echo $xml->result[0]->first_registered;?>" readonly="true" /></td></tr> <tr> <td>Colour:</td><td><input name="colour" type="text" value="<?php echo $xml->result[0]->colour;?>" readonly="true" /></td></tr> <tr> <td>Body Style:</td><td><input name="body" type="text" value="<?php echo $xml->result[0]->body;?>" readonly="true" /></td></tr> <tr> <td>Number of Doors:</td><td><input name="doors" type="text" value="<?php echo $xml->result[0]->doors;?>" readonly="true" /></td></tr> <tr> <td>Engine Size:</td><td><input name="engine_size" type="text" value="<?php echo $xml->result[0]->engine_size;?>" readonly="true" /></td></tr> <tr> <td>Fuel Type:</td><td><input name="fuel_type" type="text" value="<?php echo $xml->result[0]->fuel;?>" readonly="true" /></td></tr> <tr> <td>Gearbox Type:</td><td><input name="gearbox" type="text" value="<?php echo $xml->result[0]->gearbox_type;?>" readonly="true" /></td></tr> <tr> <td>Previous Keepers:</td><td><input name="keepers" type="text" value="<?php echo $xml->result[0]->previous_keepers;?>" readonly="true" /></td></tr> <tr> <td>BHP:</td><td><input name="bhp" type="text" value="<?php echo $xml->result[0]->smmt_power_bhp;?>" readonly="true" /></td></tr> <tr> <td>Emissions:</td><td><input name="co2" type="text" value="<?php echo $xml->result[0]->smmt_co2;?>" readonly="true" /></td></tr> <tr> <td><input name="try_again" type="button" value="Try Again" /></td><td><input name="submit" type="submit" value="Next Stage" /></td></tr> </table> </form> What have I done wrong? I want to save class property values and want to write these in the database after being called from another function. Code: [Select] class model{ public $dbh; public $user_arr=array('username','password','email','location','interest','homepage'); public function insert_data() { $sql = "INSERT INTO account VALUES ('" .$user_info['username']."','".$user_info['password'] ."','".$user_info['email']."', 'user','".$user_info['location']."','".$user_info['interests'] ."','".$user_info['homepage']."')"; //if(isset($data)) echo $sql; } public function data_approoved($user){ //$user_info = new array(); $user_arr['username'] = $user['username']; $user_arr['password'] = $user['password']; $user_arr['email'] = $user['email']; $user_arr['location'] = $user['location']; $user_info['interests'] = $user['interests']; $user_info['homepage'] = $user['homepage']; Model::insert_data(); } } }//class ended The approoved_data function is supposed to set the values of $user_info array which should be later on stored in database. Please somebody help me. Hello, I am sorry for this question in advance. I will try to explain it as detailed but, as short as possible so bare with me. I have a script that I did that parses an XML file and outputs the info in a table. The tables are created with each entry in XML file. Everything works great and as designed except for when there is no data. I know why it is doing this, I am just not sure how to approach fixing this. So here is what the data looks like where there is nothing and what the table shows. I am posting a screenshot of the XML file as I am not sure it will be empty if I post the link.
*Mods* PLEASE don't move this to the OOP section again. I've had no luck there, and it's not specifically an OOP question. Hi everyone, I just want to know if there's a shorthand way to create objects, as there is for arrays: Code: [Select] // shorthand array creation $foo = array( 'bar' => 0, 'baz' => array ( 'name' => 'Dave', 'gender' => 'male' ) ) Code: [Select] // is there a way to do this with objects? // not that I know so far... $foo = object( bar->0, 'baz' -> object ( name -> 'Dave', gender -> 'male' ) ) I find object-access syntax is often quicker and easier to write than array-access syntax. Have I missed something obvious that I can't just write it out this way? Cheers, Dave I am trying to sort these fields alphabetically by $name - $documents have run past multiple sort methods but none seem to work. I was hoping someone here knew an easy method to do this xml data: <database> <name>Test</name> <documents>0</documents> <sections>0</sections> </database> <database> <name>Test2</name> <documents>100</documents> <sections>0</sections> </database> current code: <?php $request_url = "http://10.100.100.100:9000/a=getstatus"; $xml = simplexml_load_file($request_url) or die("feed not loading"); echo "<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n"; echo " <tr>\n"; echo " <td>Thor Databases</td>\n"; echo " <td>Count\n"; echo $xml->responsedata->databases->num_databases; echo " </td>\n"; echo " </tr>\n"; foreach ($xml->responsedata->databases->database as $db) { echo"<tr><td>",$db->name,"</td><td>",$db->documents,"</td></tr>\n"; } echo "</table>\n"; ?> Thanks, E I have a Soup object in a Bowl object in a Microwave object. The Microwave object has the methods: receiveItem(Bowl $b) cookItem(???) I want the Microwave to be able to set the Soup->temperature but I'm not how to do that? Make sense? TomTees This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=323395.0 I get the following error when I try to pass a value to a methiod in a loop: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 This one has me very baffled. It will work the first time, and seems to work every other time, so I have no clue what is wrong. Here is the code: This code is part of my "display" class: function display_partner ($type,$loc,$rand=0,$narrow=0) { $this->partners = new partner($this->cxn); $display = ' <div id="cont_info" class="partner-list"> <div> <h3 class="settings">'.ucfirst($loc).' '.ucfirst($type).last_letter($type).'s</h3> </div> <div class="settings-value" style="height:12px;padding:0;margin:0;text-align:right;padding-right:10px;"> <a href="" class="trunc">Add your organization to this list</a></p> </div> <div style="height:2px;padding:0;margin:0;"> <hr class="account" /> </div> '; $ids = $this->partners->get_partners_list($type,$loc,$rand); for ($b=0;$b<sizeof($ids->id);$b++) { $this->partnerID = $ids->id[$b]; $display .= ($narrow)? $this->card_partner_narr():$this->card_partner(); if ($b!=(sizeof($ids->id)-1)) { $display .= '<hr class="account" />'; } } if (sizeof($ids->id)==0) { $display .= '<div style="color:#999999;display:line;text-align:center;height:20px;">No Partners found for '.ucfirst($loc).' '.ucfirst($type).'</div>'; } $display .= ' </div>'; return $display; } function card_partner () { $this->partners->set_partner_id($this->partnerID); $part_info = $this->partners->get_partner_info(); if ($part_info) { $display .= ' <table class="settings"> <tr> '.$this->show_if($part_info['partLogo']['val'],'<td class="settings-value" rowspan="2"><img src="'.LOGO_FOLDER.$part_info['partLogo']['val'].'" '.resize_img(LOGO_FOLDER.$part_info['partLogo']['val'],175).'alt="'.$part_info['partName']['val'].'" /></td>').' <td class="settings-value" colspan="2"><h5>'.$part_info['partName']['val'].'</h5></td> </tr> <tr> <td class="settings-value"> <span style="color:999999;">'.$part_info['partAddress']['val'].'<br /> '.$part_info['partCity']['val'].', '.$part_info['partST']['val'].' '.$part_info['partZIP']['val'].'<br /> '.$part_info['partPhone']['val'].'</span><br /> <a href="'.$this->form->show_href($part_info['partWeb']['val']).'" target="_blank">'.$part_info['partWeb']['val'].'</a> </td> <td class="settings-value">'.$part_info['partInfo']['val'].'</td> </tr> </table> '; } return $display; } This code is part of my "partners" class: function set_partner_id($partID) { echo '<p>partID: '.$partID.' '.gettype($partID).'<br> $this->partner->id: '.$this->partner->id.'</p>'; $this->partner->id = $partID; ///*** ERROR HAPPENS HERE ***/ echo '<p>id set: '.$this->partner->id.'<br> $this->partner->id: '.$this->partner->id.'</p><hr>'; } function get_partner_id() { return $this->partner->id; } // gets user info at login function get_partner_info() { $this->partner = $this->cxn->proc_info('partner','partID',$this->partner->id);//$this->partner->id return $this->partner; } The following is the output generated: partID: 24 string $this->partner->id: id set: 24 $this->partner->id: 24 partID: 26 string $this->partner->id: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 id set: $this->partner->id: partID: 17 string $this->partner->id: id set: 17 $this->partner->id: 17 partID: 25 string $this->partner->id: Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670 id set: $this->partner->id: As you can see, the value passes to $this->set_partner_id($partID) each time. It is formatted as a string. When it assigns the value to $this->partner->id, however, sometimes it works, and sometimes it doesn't. It's probably something obvious, but I've racked my brain to see what it is. Any ideas? I have instantiated a object and the constructor has an other object added the issue arises when I extent the first object how do I access the object in the variable. below is a stripped down example. I want accessfoo to access class a method foo that is stored in a var in class b is this possible. Code: [Select] class a { private function foo(){ return "hello word"; } } $a = new a() class b { private $obj; public function __construct ($obj){ $this->obj= $obj; } } class c extends b{ private function accessfoo(){ $this->obj ????????????? } $c = new class($a) I can call the following function successfully as a single php program // Acknowledge and clear the orders function ack($client, $merchant, $id) { $docs = array('string' => $id); $params = array('merchant' => $merchant, 'documentIdentifierArray' => $docs); $result = $client->call('postDocumentDownloadAck', $params); return $result; } with $result = ack($t, $merchant,'2779540483'); successful output [documentDownloadAckProcessingStatus] => _SUCCESSFUL_ [documentID] => 2779540483 I'm trying to figure out how to call this function as an object from another program. Trying the following gives error ***Call to a member function call() on a non-object*** function postDocumentDownloadAck($t, $merchant, $id) { $this->error = null; $docs = array('string' => $this->id); $params = array('merchant' => $this->merchant, 'documentIdentifierArray' => $docs); ** I've tried the following which does nothing $result = $this->soap->call('postDocumentDownloadAck', $params); ** I've tried the following - which gives error "Call to a member function call() on a non-object" $result = $this->t->soap->call('postDocumentDownloadAck', $params); if($this->soap->fault) { $this->error = $result; return false; } return $result; } *** calling program snippet for above function $merchant= array( "merchant"=> $merchantid, "merchantName" => $merchantname, "email"=> $login, "password"=> $password); $t = new AmazonMerchantAPI($merchantid, $merchantname, $login, $password); $documentlist= $t->GetAllPendingDocumentInfo('_GET_ORDERS_DATA_'); $docid = $documentlist['MerchantDocumentInfo'][$i]['documentID']; $docs = array('string' => $docid); $ackorders = $t->postDocumentDownloadAck($t, $merchant,$docs); Any ideas of what I'm doing wrong are greatly appreciated. Ok so I have a url that is an XML doc, it says at the top "This XML file does not appear to have any style information associated with it. The document tree is shown below." and it has info like this Code: [Select] <outer> <xml> <viewer> <user> <id>1245789</id> <name>CLueless</user> <business>none</business> </user> </viewer> </xml> </outer> How would I load the link and then assign everything to a variable like id, name and business I usually use something like this but Im trying to get learn simple xml $info = file_get_contents('url'); $id = explode('<id>', $info); $id = explode('</', $id[1]); $name = explode('<name>', $info); $name = explode('</', $name[1]); $biz = explode('<business>', $info); $biz = explode('</', $biz); |