MrDigital0 Posted October 6, 2012 Share Posted October 6, 2012 Hello. I have this XML Code here <response status="ok"> <serviceRequestList> <serviceRequest> <accountManagerId></accountManagerId> <billable></billable> <billableTotal></billableTotal> <billingStatus></billingStatus> <customerContactEmail></customerContactEmail> <customerContactId></customerContactId> <customerContactName></customerContactName> <customerContactPhone></customerContactPhone> <customerContactPhoneMobile></customerContactPhoneMobile> <customerId></customerId> <customerLocationCity></customerLocationCity> <customerLocationCountry></customerLocationCountry> <customerLocationId></customerLocationId> <customerLocationName></customerLocationName> <customerLocationNotes></customerLocationNotes> <customerLocationPostalCode></customerLocationPostalCode> <customerLocationState></customerLocationState> <customerLocationStreetAddress></customerLocationStreetAddress> <customerLocationZone></customerLocationZone> <customerName></customerName> <dateTimeCreated></dateTimeCreated> <dateTimeClosed></dateTimeClosed> <description></description> <detailedDescription></detailedDescription> <dueDate></dueDate> <externalId></externalId> <priority></priority> <priorityLabel></priorityLabel> <serviceManagerId></serviceManagerId> <serviceRequestId></serviceRequestId> <status></status> <timeOpen_hours></timeOpen_hours> <type></type> </serviceRequest> <serviceRequest> ... </serviceRequest> </serviceRequestList> </response> It is from an API URL I can call the XML From PHP using SimpleXML but i need a way to code the PHP so in the template file for the page it will create table row for each ServiceRequestID with the information from <serviceRequestId>, <description>, <status> <serviceManagerId> & <datetimecreated> and maybe if possible if <status> is closed instead of <datetimecreated> it shows <datetimeclosed> I am Able to get it into an array but the php code creates the array like this Array ( [id] => <serviceRequestId>1001</serviceRequestId> [description] => <description>Computer will not turn on</description> [status] => <status>Closed</status> [asssigned] => <serviceManagerId>0</serviceManagerId> [createdate] => <dateTimeCreated>2012-09-14T18:42:40</dateTimeCreated> [closedate] => <dateTimeClosed>2012-09-14T18:49:58.347</dateTimeClosed> ) Array ( [id] => <serviceRequestId>1005</serviceRequestId> [description] => <description>this is a test</description> [status] => <status>New</status> [asssigned] => <serviceManagerId>33443928</serviceManagerId> [createdate] => <dateTimeCreated>2012-10-06T05:57:08</dateTimeCreated> [closedate] => ) This is the output im getting when the smarty template is parsed 1005 this is a test New 2012-10-06T05:57:08 1005 this is a test New 2012-10-06T05:57:08 1005 this is a test New 2012-10-06T05:57:08 1005 this is a test New 2012-10-06T05:57:08 1005 this is a test New 2012-10-06T05:57:08 1005 this is a test New 2012-10-06T05:57:08 Instead of 1001 Computer will not turn on Closed 2012-09-14T18:42:40 1005 this is a test New 2012-10-06T05:57:08 the PHP File is <?php define("CLIENTAREA",true); //define("FORCESSL",true); # Uncomment to force the page to use https:// require("dbconnect.php"); require("includes/functions.php"); require("includes/clientareafunctions.php"); $pagetitle = $_LANG['clientareatitle']; $breadcrumbnav = '<a href="index.php">'.$_LANG['globalsystemname'].'</a>'; $breadcrumbnav .= ' > <a href="mypage.php">My Page</a>'; initialiseClientArea($pagetitle,'',$breadcrumbnav); # To assign variables to the template system use the following syntax. # These can then be referenced using {$variablename} in the template. $smartyvalues["variablename"] = $value; # Check login status if ($_SESSION['uid']) { # User is logged in - put any code you like here # Retrieve User ID $result = mysql_query("SELECT companyname FROM tblclients WHERE id=".(int)$_SESSION['uid']); $data = mysql_fetch_array($result); $companyname = $data[0]; $credentials = "xxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Read the XML to send to the Web Service $xml_data = "<request> <serviceRequestList> <listType>basic</listType> <customerName>$companyname</customerName> </serviceRequestList> </request>"; $url = "https://app.packettrappsa.com/api/2.0/serviceRequests/list.aspx"; $page = "api/2.0/serviceRequests/list.aspx"; $headers = array( "POST ".$page." HTTP/1.0", "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"run\"", "Content-length: ".strlen($xml_data), "Authorization: Basic " . base64_encode($credentials) ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']); // Apply the XML to our curl call curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); $data = curl_exec($ch); if (curl_errno($ch)) { print "Error: " . curl_error($ch); } else { // Show me the result curl_close($ch); // echo $data; $xml = simplexml_load_string($data); foreach($xml->serviceRequestList->serviceRequest as $service) { $srid = $service->serviceRequestId->asXML(); $srdesc = $service->description->asXML(); $srstatus = $service->status->asXML(); $srassignedto = $service->serviceManagerId->asXML(); $srcreatedate = $service->dateTimeCreated->asXML(); $srclosedate = $service->dateTimeClosed->asXML(); $result = array('id' => $srid, 'description' => $srdesc, 'status' => $srstatus, 'asssigned' => $srassignedto, 'createdate' => $srcreatedate, 'closedate' => $srclosedate); $smartyvalues["sr"] = $result; } } } else { # User is not logged in } # Define the template filename to be used without the .tpl extension $templatefile = "servicerequests"; outputClientArea($templatefile); ?> And The tpl file code is <a name="content"></a> <div id="contentWrapper"> <div id="contentArea"> <div class="post" id="post-71"> <div class="postHeader"> <h2 class="postTitle"><span></span>My Service Requests</h2> </div> <div class="postContent"> <table class="grid" cellspacing="0" rules="cols" border="1" id="dgSRList" style="width:100%;border-collapse:collapse;"> <tbody><tr> <th scope="col"> <font>No.</font> </th> <th scope="col"> <font>Description</font></th> <th scope="col"> <font>status</font></th> <th scope="col" style="white-space:nowrap;"> <font>Assigned To</font></th> <th scope="col"> <font>Created</font></th> </tr> {foreach from=$sr item=id} <tr> <td style="width:60px;white-space:nowrap;"> <a class="bold" href="servicerequest.php?id={$sr.id}">{$sr.id}</a> </td><td> <a class="bold" href="servicerequest.php?id={$sr.id}">{$sr.description}</a> </td><td> <span id="SRStatus_1000"></span>{$sr.status}</td><td>{$sr.assignedto} </td><td style="width:90px;white-space:nowrap;"> {$sr.createdate} </td> </tr> {/foreach} </tbody></table> </form> {include file="$template/sidebar.tpl"} Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/269165-need-help-with-php-code-xml-to-php-array-for-use-with-smarty/ Share on other sites More sharing options...
premiso Posted October 6, 2012 Share Posted October 6, 2012 It looks like your smarty loop is the culprit: {foreach from=$sr item=sri} <tr> <td style="width:60px;white-space:nowrap;"> <a class="bold" href="servicerequest.php?id={$sri.id}">{$sri.id}</a> </td><td> <a class="bold" href="servicerequest.php?id={$sri.id}">{$sri.description}</a> </td><td> <span id="SRStatus_1000"></span>{$sri.status}</td><td>{$sri.assignedto} </td><td style="width:90px;white-space:nowrap;"> {$sri.createdate} </td> </tr> {/foreach} Give that a try and see how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/269165-need-help-with-php-code-xml-to-php-array-for-use-with-smarty/#findComment-1383265 Share on other sites More sharing options...
premiso Posted October 6, 2012 Share Posted October 6, 2012 Another issue spotted is that this line: $smartyvalues["sr"] = $result; Should be: $smartyvalues["sr"][] = $result; Quote Link to comment https://forums.phpfreaks.com/topic/269165-need-help-with-php-code-xml-to-php-array-for-use-with-smarty/#findComment-1383279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.