transform XML nodeValue to PHP/HTML string - php

I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>

The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.

Related

Modification of Ajax Live Search to include a new field

https://www.w3schools.com/php/php_ajax_livesearch.asp
I am trying to modify the code in the tutorial for an ajax live search, to include a new field called keyword. I want the search to also search the keyword field in order to improve user experience.
I have already modified the link.xml fine to include the newly added keyword tag.
<link>
<title> title </title>
<keyword> keywords here </keyword>
<url>https://path to url</url>
</link>
I am kind of stuck in the area below, trying to modify the conditional statement.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$k=$x->item($i)->getElementsByTagName('keyword'); // New field added
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
I think you can change the condition
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
to
$checkTitle = stristr($y->item(0)->childNodes->item(0)->nodeValue,$q);
$checkKeyword = stristr($k->item(0)->childNodes->item(0)->nodeValue,$q);
if ($checkTitle || $checkKeyword) {
//
}

Search in XML with PHP

trying to add a Ajax search function to a xml file, and found this exemple :
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("my_xml_file.xml");
$x=$xmlDoc->getElementsByTagName('links');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
This works perfect but the xml I have to search is a bit different and I don't know how to setup the function according to my needs.
This is how is formatted my xml :
<data name="my_name"><![CDATA[Data content]]></data>
I changed
`$x=$xmlDoc->getElementsByTagName('links'); to $x=$xmlDoc->getElementsByTagName('data');`
But how to change
$y=$x->item($i)->getElementsByTagName('title');
to have
$y=$x->item($i)-> name; and $z=$x->item($i)->Data content without CDATA ?;
Thanx a lot for your help !!!
add this in your loop:
$attr = $a->item($i)->getAttribute('name');
if($attr==""){
continue;
}
//take care of the $attr
echo $attr;

XML node Value format [duplicate]

I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>
The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.

How do i stop my search bar from opening a new tab?

How do i stop my search bar from opening a new tab? I dont see whats making it do that? Heres my php code. I've been having a couple issues with this search bar recently...
Heres my code (It is very very long...):
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
else
{
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "No suggestions" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="No suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
you need to remove remove target='_blank'
Replace this
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
by this
$z->item(0)->childNodes->item(0)->nodeValue .
"'>" .
tag target="_blank" make opening link in new window

About the evil of eval: How to clean up

I'm building a site that will (eventually) be the front-end for a game. I want to be able to dynamically build a list of "powers" that the user is able to purchase. In order to build these lists, I'm using a PHP-based SQL query, then passing it to Javascript for dynamic choices (some powers have prerequisite powers).
I know there's a simpler way to do what I'm doing, but I'm not super concerned with that right now (I will be later, but I'm trying to get this functional and then clean) (again, I know this is non-optimal).
I'm using eval to parse which divs to show, and I want to know how not to.
I'm having issues getting my div names built right in the first place.
Here's my code:
Javascript (separate file)
function upgradeList(passed)
{
var PowerArray = [];
var DetailPowerID = [];
var EscapedPowerID = [];
PowerArray.push([]);
PowerArray = eval(passed);
var OutputThing="";
for (i=0;i<PowerArray.length;i++)
{
DetailPowerID[i] = 'detail' + PowerArray[i][0];
EscapedPowerID[i] = "'" + DetailPowerID[i] + "'";
}
for (i=0;i<PowerArray.length;i++)
{
OutputThing = OutputThing + "<br><a href='#' onClick='showUpgradeDetails(" + DetailPowerID[i] + ")'>" + PowerArray[i][2] + "</a><div class='hidden' id='" +
DetailPowerID[i] + "'>" + PowerArray[i][3] + "</div>"; }
document.getElementById("secondUpgrade").innerHTML=OutputThing;
document.getElementById("secondUpgrade").style.display='block';
}
}
PHP writing HTML and JS:
{$AbleToUpgrade and $UpgradeList are both 2d arrays built from SQL queries)
echo "<script name='UpgradeList'>";
settype($UpgradesListSize[$i],"int");
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "var UpgradeList" . $AbleToUpgrade[$i][0] . " = new Array();";
for ($j=0;$j<=$UpgradesListSize[$i];$j++)
{
echo "UpgradeList" . $AbleToUpgrade[$i][0] . ".push(Array('"
. $UpgradeList[$i][$j][0] . "', '"
. $UpgradeList[$i][$j][1] . "', '"
. $UpgradeList[$i][$j][2] . "', '"
. $UpgradeList[$i][$j][3] . "', '"
. $UpgradeList[$i][$j][4] . "'));";
}
}
echo "</script>";
... and, later...
echo "<div id='SpendUpgrade'>
Select power to upgrade:
<ul>";
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "<li><a href='#' name='UpgradeList" . $AbleToUpgrade[$i][0] . "' onClick='upgradeList(this.name)'>" . $AbleToUpgrade[$i][1] . " - " . $AbleToUpgrade[$i][2] . "</a></li>";
}
echo "</select>
<div id='secondUpgrade' class='hidden'>
</div>
<div id='thirdUpgrade' class='hidden'>
</div>
</div>";
When I load the page, I wind up with generated text like this:
Real Armor
and the corresponding div:
<div class="hidden" id="detail21" style="display: none;">Your armor only works in the Waking</div>
In order to get the div to show (display:block;), I need to call the function like so:
showUpgradeDetails("detail21")
but I can't make JS / PHP write the quotes correctly. Help (with any or all of this?) please!
I found a resolution, and it wasn't JSON.parse().
I changed PowerArray = eval(passed); into PowerArray = window[passed];.
Because passed contains the name of a variable, and is not the variable itself, I couldn't work directly with it. However, because it was a string that held exclusively the name of a globally-defined variable, I could pass it to the window[] construct and have it work.

Categories