Stracting info from string_query
Moderator: General Moderators
Stracting info from string_query
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...
?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...
-
phpfreak
- Forum Commoner
- Posts: 30
- Joined: Fri Mar 21, 2003 10:28 am
- Location: New Jersey,USA
- Contact:
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
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
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
if you get rid of the ? the function should be useful
I'm using this to get rid of the ?, but the echo shows again the ?.
Why?
Why?
Code: Select all
<?php
function stripplus($QUERY_STRING)
{
return(str_replace("?","&",$QUERY_STRING));
}
echo (urldecode ($QUERY_STRING));
?>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>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?
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
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'])."'";
?>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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
What does the implode() statement look like for $clauses because I think it's probably something like:
but it needs to be
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
Code: Select all
$clauses = implode('AND ', $clauses);Code: Select all
$clauses = implode(' AND ', $clauses);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
and for your javascript-problem some client-side examplesmaybe something of it is helpful...
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>