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 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Building my table.

    PHP Code:
    <table border="1" cellspacing="1" cellpadding="5">
        <tr>
            <th>Questions</th>
            <th>Your Answer</th>
            <?php
            
    foreach($closest as $key => $value){
                
    $sql_name mysql_query("SELECT * FROM candidates where id ='$key'");
                
    $name[] = mysql_fetch_assoc($sql_name);
            }
            
            foreach(
    $name as $key => $value){
                echo 
    "<th>".$value['name']."</th>";
            }
            
    ?>
        </tr>
        <?php
            $sql_question 
    mysql_query("SELECT * FROM questions");
            while(
    $row mysql_fetch_assoc($sql_question)){
                echo 
    "<tr>";
                echo 
    "<td>".$row['question']."<td>";
                echo 
    "</tr>";
            }

            foreach(
    $user_answer as $key => $value){
                foreach(
    $value as $answer){
                    echo 
    "<tr>";
                    echo 
    "<td>".$answer."</td>";
                    echo 
    "</tr>";
                }
            }
            
        
    ?>
    </table>
    How can I build my table on the following patten.

    Code:
    <table border="1" cellspacing="" cellpadding="5">
        <tr>
            <th>Questions</th>
            <th>Your Answer</th>
            <th>Obama</th>
            <th>Clinton</th>
            <th>Cox</th>
        </tr>
        <tr>
            <td>Are you for or against drilling for energy and oil</td>
            <td>For</td>
            <td>Against</td>
            <td>For</td>
            <td>For</td>
        </tr>
        <tr>
            <td>Are you for or against Embyronic Stem Cells</td>
            <td>For</td>
            <td>For</td>
            <td>For</td>
            <td>For</td>
        </tr>
        <tr>
            <td>Are you for or against death penatly</td>
            <td>Against</td>
            <td>Against</td>
            <td>For</td>
            <td>Against</td>
        </tr>
        <tr>
            <td>Are you for or against education for all childrens</td>
            <td>Against</td>
            <td>Against</td>
            <td>Against</td>
            <td>Against</td>
        </tr>
        <tr>
            <td>Are you for or against same-sex marriage</td>
            <td>For</td>
            <td>Against</td>
            <td>Against</td>
            <td>For</td>
        </tr>
    </table>
    $closest =
    PHP Code:
    Array
    (
        [
    4] => 2
        
    [3] => 2
        
    [2] => 2

    $name =
    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 4
                
    [name] => Brownback
            
    )

        [
    1] => Array
            (
                [
    id] => 3
                
    [name] => Biden
            
    )

        [
    2] => Array
            (
                [
    id] => 2
                
    [name] => Clinton
            
    )


    $user_answer =
    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    0] => 1
                
    [1] => 2
                
    [2] => 2
                
    [3] => 1
                
    [4] => 2
            
    )


    I want to base the table on the HTML above but I cannot create it. How would I go about doing it?

  • #2
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Any ideas anyone?

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I have no idea how the pattern you posted relates to the $closest and $name arrays you posted.

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    I have no idea how the pattern you posted relates to the $closest and $name arrays you posted.
    These name comes from the name array that is build up through the closest array. The closest array contains the ids of these names.

    PHP Code:
            <th>Obama</th>
            <
    th>Clinton</th>
            <
    th>Cox</th>
        </
    tr

  • #5
    New Coder
    Join Date
    Feb 2007
    Location
    Near Belfast
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    I would do it like so...

    Code:
    <?
    //sql connection
    // mysql_query connection(s)
    ?>
    <table border="1" cellspacing="1" cellpadding="5">
    <?
    while($row = mysql_fetch_array($questions)){
        // can have a database column assigned to $question or, a switch statement
        echo '<tr><th>';
        echo $question;
        echo '</th></tr>';
        // insert switch statement $query1 to select different sql statements(answers) for different questions
            while($row = mysql_fetch_array($query1)){
                // echo all results for question one out
                echo '<tr><td>';
                echo $row['Answer'];
                echo '</td></tr>';
            }
        echo '<tr><td>&nbsp;</td></tr>';
    }
    ?>
    </table>
    Knowledge is only half the value of passion. But passion doesn't pay the bills.

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by pemcconnell View Post
    I would do it like so...

    Code:
    <?
    //sql connection
    // mysql_query connection(s)
    ?>
    <table border="1" cellspacing="1" cellpadding="5">
    <?
    while($row = mysql_fetch_array($questions)){
        // can have a database column assigned to $question or, a switch statement
        echo '<tr><th>';
        echo $question;
        echo '</th></tr>';
        // insert switch statement $query1 to select different sql statements(answers) for different questions
            while($row = mysql_fetch_array($query1)){
                // echo all results for question one out
                echo '<tr><td>';
                echo $row['Answer'];
                echo '</td></tr>';
            }
        echo '<tr><td>&nbsp;</td></tr>';
    }
    ?>
    </table>
    Can you give a more live example?


  •  

    Posting Permissions

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