Store value from query string into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Store value from query string into an array

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The following thread may be of interest: viewtopic.php?t=45711
adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read the whole thread.
adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Hmm Maybe not. I thought it would prepopulate an array based on the query string.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

nope you cant send an multi demensional array though GET without an extra step. you gotta serialize() it first then unserialize() it
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Post 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]
adeelzia
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 5:31 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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[] ?
Post Reply