View Full Version : concatenating multiple form controls from db info

11-13-2010, 03:12 AM

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?

11-13-2010, 07:31 AM
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 (http://www.tizag.com/mysqlTutorial/mysqljoins.php) if you want to read about it. Are you wanting to join the tables together in your PHP page or in your database?

11-13-2010, 07:53 AM
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:


$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>";


$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:

<script type="text/javascript">
function getXMLcontent(tvalue) {
alert(<?php echo $_get['child'] ?>);
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

document.getElementById('output').style.visibility = 'visible';
xmlhttp.open("GET","return.php", false);


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?

11-13-2010, 09:01 AM
'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.

11-13-2010, 09:05 AM
'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? :(

11-13-2010, 09:07 AM
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.

11-13-2010, 09:19 AM
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.

11-13-2010, 09:23 AM
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