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

Thread: Table iteration

  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Table iteration

    PHP Code:
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th>Questions</th>
            <th>Your Answer</th>
            <?php
            
    foreach($closest as $key => $value){
            
    $result mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
            while (
    $row mysql_fetch_array($result)){
                echo 
    "<th>".$row['name']."</th>";
            }
            }
            
    ?>
        </tr>
            <?php
            $result 
    mysql_query("SELECT * FROM questions");
            foreach(
    $user_answer[0] as $key => $value){
                
    $a $value;
            }
            while (
    $row mysql_fetch_array($result)){
                echo 
    "<tr>";
                echo 
    "<td>".$row['question']."</td>";
                echo 
    "<td>".$a."</td>";
                echo 
    "</tr>";
            }
            
    ?>
        </tr>
    </table>
    How do I echo out all the $value($a) in the while loop? $a is just the last value of the array right now.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th>Questions</th>
            <th>Your Answer</th>
            <?php
            
    foreach($closest as $key => $value){
            
    $result mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
            while (
    $row mysql_fetch_array($result)){
                echo 
    "<th>".$row['name']."</th>";
            }
            }
            
    ?>
        </tr>
            <?php
            $result 
    mysql_query("SELECT * FROM questions");
            
    $a $user_answer[0];
            
    $i 0;
            while (
    $row mysql_fetch_array($result)){
                echo 
    "<tr>";
                echo 
    "<td>".$row['question']."</td>";
                echo 
    "<td>".$a[$i]."</td>";
                echo 
    "</tr>";
                
    $i++;
            }
            
    ?>
        </tr>
    </table>
    Is that what you wanted?

  • Users who have thanked Mwnciau for this post:

    MHaris (08-23-2007)

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    PHP Code:
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th>Questions</th>
            <th>Your Answer</th>
            <?php
            
    foreach($closest as $key => $value){
            
    $result mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
            while (
    $row mysql_fetch_array($result)){
                echo 
    "<th>".$row['name']."</th>";
            }
            }
            
    ?>
        </tr>
            <?php
            $a 
    $user_answer[0];
            
    $i 0;
            foreach(
    $closest as $key => $value){
            
    $result mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");
            }
            while (
    $row mysql_fetch_array($resultMYSQL_BOTH)){
                echo 
    "<tr>";
                echo 
    "<td>".$row['question']."</td>";
                echo 
    "<td>".$a[$i]."</td>";
                echo 
    "<td>".$row['answer']."</td>";
                
    $i++;
                echo 
    "</tr>";
            }
            
    ?>
        </tr>
    </table>
    $row['answer'] is suppose to echo answers of all the three keys in closest but echoing only the last key.

    What should I do? And is there a better way to code these type of things?

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Help?

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    foreach($closest as $key => $value){
    $result mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");

    Everytime that loops round $result is being written over, I'm not sure what you want to do, can you explain it again?

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mwnciau View Post
    PHP Code:
    foreach($closest as $key => $value){
    $result mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.id = '$key'");

    Everytime that loops round $result is being written over, I'm not sure what you want to do, can you explain it again?
    http://codingforums.com/showthread.php?t=122156


  •  

    Posting Permissions

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