Jump to content

Deriving a key for loading xml via domxpath and preg_match_all?


alco19357

Recommended Posts

Hello,

I am making a script that uses various preg_match_all and I want to derive users/user from: "more code here key=users/user more code here"

 

Once I get the key name, I want to use it in xpath/dom with php to do a query, such that:

 

$childs = $xpath->query($_wherePre . "[" . $whereClause . "]");

 

$_wherePre would be the derived key name, with some added trim functions, etc. and $whereClause represents attribute where clauses given in a loop (this part works fine)

 

It's the $_wherePre I can't get to work. I've done debugging and when I enter: $_wherePre = "users/user"; into my php script, it works fine... but when I enter $_wherePre = trim($derivedKey); (which when I enter in debugging: echo trim($derivedKey); I get users/user).  I get a bunch of coding errors stating that the file could not be loaded. I tried messing with the string types: object, string. Is there anything that I can do that'll make this derived key instantiate the xml file connection?

 

PLEASE HELP!!!!!!!!!

 

THank you sooo much!

Alex

Link to comment
Share on other sites

The class file

function search($query=''){
	preg_match_all("|\([^(]+\)|U", $query, $keys, PREG_PATTERN_ORDER);
	$retrieve_r = substr(trim($keys[0][0]), 1, (strlen(trim($keys[0][0]))-2));
	$where_r = substr(trim($keys[0][1]), 1, (strlen(trim($keys[0][1]))-2));

	$rePlacePattern = array("|\([^(]+\)|U", "/select/", "/from/", "/where/");
	$fillPattern = array("", "", "", "");
	$whatsLeft = trim(preg_replace($rePlacePattern, $fillPattern, $query));
	preg_match_all("/(.*)(key=)(.*)(primary=)(.*)/i", $whatsLeft, $leftOver, PREG_SET_ORDER);
	$xmlfile = trim($leftOver[0][1]);
	$intoKey = trim($leftOver[0][3]);
	$primaryKey = trim($leftOver[0][5]);

	$whatsLeft2 = preg_replace($rePlacePattern, $fillPattern, $query);
	preg_match_all("/(.*)(key=)(.*)(primary=)(.*)/U", $whatsLeft2, $explode, PREG_SET_ORDER);

	$_SESSION['trysession'] = trim($explode[0][3]);
	$trysession = $_SESSION['trysession'];

	$_wherePre = (string)(urldecode($intokey));
	$_whereClause_r = "[@user_id='1' or @sort_id='2']";
	$_whereClause = substr(trim($_whereClause_r), 1, (strlen(trim($_whereClause_r))-2)); // removes the front and back brackets



	$stripFirst = trim(preg_replace('/(^and |^or )/i', '', trim($_whereClause)));
	$stripLast = trim(preg_replace('/( and$| or$)/i', '', $stripFirst));
	preg_match_all('/( or | and )/i', $stripLast, $whereStatements, NULL);
	$getContents = preg_split('/( or | and )/i', $stripLast);

	//settype($_wherePre, 'string');

	$_construct_where = array();
	for($i=0; $i<count($getContents); $i++){
		$identifier = trim($getContents[$i]);
		$value = trim($whereStatements[0][$i-1]);
		$totalInArray = count($_construct_where);

		if($value == 'and' && $i > 0){
			$_construct_where[$totalInArray-1] = $_construct_where[$totalInArray-1].' '.$value.' '.$identifier;
			$i = $i;
		}elseif($value == 'or'){
			$_construct_where[$totalInArray] = $identifier;
		}else{
			$_construct_where[$totalInArray] = $identifier;
		}
	}

	$dom = new DOMDocument();
	$dom->load($this->include_dir . '/' . $this->xml_dir . '/' . 'users' . '/' . 'users' . '.xml');
	$xpath = new domxpath($dom);

	$return = array();

	foreach($_construct_where as $whereClause){
		var_dump($_wherePre);
		$childs = $xpath->query($_wherePre . "[" . $whereClause . "]");
		foreach($childs as $child){
			$return_attr = array();
			//if((count($getAttr) == 1) && ($getAttr[0] == '*')){
			if(1==1){
				$selectall = $xpath->query($_wherePre . "[" . $whereClause . "]" . "/basic_info/@*");
				foreach($selectall as $all){
					$return_attr[] = $all->textContent;
				}
			}else{
				foreach($getAttr as $attr){
					$attribute = $xpath->query("/users/user[@user_id='1' and @sort_id='2']/basic_info/@*");
					foreach($attribute as $a){
						$return_attr[$attr] = $a->textContent;
					}
				}
			}
			//$getPrimaryKey = $xpath->query($primary_key);
			//foreach($getPrimaryKey as $primaryKey){
				//$return[$primaryKey->textContent] = $return_attr;
				$return[] = $return_attr;
			//}
		}
	}

//		echo gettype($_wherePre);

}

 

the calling file

$str = "select (as) from clothing/mens key=users/user primary=[@user_id] where (@item_id='322' or @item_id='343')";
print_r( $xml->search($str) );

 

the response

string(0) ""

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>109</b><br />

string(0) ""

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>109</b><br />

string(0) ""

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>109</b><br />

string(0) ""

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  DOMXPath::query() [<a href='domxpath.query'>domxpath.query</a>]: Invalid expression in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>108</b><br />

<br />

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\wamp\www\contractor scoop\www\admin\include\classes\performxml.php</b> on line <b>109</b><br />

Link to comment
Share on other sites

Well that var_dump tells you that $_wherePre is an empty string. I'm not exactly sure if you know why or not. I looked through that large code and it's far too confusing what it's doing with all the trim, substr, etc. :( Just too much to process. :D Mind cleaning it up or post the pertinent code?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.