Page 1 of 1
Stracting info from string_query
Posted: Tue Mar 25, 2003 8:59 pm
by Igguana
I have a string_query that brings out the following info when echoing:
?envio1=Alquiler?envio2=Puebla_de_Farnals?envio3=Local Comercial?envio4=4?envio5=24000?envio6=Costa
How can I extract the different variables?
envio1=Alquiler
envio2=Puebla_de_Farnals
etc...
Posted: Tue Mar 25, 2003 9:47 pm
by phpfreak
hi there ,
try this :
<?php
function stripplus($output)
{
return(str_replace("_"," ",$output));
//this is to remove the underscores inserted by the php in place
//of spaces
}
?>
<?php
while (list ($key,$val) = each ($_POST))
{
$key=stripplus($key);
$val=stripplus($val);
echo "$key = $val";
echo "<br>";
}
?>
let me know if you nee anything else
regards
srinivas
Posted: Wed Mar 26, 2003 5:55 am
by Igguana
What is suposed to do the second part of the script, is suposed to echoing something?
I can't see any results...
Posted: Wed Mar 26, 2003 6:15 am
by volka
take a look at
http://www.php.net/manual/en/function.parse-str.php
if you get rid of the ? the function should be useful
Posted: Wed Mar 26, 2003 6:33 am
by Igguana
I'm using this to get rid of the ?, but the echo shows again the ?.
Why?
Code: Select all
<?php
function stripplus($QUERY_STRING)
{
return(str_replace("?","&",$QUERY_STRING));
}
echo (urldecode ($QUERY_STRING));
?>
Posted: Wed Mar 26, 2003 6:58 am
by volka
Code: Select all
<html><body><pre>
<?php
$source = '?envio1=Alquiler?envio2=Puebla_de_Farnals?envio3=Local Comercial?envio4=4?envio5=24000?envio6=Costa';
// the first ? is useless
$source = substr($source, 1);
// btw: where do all those ?s come from??
$source = str_replace('?', '&', $source);
parse_str($source, $result);
print_r($result);
?></pre></body></html>
Posted: Wed Mar 26, 2003 7:10 am
by Igguana
The stupid ???? come from a javascript I used to send the variables to this page, I wanted the previous page to open with some features, like width, heigth, no menu bar, no status bar, etc. Using the common pop up script it was imposible (for me at least) to send variables using POST or GET.
Posted: Wed Mar 26, 2003 7:21 am
by volka
might be easier to change the javascript and let php do its work again

Posted: Wed Mar 26, 2003 7:25 am
by Igguana
If you know a way to open a new browser window with features by pressing the submit button of a form and sending the variables, you wellcome. By the moment, the problem I have using your code (It works in fact) is that, when I try to use it something happens:
Do you remember this code?
Code: Select all
<?php
if (isset($result['envio1'] ) && $result['envio1'] != -1)
$clauses[] = "Modo='".mysql_escape_string($result['envio1'])."'";
if (isset($result['envio2'] ) && $result['envio2'] != -1)
$clauses[] = "Localidad='".mysql_escape_string($result['envio2'])."'";
if (isset($result['envio3'] ) && $result['envio3'] != -1)
$clauses[] = "Tipo='".mysql_escape_string($result['envio3'])."'";
if (isset($result['envio4'] ) && $result['envio4'] != -1)
$clauses[] = 'Habitaciones='.(int)$result['envio4'];
if (isset($result['envio5'] ) && $result['envio5'] != -1)
$clauses[] = 'Precio<='.(int)$result['envio5'];
if (isset($result['envio6'] ) && $result['envio6'] != -1)
$clauses[] = "Zona='".mysql_escape_string($result['envio6'])."'";
?>
As you can see, I'm using the array $result that come from your previous code, what happens is that the query works for each element alone but with all of them together:
Array ( [envio1] => Alquiler [envio2] => Puebla de Farnals [envio3] => Local Comercial [envio4] => 7 [envio5] => 30000 [envio6] => Costa ) FROM DatosInmueble WHERE Modo='Alquiler'AND Localidad='Puebla de Farnals'AND Tipo='Local Comercial'AND Habitaciones=7AND Precio<=30000AND Zona='Costa' :You have an error in your SQL syntax near 'Precio<=30000AND Zona='Costa'' at line 1
Posted: Wed Mar 26, 2003 9:06 am
by Igguana
After making some experiments, what it seems to cause troubles is the combination in the query of "Precio" and "Zona".
Everything else seems to work pretty fine.
I'm thinking about the kind of the values...
Uhmm.
Why the hell I accepted this Job?

