Multiple variables in GET from FORM

Hi,

I have this form:


        <form method="get" action="">
         <label>Search by type: </label>
            <select name="c" id="c">
               <? while($row = mysql_fetch_object($category)):
            echo "<option value=\\"$row->CTname\\">$row->CTname</option>";
                  endwhile;
               ?>
            </select>
        <input type="submit" id="submit" value="Search"/>
       </form>

When i hit the button it gives me:

manage-comments?c=ConfPaper

How can i make it so it does this:

manage-comments?c=ConfPaper&ID=2

The ID field from the database is:


$row->CTid

So in a sense i am trying to show this in the URL:


manage-comments?c=$row->CTname&ID=$row->CTid

Is this possible?

If so, can anyone kindly help.

Thanks

Is there an alternative method i could try to implement? All 10 tables have different information so i need to have these. So i have added a Category table which can distinguish between each table. This is why each table has a ‘Pid’ just so i can separate the tables.

I just need a method where i can SELECT each Comment (from table Comments), from any of the ten tables, and also within each table to row it relates to…

How do you guys think i should do this?

The OP’s code is

<select name=“c” id=“c”>

Now you can argue that ‘c’ is not a very descriptive name, but saying that it won’t work is not correct. And I don’t know why he needs the id ‘c’, but it has nothing to do with the query string because as you say, that value will not be sent.

Personally rather than aim for this…

manage-comments?c=ConfPaper&ID=2

I’d aim for this

manage-comments?ConfPaper=2

And that is exactly what I did in the code I posted. :slight_smile:

When I said that I was referring to receiving an ID from the attribute of the element.

<select id=“c”>

There’s no way you can get that c into the form. You have to use another form element to carry it.

Personally rather than aim for this…

manage-comments?c=ConfPaper&ID=2

I’d aim for this

manage-comments?ConfPaper=2

Not only easier to read (which it should be since this will be passed as Get) but the form is easier

<select name=“ConfPaper”>

The PHP will be trickier but not enough to disqualify the approach.

Ok,

I might as well explain what exactly i am trying to do, then maybe you guys can explain a better method.

Right, i have a Comments table:

Comments
CMid
CMHid
CMPid
CMUid
CMcomment
CMdateadded
CMdeleted

Each comment can be linked to 10 different tables:

ConfPaper, JournalArticle, EdBookChapter, Thesis, Patent, Monograph, Unpublished, Misc, Website, TechReport

Each table contains different information, and holds documents.

Now each of these 10 tables have a “Pid” which is how the Comments table JOINS onto it by CMHid.

I need to basically retrieve which record has been commented on FROM WHICH table.

So a typical set of records in the Comments table can be as such:

CMid - 2 (Unique, AutoIncr)
CMHid - 1 (Which Table from the 10 it belongs to)
CMPid - 13 (ID of record from the 10 tables)
CMUid - 1 (User ID)
CMcomment - This is a comment
CMdateadded - 2010-07-26 15:05:31
CMdeleted - 0

So to do this i was thinking of doing something like this:


    public function selectAllComments($tablename, $tableID){

        $query = "SELECT * FROM Comments c
                   LEFT JOIN Category ct ON c.CMHid = ct.CTid
                   LEFT JOIN Users u ON u.Uid = c.CMUid
                   LEFT JOIN ConfPaper t ON t.Pid = c.CMHid
                   WHERE c.CMdeleted = 0 AND c.CMHid = 1
                   GROUP BY c.CMcomment
                   ORDER BY c.CMdateadded DESC";
        return mysql_query($query);
    }

Category is a list of categories for each of the 10 tables, the Pid in each table represents which category it belongs to.

So now with the form i am trying to retrieve which table name AND the ID of the table:


        <form method="get" action="">
         <label>Search by type: </label>
            <select name="c" id="c">
               <? while($row = mysql_fetch_object($category)):
                        echo "<option value=\\"$row->CTname\\">$row->CTname</option>";
                  endwhile;
               ?>
            </select>
        <input type="submit" id="submit" value="Search"/>
       </form>

Now firstly, is my method even the correct way to do this?

How can i achieve what i am trying to do?

Thanks

guido2004, you are a genius! :lol:

I changed my method to this:


    public function selectAllComments($tableID, $tablename, $prefix){

        $query = "SELECT *, t.".$prefix."title AS theName FROM Comments c
                   LEFT JOIN Category ct ON c.CMid = ct.CTid
                   LEFT JOIN Users u ON u.Uid = c.CMUid
                   LEFT JOIN ".$tablename." t ON t.Pid = c.CMHid
                   WHERE c.CMdeleted = 0 AND c.CMHid = ".$tableID."
                   GROUP BY c.CMcomment
                   ORDER BY c.CMdateadded DESC";
        return mysql_query($query);
    }

then just selected:

$row->theName

Thank you so much!

:blush:
You’re welcome

To achieve what? Send two values in the query string? Of course, more than two also. Each form field value will be sent in the query string.

If you’re talking about what you want to do with the values sent by the form, I have no idea if it’s possible. I have no idea what you want to achieve :slight_smile:

Can you explain what the form’s supposed to do?

