Page 2 of 2
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:31 pm
by mecha_godzilla
You can still use htmlspecialchars() if you need to convert the data, but whether you should do this or not depends on how you're going to use it in your script:
Code: Select all
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]first-name[\>](.*)[\<]\/first-name[\>]/s", "$romancartxml", $firstname);
$count = preg_match("/[\<]postcode[\>](.*)[\<]\/postcode[\>]/s", "$romancartxml", $postcode);
$firstname = htmlspecialchars($firstname[0]);
$postcode = htmlspecialchars($postcode[0]);
echo "$firstname $postcode";
HTH,
M_G
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:36 pm
by simonmlewis
Thanks.
Oddly, without htmlspecialchars, I was able to output the variable as the word inside those 'tags/headers'.
With two extracted, I'll be able to update our database much more efficiently. Great job.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Wed Mar 06, 2013 4:26 am
by simonmlewis
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];
echo "Processing $itemcode, $primarystockremaining";
include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
$num_result = mysql_num_rows($result);
echo "$num_result for $itemcode";
while ($row = mysql_fetch_object($result))
{
} mysql_free_result($result);
mysql_close($sqlconn);
echo "<br/>$itemcode";
?>
Apologies for the extra code at the bottom, but this is odd. $itemcode echos perfectly. And if I force $itemcode = "070313-19";, then this query pulls up a result of 1. Which is correct.
But if I just query it like this, it pulls up nothing.
I know there is a way to kind of "see" the query, to know if it is putting the itemcode into the query correctly, but don't know how. It's really bizarre. I thought this would be the easy bit!
Re: How do you extract fred from "<company>fred</company>"?
Posted: Wed Mar 06, 2013 5:24 am
by simonmlewis
Update: If I post the itemcode via a normal non-xml type way, it works.
But if I do it the way I am here, it won't. Here is the current code that still doesn't work.
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];
echo "<br/>$itemcode";
include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
echo "$row->players";
} mysql_free_result($result);
mysql_close($sqlconn);
?>
echo "<br/>$itemcode";...... this does echo the itemcode.
But $row->players produces nothing, yet it should. What the heck is happening?
Re: How do you extract fred from "<company>fred</company>"?
Posted: Wed Mar 06, 2013 5:47 am
by simonmlewis
I am getting somewhere with this now, but still stumped:
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]primarystockremaining[\>](.*)[\<]\/primarystockremaining[\>]/s", "$romancartxml", $primarystockremaining);
$count2 = preg_match("/[\<]item-code[\>](.*)[\<]\/item-code[\>]/s", "$romancartxml", $itemcode);
$primarystockremaining = $primarystockremaining[0];
$itemcode = $itemcode[0];
echo "<br/>$itemcode, $primarystockremaining
<form method='post' action='#'>
<input type='text' name='test' value='$itemcode'>
<input type='submit'>
</form>";
include "dbconn.php";
$result = mysql_query ("SELECT id, romancode, players FROM events WHERE romancode = '$itemcode'") or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
echo "$row->players";
} mysql_free_result($result);
mysql_close($sqlconn);
?>
This echos the attached screenshot.
So it's not taking the content out, and storing it in a variable. How in HTML do I keep that contents as the item-code, without the outter parts??
Re: How do you extract fred from "<company>fred</company>"?
Posted: Wed Mar 06, 2013 1:29 pm
by Christopher
preg_match is returning what it matched. That includes the <item-code> and </item-code>. You need to use substr() to remove the tags.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Wed Mar 06, 2013 2:21 pm
by requinix
Or use $primarystockremaining[1] and $itemcode[1].