Page 1 of 2

Store value from query string into an array

Posted: Fri Aug 18, 2006 4:23 pm
by adeelzia
Hello to All!
Below is given a query string send from one page and shown on another,and if one

see it in detail it contains values of a listbox i.e named "select2",and

different values from pervious page,can any one tell me how to save them in an

array on the next page and display them.thanks.

Code: Select all

http://localhost/final/junk2.php?select2=AM1&select2=AM2&select2=AM3&select2=AM5

&select2=AM9

Posted: Fri Aug 18, 2006 4:29 pm
by feyd
The following thread may be of interest: viewtopic.php?t=45711

Posted: Fri Aug 18, 2006 5:02 pm
by adeelzia
I cannot use [] sign to make "select2" an array bcoz this query is coming from another page and that page contains javascript coding to populate the "select2" listbox dynamically.Can there be any other way to sort out the problem

Posted: Fri Aug 18, 2006 5:17 pm
by feyd
Read the whole thread.

Posted: Fri Aug 18, 2006 5:29 pm
by adeelzia
sorry boss me a duff person cant understand your hint 8O :cry: ,please help me by giving a example code,would be very thankful

Posted: Fri Aug 18, 2006 5:45 pm
by s.dot
Try this on the page with the long select2 query string ;)

Code: Select all

echo '<pre>';
print_r($_GET['select2']);
echo '</pre>';
You may find what you're looking for. :-D

Posted: Fri Aug 18, 2006 5:47 pm
by s.dot
Hmm Maybe not. I thought it would prepopulate an array based on the query string.

Posted: Fri Aug 18, 2006 5:54 pm
by shiznatix
nope you cant send an multi demensional array though GET without an extra step. you gotta serialize() it first then unserialize() it

Posted: Fri Aug 18, 2006 6:42 pm
by feyd
shiznatix wrote:nope you cant send an multi demensional array though GET without an extra step. you gotta serialize() it first then unserialize() it
nonsense.

foo.php?foo[a][a]=1&foo[a]=2&foo[a][c]=3&foo[a]=4&foo=5&foo[c]=6&foo[c][a]=7&foo[c]=8&foo[c][c]=9

Posted: Fri Aug 18, 2006 7:04 pm
by feyd
adeelzia wrote:sorry boss me a duff person cant understand your hint 8O :cry: ,please help me by giving a example code,would be very thankful
The thread linked refers to parsing the raw submission data manually due to the inflexibility of the route chosen for submission.

$_SERVER['QUERY_STRING'] will contain the submission data you are dealing with. Using explode() a few times will result in name-value pairs which you can then iterate over with loop to generate arrays of multiple named values.

Posted: Sun Aug 20, 2006 3:21 pm
by adeelzia
[quote: Mr Fyed wrote]The thread linked refers to parsing the raw submission data manually due to the inflexibility of the route chosen for submission.

$_SERVER['QUERY_STRING'] will contain the submission data you are dealing with. Using explode() a few times will result in name-value pairs which you can then iterate over with loop to generate arrays of multiple named values.[/quote]

Mr.Fyed that explode() function also stores the last item of the list box(sent in the query string) i.e. all the other elements are overwritten because of the same variable name.
Dont All of You guy know how to change querystring into different named variables if a list box is sent in the query string..................[/quote]

Posted: Sun Aug 20, 2006 3:32 pm
by feyd
Show your code. I would suspect you have an error in the logic somewhere. When done right, the concept I posted about works perfectly well. Because most of us are in control of the forms that submit data to our pages, we code them such that PHP will automatically create the arrays as I showed previously.

Posted: Mon Aug 21, 2006 8:16 am
by adeelzia
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my code of the first page it contains javascript function to add or delete items from one listbox to another,listbox named select1 is populated from database(put garbage value if you want to check), and when done the generate report button send this whole values from listbox named select2 to the next page in form of querystring which shows like:

Code: Select all

http://localhost/final/junk2.php?select2=ActivityMonitor1&select2=ActivityMonitor2&select2=ActivityMonitor6&select2=adfasf&select2=002&genRep=Generate+Report
, now what I want is that these values should be stored into an array or something because I have some other works to do with them, let see how you handle this now,banking on your brilliance:


Code: Select all

<html>
<body>

<script language="JavaScript">

function deleteOption(object,index)
{
    object.options[index] = null;
}

function addOption(object,text,value)
{
    var defaultSelected = true;
    var selected = true;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
}

function copySelected(fromObject,toObject)
{
    for (var i=0, l=fromObject.options.length;i<l;i++)
    {
        if (fromObject.options[i].selected)
            addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--)
    {
        if (fromObject.options[i].selected)
            deleteOption(fromObject,i);
    }

}

function copyAll(fromObject,toObject)
{
    for (var i=0, l=fromObject.options.length;i<l;i++)
    {
        addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--)
    {
        deleteOption(fromObject,i);
    }
}

</script>
    
<form name=form1 action="junk2.php">

 <p align="center">

<select name="select1" multiple size="5">

<?php

                    include ("include/database.php");
                    $link = mysql_connect($dbServer, $dbUser, $dbPassword);

                    if (!$link)
                    {
                           die('Could not connect: ' . mysql_error());
                    }

                    else
                    {
                           mysql_select_db('project');
                           $result = mysql_query("SELECT projCode FROM defineproject WHERE projStatus = 'active'");



                           while ( $query_data = mysql_fetch_array($result))
                           {
                 ?>
                           <option value=  <? echo  @$query_data["projCode"]; ?>>

                 <?
                           echo $query_data["projCode"];
                           }

                           mysql_free_result($result);

                   }

                   mysql_close( $link );

                   ?>

  

                </option>
</select>
   </p>




<p align="center">

<input type="button" value=" > " onClick="if (document.images) copySelected(this.form.select1,this.form.select2)">
<p align="center">
<input type="button" value=" < " onClick="if (document.images) copySelected(this.form.select2,this.form.select1)">
<p align="center">
<input type="button" value=">>" onClick="if (document.images) copyAll(this.form.select1,this.form.select2)">
<p align="center">
<input type="button" value="<<" onClick="if (document.images) copyAll(this.form.select2,this.form.select1)">



<p align="center">

<select name="select2" multiple size="5">
</select>

</p>


 <p align="center"><font face="Verdana">
 <input type="submit" value="Generate Report" name="genRep" style="font-family: Verdana" >

 </font></p>


</form>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Aug 21, 2006 5:07 pm
by adeelzia
Is there any way that the following querystring should be made in such a way that the same names of select2 could be changed to something else and then stored into an array?also how it would be known that how many variables have been submitted.Thanks

Posted: Tue Aug 22, 2006 6:41 pm
by volka
a) I do not see any explode() in that code.
b)
adeelzia wrote:I cannot use [] sign to make "select2" an array bcoz this query is coming from another page and that page contains javascript coding to populate the "select2" listbox dynamically.
So why can't the select element have the name select2[] ?