You could add a column to the categories table that contains the prefix.
Then all you have to do is add the prefix to the function call:


    $comments = Comments::selectAllComments($_GET['c'], $cat->CTname, $cat->CTprefix);

And change a little bit in the query:


    public function selectAllComments($tableID, $tablename, $prefix) {

        $query = "SELECT *, ".$prefix."title FROM Comments c
                   LEFT JOIN Category ct ON c.CMid = ct.CTid
                   LEFT JOIN Users u ON u.Uid = c.CMUid
                   LEFT JOIN ".$tablename." t ON t.Pid = c.CMHid
                   WHERE c.CMdeleted = 0 AND c.CMHid = ".$tableID."
                   GROUP BY c.CMcomment
                   ORDER BY c.CMdateadded DESC";
        return mysql_query($query);
    } 

Ok, if i didn’t change the table rows all to match title, you have suggested constructing it just the same way i have done for the table name.

The query is running correctly, its just a matter of selecting the correct title. Only problem is how would i be able to include it in my query?

Can you point me in the right direction? Just slightly confused as to how it would work…

Thanks

Do you want the users editing it?

Ok, let me rephrase – if you want ANYTHING to show up in the form data it must be contained in the value of a form element (input, textarea, select). The value of a textarea tag is it’s innerHTML.

By convention data you need to pass on the form that isn’t editable is placed in a hidden field, hence my original answer.

The id attribute will NOT work.

Why?

If you want the id to be passed when the form is submitted it must be a hidden value.

<input type="hidden" name="id" value="<?= $id ?>">

The approach you are trying will never work. The ID attribute of any form element tag is not part of the data of the form and should not be construed as such. The id of a form element sees two principle uses. First is for javascript DOM manipulation (though form elements can also be reached by addressing their name property). Second is as a tie in for the “for” attribute of the label tag.

Also bear in mind that making an input element with a name of ID is problematic for javascript interactions. In many frameworks and some browsers the an input field with the name “id” will get crossed up with id attribute of the form itself. It is better to use another name for this reason to avoid confusion - such as recordid.

The value will be changed by the user, thats the problem.

Would it still be possible to achieve this if thats the case?

I guess you could:

  1. change the column name in ‘title’ for all 10 tables :slight_smile:
    or
  2. construct the column name dynamically in your query, like you do with the table name.

Hi,

Thanks for all of your responses, guido2004 i just looked at your suggestions and got it to work :smiley:

This is what i did:


        <form method="get" action="">
         <label>Search by type: </label>
            <select name="c" id="c">
               <? while($row = mysql_fetch_object($category)):
                        echo "<option value=\\"$row->CTid\\">$row->CTname</option>";
                  endwhile;
               ?>
            </select>
        <input type="submit" id="submit" value="Search"/>
       </form>

Then i had this to check:


if(isset($_GET['c'])):
    $category_name = Comments::selectAllCategoryByID($_GET['c']);
    $cat = mysql_fetch_object($category_name);

    $comments = Comments::selectAllComments($_GET['c'], $cat->CTname);
endif;

And finally this is my method inside my class:


    public function selectAllComments($tableID, $tablename){

        $query = "SELECT * FROM Comments c
                   LEFT JOIN Category ct ON c.CMid = ct.CTid
                   LEFT JOIN Users u ON u.Uid = c.CMUid
                   LEFT JOIN ".$tablename." t ON t.Pid = c.CMHid
                   WHERE c.CMdeleted = 0 AND c.CMHid = ".$tableID."
                   GROUP BY c.CMcomment
                   ORDER BY c.CMdateadded DESC";
        return mysql_query($query);
    }

I have one final problem with this. I need to also select the “Title” from the table that is returned. If you look here:

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/comments.jpg

You can see it all works well, but the title (Paper) does not appear. This is because the title fields in all 10 tables hold a different prefix.

For example, ConfPaper has CPtitle, EditedBook has EDtitle…

So how can i select the title that is relevant to the option selected from the drop down?

Hope you can help?

Also, what would you suggest changing the name ‘c’ to? I would not be able to change it to what you suggested because the name woudl always be changing depending on the option selected from the dropdown…These pages are only accessible in the admin section of the website so nobody can really get to the page if they are not logged in, so i’m not that worried that somebody will go on and try fiddling around…

Thanks

Amazing! i didn’t even think of that, let me try that and i will get back to you :slight_smile:

Without entering the discussion if the ten separate tables is a good idea (it might be, but that’s a question you’d better ask in the MySQL or database (since this is a more generic database design question, not MySQL specific)), I think what you need for your query is to pass the CTid, not the CTname, to the script:


        <form method="get" action="">
         <label>Search by type: </label>
            <select name="c" id="c">
               <? while($row = mysql_fetch_object($category)):
            echo "<option value=\\"$row->CTid\\">$row->CTname</option>";
                  endwhile;
               ?>
            </select>
        <input type="submit" id="submit" value="Search"/>
       </form>

Now ‘c’ will contain the id, not the name.

If I have understood what you have tried to do, I would just pass the ID of the category and will find the category name (or table name) from the table before preparing the comment retrieving query.