Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been doing a lot of work in learning how to return multidimensional arrays dynamically- but what I can't seem to figure out is how to nest them.

I have two tables, each has the identical format: ID, name.

Table one: SSC - sscid - sscname

Table two: SRV - srvid - srvname

What I am trying to do is print all of the items in table two under EACH item in the table one list.

The table one items are the headers, the table two items are returned as a checkbox (with the srvid as the value) and label(srvname).

I can get it to all print together, but it is a. one giant list of results and it's in a

| checkbox | table 1: name | table 2: name | format. 

Not pretty at all (although it is progress for me to get this far).

After I run my query and get the result, my code looks like this:

Now, I've had a few additional thoughts about the design of the concept re:the database tables go, but everything I read indicates that they really need to be on their own tables, and they should be able to be referenced by the key from one table and the key from the other (eventually ended up in a joint table with user ID references) Because they are numerically indexed, I don't know why this would be an issue for me; however I simply can't seem to get this to work properly.

I should mention that when I alter the code to try to make the ssc_name span 2 cols and make it more like a header, it returns a header row for each checkbox/srv row, instead of for all of the checkbox/srv rows.

if($result) {
    echo '<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
    <tr><th colspan="2"><h3>Options</h3></th></tr>
    <tr><td></td><td align="left"><b>Services</b></td></tr>';
    $numfields = mysql_num_fields($result);
    $data = array();
    $flist = array();
    for($i=0;$i<$numfields;$i++)$flist[] = mysql_field_name($result,$i);
    $data[0] = $flist;
    while($row = mysql_fetch_assoc($result)) {
        $data[] = $row;
        echo '<tr><td colspan="2" align="center"><b>' . $row['ssc_name'] .'</b><td></tr>
        <tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>
        <td align="left">' . $row['ssvname'] . '</td>
        </tr>';
    }
    echo '</table>';
}

Can anyone help me figure this out, please?

share|improve this question
1  
Sorry to tell you that, but your code wouldn't make people want to read it. HTML, single line for, almost no indentation... Blah. –  Samy Dindane May 24 '12 at 22:10
 
Can you post a var_dump of the data being returned, without the markup mixed in? Also provide a rough example of the table you want (perhaps paste the HTML at jsfiddle.net so we can take a look). –  juddlyon May 24 '12 at 22:11
 
Is their any association with the first and second tables? If not just store the first in an array and loop through it in every iteration of the second query –  tylerpenney May 24 '12 at 22:11
 
@Samy I'm not sure what you mean? But I'll try to edit and space things out. All my lines are two space indents but that might just be the design standard side of me. –  mschocobo May 25 '12 at 0:13
 
@juddlyon I can do that- bbiab. tylerpenney they are related in the fact that they are two parts of an indication of a company's field of work- I actually have two versions of this setup: two tables, then the two tables combined with all possible combinations that return one ID. Since I just want it to return the data, I don't care (that much) how it looks; the pretty can come after the functional. hth. –  mschocobo May 25 '12 at 0:17

1 Answer

up vote 0 down vote accepted

You are missing a > on this line

<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>

Should be

<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" /></td>

Something you might be able to pick up on with better formatting...

<?php

    if ($result) {      
        echo <<<EOD
<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
    <tr>        
        <th colspan="2"><h3>Options</h3></th>       
    </tr>       
    <tr>    
        <td></td>       
        <td align="left"><b>Services</b></td>       
    </tr>
EOD;
        while ($row = mysql_fetch_assoc($result)) {         
            echo <<<EOD
    <tr>        
        <td colspan="2" align="center"><b>{$row['ssc_name']}</b><td>            
    </tr>       
    <tr>        
        <td align="center"><input type="checkbox" value="{$row['ssv_id']}" /></td>          
        <td align="left">{$row['ssvname']}</td>         
    </tr>           
EOD;
        }       
        echo '</table>';
    }

?>

And I'm not really sure what business any of those arrays have being in there.

share|improve this answer
 
Thank you! I should have updated that I figured it out. I have implemented better formatting and have adopted what seems to be the standard for spacing things out so hopefully future questions won't be so unappealing to answer. –  mschocobo May 31 '12 at 12:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.