We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

PHP conditional in XML db query output

Medium Priority
704 Views
Last Modified: 2012-05-06
I have a php script which is outputting the correct data.I wont lead off with the whole query...

I believe someone can help me code this:

CURRENT:
print("<cell><![CDATA[".$row['loc']."]]></cell>");
...which returns and XML "CELL" element with the sql query result populating the cell data.

WHAT I NEED TO DO:

I need this line to be a conditional...

WHAT I'VE TRIED:
IF ($row['loc'] = "Shipping" print("<cell><![CDATA["."<div class='redbg'>".$row['loc']."</div>"."]]></cell>"));
ELSE
(print("<cell><![CDATA["."<div class='bluebg'>".$row['loc']."</div>"."]]></cell>"));
<<<<<I've tried several versions of this...XML debugging is very lacking>>>>>>
The div works fine without the conditional...it only breaks when the IF and ELSE

THANKS!

Rapturer

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Use double equal signs for comparison!
Most Valuable Expert 2011
Author of the Year 2014

Commented:

if ($row['loc'] == "Shipping")
{
   print("<cell><![CDATA["."<div class='redbg'>".$row['loc']."</div>"."]]></cell>")
}
else
{
   print("<cell><![CDATA["."<div class='bluebg'>".$row['loc']."</div>"."]]></cell>");
}

Open in new window

Author

Commented:
I actually did have the correct operator.. "==" just did not show it in the code.
 
I did try your code Ray and got a the following:
PHP Parse error:  syntax error, unexpected '}' in /home/control/public_html/APTS_BETA/clave_view1_GET.php on line 60
Line 60 is the single closing bracket " } "
Thanks! - Rapturer
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I only posted 8 lines, but the error cited line 60.  In order to understand the error, I would need to see what's in the actual code that generated the error message.  Squirt that into the code snippet and I will take a look.

Author

Commented:
Here you are Ray. As you can see the code is a real wreck right now with line edits and such, but you will get the drift.
This is the GET file for  DHTMLX's Dataprocessor. Dhtmlx grid has an amazing JAX library.
Best,
Rapturer  

<?php
/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
This version of Software is free for using in non-commercial applications.
For commercial use please contact sales@dhtmlx.com to obtain license
*/
 
//include db connection settings
require_once("modules/config.php");
 
$link = mysql_pconnect($mysql_host, $mysql_user, $mysql_pasw);
$db = mysql_select_db ($mysql_db);
 
 
//start session to build different trees for different sessions (if you set $_SESSION["id"] to some hardcoded value, this way of processing will be skipped)
/*
session_start();
//$_SESSION = array();
if(!isset($_SESSION["id"])){
	$_SESSION["id"] = microtime();
	//add some rows to the table if user comes first time
	$sql = 	"Insert into samples_grid(sales,title,author,price,instore,shipping,bestseller,pub_date,GUID) ";
	$sql.= 	"Values(100,'Your Favotite Book','Bill Starter',23,'1','12','1',null,'".$_SESSION["id"]."')";
	$res = mysql_query ($sql);
	$sql= "Insert into samples_grid(sales,title,author,price,instore,shipping,bestseller,pub_date,GUID) ";
	$sql.= 	"Values(-50,'Good Start','Phillip Nomore',12.5,'0','12','0',null,'".$_SESSION["id"]."');";
	$res = mysql_query ($sql);
}
*/
 
//FUNCTIONS TO USE IN THE CODE LATER
 
