0

I have 'simple' problem :-)

I'm reading large XML file using this:

$node = new SimpleXMLElement($reader->readOuterXML()); 

$node have elements severity, class... I can compare them like this:

if($node->severity==WARNING){ 

Or show them like this:

echo $node->severity; 

What I want to prepare if statements in advance reading posts variables like this:

if(isset($_POST['Severity']) && !EMPTY($_POST['Severity'])){ $Severity[]=$_POST['Severity']; foreach($Severity as $Severityval){ foreach($Severityval as $Severityvalues){ $searchquery .= '($node->Severity==' .$Severityvalues. ') || '; } } } 

Then I want to use $searchquery inside XML reader:

if($searchquery){ echo $node->severity; } 

But, this always return true and everything is shown :-(

My variable is properly created, if I echo it shows ($node->Severity==WARNING)

Maybe screen shot can explain my problem:

enter image description here

Thanks in advance

5
  • Can you please tell us what's going wrong ? Commented Nov 10, 2016 at 9:49
  • Just edited, If($searchquery) always return true Commented Nov 10, 2016 at 10:00
  • try to add a condition in your foreach to set only that variable if $node->severity==WARNING and add strlen(trim($searchquery)) > 0 or isset($searchquery) on your if statement ! Commented Nov 10, 2016 at 10:03
  • Both gives me true. As I said, I can echo $searchquery and it shows proper criteria. I think problem is with $node-> ...something like that Commented Nov 10, 2016 at 10:08
  • I think it is rather a design problem. You must have to test in your foreach statement and after test it ! Commented Nov 10, 2016 at 10:13

1 Answer 1

1

Try this :

if(isset($_POST['Severity']) && !EMPTY($_POST['Severity'])) { $Severity[] = $_POST['Severity']; $i = 0; foreach($Severity as $Severityval) { foreach($Severityval as $Severityvalues) { if ($node->Severity == $Severityvalues) { $i++; } } } if ($i > 0) { echo $node->severity; } } 

EDIT : I understand fully the problematic, if you had loaded one per one XML, you can do that (for each selected on node value - example work for severity, it's a good sample) :

$root_node = $your_var; if(isset($_POST['Severity']) && strlen(trim($_POST['Severity'])) > 0) { $severities_selected = array_values($_POST['Severity']); $severity = (string) $root_node->severity; if (in_array($severity, $severities_selected)) { echo "This XML contains one selected severity (".$severity.")"; } } 

Hope this helps !!

Sign up to request clarification or add additional context in comments.

11 Comments

I understand what you trying. Already tried something like that. But problem is that I have other POST variables to :-(. So my IF looks like this: if(($node->Severity==FATAL) || ($node->Severity==SEVERE) || ($node->Severity==WARNING)) && (($node->Class==Backup/Restore) || ($node->Class==Communication)){....
What is the final goal of this script ? To get a list of severities nodes ?
To filter xml file by different elements (severity, class, datetime...)
Mathieu thank you for your time. I just added screen shot of my form. Maybe there is some other way to establish this.. but have no idea
As I can see, you want to build SQL query form your template to your database ? Why XML objects ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.