Posted: Wed Mar 26, 2003 9:10 am
by twigletmac
What does the implode() statement look like for $clauses because I think it's probably something like:
Code: Select all
$clauses = implode('AND ', $clauses);
but it needs to be
Code: Select all
$clauses = implode(' AND ', $clauses);
note the spaces on either side of AND.
It would probably be helpful if you posted the code that you use to create the SQL statement so that we can see if we can work out why it isn't working.
Mac
Posted: Wed Mar 26, 2003 9:18 am
by Igguana
that was exactly what happened!!!
I wonder how U realize that!
I have to be really blind...
Posted: Wed Mar 26, 2003 12:09 pm
by volka
and for your javascript-problem some client-side examples
Code: Select all
<html>
<!-- file test.php -->
<head>
<script type="text/javascript">
function setValues(oForm)
{
var d = new Date()
oForm.i1.value = d.getDate();
oForm.i2.value = d.getMonth();
oForm.i3.value = d.getFullYear();
return true;
}
function getFromForm()
{
oForm = document.getElementById("firstForm");
sUrl = "i1="+escape(oForm.i1.value);
sUrl += "&i2="+escape(oForm.i2.value);
sUrl += "&i3="+escape(oForm.i3.value);
document.location = "test.php?"+sUrl;
return true;
}
function getFromForm2(oForm)
{
sUrl = "test.php?";
for(i=0; i!=oForm.elements.length; i++)
{
if(oForm.elements[i].name && oForm.elements[i].name != "" && oForm.elements[i].value)
sUrl += escape(oForm.elements[i].name) + "=" + escape(oForm.elements[i].value) + "&";
}
document.location = sUrl;
return true;
}
</script>
</head>
<body>
<pre>
POST: <?php print_r($_POST); ?>
GET: <?php print_r($_GET); ?>
</pre>
<table border="1">
<tr><th>forms</th><th>anchors</th></tr>
<tr>
<td tyle="width: 50%;">
a form requesting a document in the same window
<form action="test.php" method="POST" id="firstForm">
<input type="text" name="i1" value="firstForm.v1" /><br />
<input type="text" name="i2" value="firstForm.v2" /><br />
<input type="text" name="i3" value="firstForm.v3" /><br />
<input type="submit" />
</form>
</td>
<td rowspan="5">
<a href="test.php">link without additional parameters</a><br />
<a href="test.php?var1=val1&var2=val2">link passing two parameters via GET</a><br />
<a href="test.php?var1=val1&var2=val2" target="_blank"> ...targeting a new window</a><br />
<a href="test.php?var1=val1&var2=val2" target="newWindow1"> ... targeting a new window called "newWindow1"</a><br />
<hr />
<a href="javascript:getFromForm();" >reading the values from the first form on the left side</a><br />
<a href="javascript:getFromForm2(document.getElementById('firstForm'));" >reading the values from the first form on the left side, not knowing the structure of the form</a><br />
</td>
</tr>
<tr>
<td>
a form requesting a document for a window called "newWindow1"
<form action="test.php" method="POST" target="newWindow1">
<input type="text" name="i1" /><br />
<input type="text" name="i2" /><br />
<input type="text" name="i3" /><br />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
a form requesting a document for a window called "newWindow1" <br />
elements are hidden
<form action="test.php" method="POST" target="newWindow1">
<input type="hidden" name="i1" value="v1" />
<input type="hidden" name="i2" value="v1" />
<input type="hidden" name="i3" value="v1" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
a form requesting a document for a window called "newWindow1" <br />
elements are hidden and set by setValues() via onSubmit
<form action="test.php" method="POST" target="newWindow1" onSubmit="javascript:setValues(this);">
<input type="hidden" name="i1" />
<input type="hidden" name="i2" />
<input type="hidden" name="i3" />
<input type="submit" />
</form>
</td>
</tr>
<tr>
<td>
a form requesting a document for a window called "newWindow1" <br />
elements are hidden and set by setValues() via onSubmit<br />
styled (a bit) like an <a href...>
<form action="test.php" method="POST" target="newWindow1" onSubmit="javascript:setValues(this);">
<input type="hidden" name="i1" />
<input type="hidden" name="i2" />
<input type="hidden" name="i3" />
<input type="submit" value="submit values"
style="border: 0px; background-color: white; text-decoration: underline; color: blue; cursor: pointer;"
/>
</form>
</td>
</tr>
</table>
</body>
</html>
maybe something of it is helpful...