//print tree XML based on parent_id (function calls itself to go through the nested levels)
	function getRowsFromDB($parent_id){
		//get tree level from database taking parent id as incomming argument
		$sql = "SELECT DISTINCT products.partnumber AS \"Part Number\", invoices.id, invoices.loc, invoices.stagestatus, invoices.day, invoices.month, invoices.year, invoices.tracking as \"Shipping\", invoices.productionnotes as \"Notes\", products.claveid AS \"CR1\", products.claveid2 \"CR2\", lineitems.id AS \"LID\", lineitems.width, invoices.requireddate as \"Required Date\", invoices.ponumber as \"PO Number\", invoices.scheduledtorun, lineitems.height, SUM(lineitems.width * lineitems.height) /144 * ( lineitems.quantity ) AS \" Total SqFt\", lineitems.quantity,  SUM(lineitems.quantity) as quantity, clients.company FROM clients INNER JOIN invoices ON ( clients.id = invoices.clientid ) , lineitems INNER JOIN products ON ( lineitems.productid = products.id ) WHERE (lineitems.invoiceid = invoices.id) AND invoices.stagestatus=\"Production\" GROUP by lineitems.invoiceid ORDER by invoices.loc";
 
 
 
 
		//echo $sql;
		$res = mysql_query ($sql);
		if($res){
			while($row=mysql_fetch_array($res)){
				//create xml tag for grid row
				print("<row id=' ".$row['id']." '>");
					print("<cell><![CDATA["."<a href='workorders_addedit.php?id=".$row['id']."'target="."'_blank' >SG".$row['id']."</a>"."]]></cell>");
				//	print("<cell><![CDATA["."<b>".$row['Required Date']."<b>"."]]></cell>");
				//	print("<cell><![CDATA[".$row['company']."]]></cell>");
				//	print("<cell><![CDATA[".$row['Part Number']."]]></cell>");
				//	print("<cell><![CDATA[".$row['PO Number']."]]></cell>");
					print("<cell><![CDATA["."<b>".ceil ($row['Total SqFt'])."</b>"."]]></cell>");
					print("<cell><![CDATA["."<b>".$row['quantity']."<b>"."]]></cell>");
						print("<cell><![CDATA[".$row['CR1']."]]></cell>");
						print("<cell><![CDATA[".$row['CR2']."]]></cell>");
				//			print("<cell><![CDATA[".$row['Notes']."]]></cell>");
				//			print("<cell><![CDATA[".$row['stagestatus']."]]></cell>");
				//	print("<cell><![CDATA["."<div class='redbg'>".$row['loc']."</div>"."]]></cell>");
					if ($row['loc'] == "Inspection"){
   print("<cell><![CDATA["."<div class='redbg'>".$row['loc']."</div>"."]]></cell>")}else{
   print("<cell><![CDATA["."<div class='bluebg'>".$row['loc']."</div>"."]]></cell>")
};
 
							
						//	print("<cell><![CDATA[".if ($row['loc']!=0 echo('<div class=`redbg`>'.$row[`loc`])."</div>".else echo $row['loc'];."]]></cell>");
				//	print("<cell><![CDATA[".$row['scheduledtorun']."]]></cell>");
			//		print("<cell><![CDATA[".gmdate("m/d/Y",strtotime($row['pub_date']))."]]></cell>");
				//close xml tag for the row
				print("</row>");
				
			}
		}else{
			echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
		}
	}
 
//XML HEADER
 
//include XML Header (as response will be in xml format)
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
 		header("Content-type: application/xhtml+xml"); } else {
 		header("Content-type: text/xml");
}
echo("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"); 
?>
 
<!-- start grid xml -->
<rows id="0">
	
<?php
	//print tree XML
	getRowsFromDB(0);
	//Close db connection
	mysql_close($link);
?>
 
<!-- close grid xml -->
</rows>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Those single-line, multiple statement code constructs are always a pain to debug.  Try changing that sort of thing to something more atomic, like this.  Then we can isolate the errors more readily.

Feel free to correct typos and parse errors! ~Ray
if ($row['loc'] == "Shipping")
{
   echo "<cell>\n";
   echo "<![CDATA[";
   echo "<div class='redbg'>";
   echo "{$row["loc"]}";
   echo "</div>\n";
   echo "]]>\n";
   echo "</cell>";
}
else
{
   echo "<cell>\n";
   echo "<![CDATA[";
   echo "<div class='bluebg'>";
   echo "{$row["loc"]}";
   echo "</div>\n";
   echo "]]>\n";
   echo "</cell>";
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank you Ray ... Worked Perfectly

Rapturer
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Cool!  Thanks for the points, ~Ray
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.