Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts

    concatenating multiple form controls from db info

    all,

    does anyone have some advice on how I could go about concating a series of related values between, say, 4 or five 5 tables??

    for instance, I currently have a mysql db with 4 related tables, which can all be innerjoined in one query statement if I really wanted. but what i'm looking at getting is a few menus on my page that are 'multiples', all related to the child side of the initial relationship.

    so I would want to be able to select a parent record from one menu, and then multiselect the childs from the next menu. and after, display that information as one concatenated sting on the page. it doesn't really matter right now if I print it or throw it to an alert box. I just want to work it the easiest way. the language doesn't matter either. one hurdle is the fact that I have so many records that they have to be stored in a db.

    first of all, is there any easier way to display my parent choices than querying the mysql db and echoing out a million '<option>' tags inside of a single '<select>' tag? thinking about it, that's the only idea I have right now. can someone suggest a better way perhaps?

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    Well, you're going to need to echo out as many option tags as you have options. If you're doing it manually now, a while(mysql_fetch_array()) statement might be easier, and if you're already doing that, it really is the only way. A little about joining tables here if you want to read about it. Are you wanting to join the tables together in your PHP page or in your database?
    "Yeah science!"
    Online Science Tools

  • #3
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    I don't really need to read about those joins, as I already have a handle on db theory from my past.

    but since there is really no other way other than the echo, the biggest issue I'm dealing with right now is being able to use AJAX to fill in the child menus on the page. for example, here is the page I would call using ajax:
    PHP Code:
    <?php

        $query 
    "SELECT child FROM table WHERE parent = '" $_get['child'] . "'" or die(mysql_error());
        
    $result mysql_query($query) or die(mysql_error());
        
    $numrows mysql_numrows($result);
        
    $childlist '<select style="width:75%" name="child[]" id="child" multiple="multiple" onchange="javascript:getXMLcontent("'."'"."child"."'".')">';
        
    $i 0;
        
        
    //$childlist = $childlist . "<option value=" . '"' . '"' . "></option>";
        
        
    while ($i $numrows) {
            
    $childlist $childlist "<option value=" '"' mysql_result($result$i"child") . '"' ">" mysql_result($result$i"child") . "</option>";
        
            
    $i++;
        }
        
        
    $childlist $childlist "</select>";
        echo 
    "$childlist ";

    ?>
    so the page is echoing the innerHTML of the div I want to fill. the div = the child menu. and here is the javascript code that grabs the callback:
    Code:
    <script type="text/javascript">
    function getXMLcontent(tvalue) {
    alert(<?php echo $_get['child'] ?>);
    	if (window.XMLHttpRequest)
    	  {
    	  xmlhttp=new XMLHttpRequest();
    	  }
    	else
    	  {
    	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	  }
    	  
    				document.getElementById('output').style.visibility = 'visible';
    					xmlhttp.open("GET","return.php", false);
    					xmlhttp.send();
    
    		document.getElementById('2').innerHTML=xmlhttp.responseText;
    }
    </script>
    the form that my menus are located in is the "get" method. the alert that comes back from the js function says "undefined". I know I'm wrong and my coding is wrong, but I don't know where. can you give me a boost?
    Last edited by ajetrumpet; 11-13-2010 at 06:56 AM.

  • #4
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    'mysql_numrows' should be 'mysql_num_rows'. You can start with that. It might not solve your actual problem, but it would be easier if you echoed $chidllist inside of the while loop instead of appending all of your data into one variable. The <select> part you don't even have to echo, you can type that in plain HTML by itself.
    Last edited by djh101; 11-13-2010 at 08:05 AM.
    "Yeah science!"
    Online Science Tools

  • #5
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by djh101 View Post
    'mysql_numrows' should be 'mysql_num_rows'. You can start with that.
    I know, but my browsers work as is. When I start to produce code live, I'll do a find and replace and globally fix this in all of my files.

    is that you got for me regarding this thread?

  • #6
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    No, just saying you should start off with that. I like to take care of one problem at a time. mysql_numrows isn't a mysql command, no matter what browser or server you're using (unless you are using php and mysql installed on your own server and manually went in and changed the syntax rules).

    EDIT: Also, there shouldn't be quotes around $chidlist in that last echo command. Quotes are for strings, variables don't need to be quoted.
    Last edited by djh101; 11-13-2010 at 08:12 AM.
    "Yeah science!"
    Online Science Tools

  • #7
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    okie dokie. what about the issue surrounding the "get" variable that is coming back as undefined?

    and also, the PHP documentation I read way back when told me that double quotes around variables is just fine, as doubles are much more forgiving than singles are.

  • #8
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    Okay, let me just put all my current suggestions together in this one post:
    -$_get should be $_GET (variables are case sensitive)
    -mysql_numrows is invalid, it should be mysql_num_rows
    -or die(mysql_error()) is not attached to your $query variable
    -<select ... > does not need to be inside your PHP
    -not very important, but you could use a for loop instead of a while loop
    -quotes can be escaped with a \ rather than by what you're doing now with all those single quotes and dots
    -your HTML can be echoed inside of the while statement instead of appending everything into one variable
    -there shouldn't be quotes around $childlist in your echo statement
    "Yeah science!"
    Online Science Tools


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •