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

    foreach unde while?

    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_ASSOC)){
                echo 
    "<tr>";
                echo 
    "<td>".$row['question']."</td>";
                echo 
    "<td>".$a[$i]."</td>";
                foreach(
    $closest as $key => $value){
                echo 
    "<td>".$row['answer']."</td>";
                }
                
    $i++;
                echo 
    "</tr>";
            }
            
    ?>
        </tr>
    </table>
    The $row['answer'] is taking the last row from the query that is in foreach. Though, I want all the answers of all the candidates to be published in the table.

    Any help how would I go about doing it? If I do a while in the second foreach, 10 tds will be created due to repetition.

    PHP Code:
                foreach($closest as $key => $value){
                echo 
    "<td>".$row['answer']."</td>";
                } 

    How should I do it?

  • #2
    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
    The obvious problem is you have your mysql_query() function call inside your foreach loop but you do not fetch the data out of the query until after the foreach loop is over, which means the only $closest array element that is fetched out of the query is the last one.

    I would suggest you run one query and build an array containing just the relevant info you need based on that one query. You can then manipulate the array for purposes of building your html code. As you have it, you're using the query loop (the "while"s) to build the markup, so you're forced into multiple queries, and (apparantly) multiple foreach loops on the $closest array.

    Which brings me to my question: What does the $closest array contain and what does it do for you? It seems the data from your query is related directly to the stuff in the $closest array (connected via $key), so it makes sense to me to add the query data to the $closest array and go from there.

    Example:
    PHP Code:
    foreach($closest as $key => $value){

        
    $result mysql_query("
        SELECT c.*, q.*, a.*
        FROM answers as a
        LEFT JOIN candidates as c
          ON a.candidate_id = c.id
        LEFT JOIN questions as q
          ON a.question_id = q.id
        WHERE c.id = '$key'"
    ) or die(mysql_error());

        for (
    $i 0$i mysql_num_rows($result); $i++)
            
    $closest[$key][$i] = mysql_fetch_assoc($result);
        }
    }

    //You can now use the $closest array to build your html without having to do any other queries
    foreach ($closest as $key => $value) {
        foreach (
    $value as $answers) {
            echo 
    "<th>".$answers['name']."</th>";
        }


  • Users who have thanked Fumigator for this post:

    MHaris (08-24-2007)

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    PHP Code:
    WarningCannot use a scalar value as an array in C:wampwwwquiztemplatescompare_table.php on line 18 
    PHP Code:
            $closest[$key][$i] = mysql_fetch_assoc($result); 
    $closest is
    PHP Code:
    foreach($binned as $key => $value){
        
    $results[$key] = count(array_diff_assoc($user_answer[0], $value));
    }
        
    asort($results);
    $closest array_slice($results031); 
    And $binned is:

    PHP Code:
    $result mysql_query("SELECT * FROM answers") or die(mysql_error());
    while (
    $row mysql_fetch_assoc($result)){
        
    $cID $row['candidate_id'];
        
    $binned_key $cID;
        
    $binned[$binned_key][] = $row["answer"];

    And also Warning:

    Invalid argument supplied for foreach() in C:\wamp\www\quiz\templates\compare_table.php on line 24
    Line # 24:
    PHP Code:
       foreach ($value as $answers) { 
    Last edited by MHaris; 08-24-2007 at 01:08 PM.

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

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    $value is not an array, post the code that deals with it before line 24.

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    $value is not an array, post the code that deals with it before line 24.
    PHP Code:
    //You can now use the $closest array to build your html without having to do any other queries
    foreach ($closest as $key => $value) {
        foreach (
    $value as $answers) {
            echo 
    "<td>".$answers['name']."</td>";
        }
    }  
            
    ?> 

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Put this before the foreach and post what it outputs:
    PHP Code:
    echo '<pre>' print_r($closest1); 

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    question if i may, what does the 1 do in print_r() is it the same as true? if so why would you want to return the output instead of it being output?

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yes, it's the same. It returns the output to echo() because print_r() will print the array before anything is output so the <pre> tag would be after the array. So, if you return it to echo() the <pre> tag is first and will format the array so it's more readable.

    Edit: This is only when it's on the same line. You can echo('<pre>'); before using print_r() and get the same result. I just like it all on one line.
    Last edited by Inigoesdr; 08-26-2007 at 04:56 PM.

  • Users who have thanked Inigoesdr for this post:

    rafiki (08-26-2007)

  • #10
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Put this before the foreach and post what it outputs:
    PHP Code:
    echo '<pre>' print_r($closest1); 
    PHP Code:
    Array
    (
        [
    2] => Array
            (
                [
    4] => Array
                    (
                        [
    id] => 34
                        
    [name] => Clinton
                        
    [question] => Are you for or against same-sex marriage
                        
    [answer] => 2
                        
    [question_id] => 7
                        
    [candidate_id] => 2
                    
    )

            )



  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Maybe you should post the full page, because I just ran that code and it works fine for me.

  • #12
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I have the same prob as here...

    I want to display html table like this.
    A call log each hour. (24 hours)

    Code:
    <tr><td>Phone#</td>
                   <td>0</td>
                   <td>1</td>
                   <td>2</td> ..... 
                   <td>23</td>
    </tr>
    
    # loop until last row and position each call to spicific time(hour)
    <tr>         <td>123-43-54</td>
                   <td>00:15<br>
                         00:35</td>
                   <td>01:15<br>
                         01:25<br>
                         01:55</td>
    </tr>
    <tr>         <td>123-52-78</td>
                   <td>02:15<br>
                         02:35</td>
                   <td>23:15<br>
                         23:25<br>
                         23:55</td>
    </tr>
    I only have one mysql table for this.
    1. caller
    2. time_call

    I need help regarding this. Any Help?


  •  

    Posting Permissions

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