building a query string

This is a discussion on building a query string within the C Programming forums, part of the General Programming Boards category; This is another part of that url-decode program, I apologize if I shouldn't have made a new thread, but it ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    building a query string

    This is another part of that url-decode program, I apologize if I shouldn't have made a new thread, but it is an entirely different problem. Thanks to the wonderful folks that have helped me here I'm pretty sure I can decode the key/values in a query string. I haven't seen any tutorials or ideas for building a query string with a C function though, I've seen a few with javascript and perl but they seem to be rare. Has anyone had any experience in assembling query strings with C? I found this example but it's written in PHP I think. Do you think Is it possible for me to translate this and encode it as C? I have the urlencode function working now.

    Code:
    html>
    <head>
    <title>A function to build query strings</title>
    </head>
    <body>
    <?php
    function qlink( $q ){
        GLOBAL $QUERY_STRING;
        if ( ! $q ) 
           return $QUERY_STRING;
        $ret = "";
        foreach( $q as $key => $val ) {
            if ( strlen( $ret ) ) 
               $ret .= "&";
            $ret .= urlencode( $key ) . "=" . urlencode( $val );
        }
        return $ret;
    }
    $q = array ( name => "Joe",
                 interest => "coding",
                 homepage => "http://www.google.com"
              );
    print qlink( $q );
    
    ?>
    <p>
    <a href="anotherpage.php?<? print qlink($q) ?>">Go!</a>
    </p>
    </body>
    </html>
    I haven't written any code yet for this function but some things are:

    a query string is going to be an array such as:

    name=Lince+Jones&address=410+E.+Butterfly+Blvd&pho ne=&#37;28888%29+867-5309

    This contains the keys: name, address, and phone.

    In the first step of your program that gets user input you should assemble a query string like
    the one above.

    Next, pass that query string to a generic routine that will
    build up a list of key/value pairs. Then implement a function that
    would let the caller retrieve a value from the list, using the list and
    the key as parameters, and returning a string.

    So the keys name, address, and phone are going to be, I'm not sure how to phrase it
    seperate objects that are passed to the query string building function right?
    Last edited by Lince; 08-22-2007 at 04:30 PM. Reason: spelling

  2. #2
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Lince,

    I have been watching your posts on this same topic for a bit now and it seems that you are more interested in obtaining an answer rather then attempting the work yourself. However, I will attempt to guide you towards your end goal...

    Your wondering how to put together something that will make an URL-encoded string (that in essence would most likely be used after the ? in an URL going towards a CGI script). This can be done very simply, especially if you know the exact values that you are going to be placing together.

    Example Psuedo-Code:

    Code:
    void BuildQueryString(char *dest_querystr, char *name, char *address, char *phonenum)
    {
    1. take all three and make one long string in the required format
    2. change all spaces in string to '+'s
    3. change any required special characters to their appropriate hex values
    4. ensure that fully encoded string goes in to dest_querystring (or whatever you want to name it)
    }
    Of course the format of the function definition could be changed as per your own required specifications of your software so far. By following those steps, you should in no time, have a function of your very own that will URL-encode and build any query string that you require.

    I would also suggest that if you are unable to build a program up like this (psuedo-code method) then perhaps something is missing. Like if you don't understand how URL-encoding/query strings work... do research on it. Figure out how it's done (many specifications out there for many different things) and then build psuedo-code for it. Follow the psuedo-code as your build your project and then eventually you wont have to do this extra step once experienced enough.

    If after making an attempt at the above psuedo-code or one of your own making, post some code back and I'll be happy to take a look it and see where we can go from there.

    Regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21