SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Atlanta
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,
    I need to print the report with different tables having different columns, but I only want to use one sql statement.

    Here is what I'm doing:

    $result = mysql_query("select * from user");

    echo"<table>\n";
    while ($row=mysql_fetch_array($result))
    {
    printf("<tr><td>".$row["student"],"</td>");

    printf("<td>".$row["content"],"</td>");
    printf("<td>".$row["uniqueness"],"</td>");
    printf("<td>".$row["visual"],"</td>");
    printf("<td>".$row["graphics"],"</td>");
    printf("<td>".$row["layout"],"</td>");
    printf("<td>".$row["info"],"</td>");
    printf("<td>".$row["nav"],"</td>");
    printf("<td>".$row["speed"],"</td>");
    printf("<td>".$row["useful"],"</td>");
    printf("<td>".$row"[satisfied"],"</td></tr><br>\n");
    reset ($row);
    }
    echo"</table>";

    echo"<table>\n";
    while ($row=mysql_fetch_array($result))
    {
    printf("<tr><td>".$row["count(student)"],"</td>");
    printf("<td>".$row["avg(content)"],"</td>");
    //printf("<tr><td>".count($row["content"])=count($row["content"])+1,"</td><br>\n");
    printf("<td>".$row["avg(uniqueness)"],"</td>");
    printf("<td>".$row["avg(visual)"],"</td>");
    printf("<td>".$row["avg(graphics)"],"</td>");
    printf("<td>".$row["avg(layout)"],"</td>");
    printf("<td>".$row["avg(info)"],"</td>");
    printf("<td>".$row["avg(nav)"],"</td>");
    printf("<td>".$row["avg(speed)"],"</td>");
    printf("<td>".$row["avg(useful)"],"</td>");
    printf("<td>".$row["avg(how_satisfied)"],"</td></tr><br>");

    }
    echo"</table>";
    echo"<table>\n";
    while ($row=mysql_fetch_array($result))
    {
    printf("\n<tr><td>".$row["stud"],"</td>");
    printf("<td>".$row["staff"],"</td>");
    printf("<td>".$row["visitor"],"</td>");
    printf("<td>".$row["date_time"],"</td>");
    printf("<td>".$row["info"],"</td>");
    printf("<td>".$row["need"],"</td>");

    printf("<td>".$row["type_info_needed"],"</td></tr><br>\n");
    }
    echo"</table>";


    echo"<table>\n";
    while ($row=mysql_fetch_array($result))
    {
    printf("<tr><td>".$row["camp"],"</td>");
    printf("<td>".$row["stud"],"</td>");
    printf("<td>".$row["staff"],"</td>");
    printf("<td>".$row["visitor"],"</td>");
    printf("<td>".$row["test"],"</td>");
    printf("<td>".$row["brows"],"</td></tr><br>\n");
    echo "type_info_needed: ".$row[1]."<br>\n";
    }
    echo"</table>";
    mysql_free_result ($result);

    This code is executing only the first while statement, after that it just exits.

    Please give me the solution. Thankyou
    Aneal
    Aneal

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the first while loop, first
    $i = 0;
    while ($row=mysql_fetch_array($result))
    {
    // first keep all codes of the first while loop here,
    // but remove the line: reset ($row);
    :
    // then assign each row variable of previous second and
    // third while loops to temporary arrays such as

    temp_count_student [$i] = $row["count(student)"];
    temp_avg_content [$i] = $row["avg(content)"];
    //do the same for all others
    $i = $i + 1;
    }

    $i = $i -1;
    for ($j = 0; $j < $i; $j++)
    {
    //assemble your printf here for second table
    printf("<tr><td>".$temp_count_student [$j],"</td>");
    printf("<tr><td>".$temp_avg_content [$j],"</td>");
    // and so on...
    }

    for ($j = 0; $j < $i; $j++)
    {
    //assemble your printf here for third table
    .................
    }

    John
    Last edited by johnn; May 24, 2001 at 00:09.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Ahhh hah! I understand what you want to do now. You want to query a database for the information in a table. Then you want to display some of the fields in a record in one table, and display other fields in the record in a second table, and then display the rest of the fields in the record in a third table. Right?

    Ok, you tried this:

    reset ($row);

    That doesnt work because $row is a "horizontal" array containing the fields of one record. So, at the most you would be resetting the array $row that contains one record back to the first field in that record; but, in fact, the while loop terminates when there are no more $row's left, so you are trying to reset a variable that contains nothing. What you should try to do is use reset() on $result which contains a "vertical" array of all the records to try and reset it back to the first record. I don't know if that will work because $result is what is called a "result identifier", so I do not think it is really an array, but try it and see if it works.

    To get around that problem if reset($result) doesn't work, you could try this, but I have a feeling it might not work either:

    $temp=$result;
    while ($row=mysql_fetch_array($temp))

    Something that should work is this:
    Code:
    while ($row=mysql_fetch_array($result))
    {
       $stored_records[$i]=$row;
       $i++;
    }
    Then you will have a two dimensional array $stored_records, and you can do this:
    Code:
    for ($j=0; $j<count($stored_records); j++)
    {
       echo '<td>' . $stored_records[$j]['name'] . '</td>';
    }
    Finally, these statements won't work:

    printf("<td>".$row["avg(visual)"],"</td>");

    There is no field name called "avg(visual)" in your table is there? avg() is an sql function and can only be in a SELECT statement.
    Last edited by 7stud; May 24, 2001 at 01:30.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Atlanta
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you friends,

    It is working fine now and thanks for explanings the reset(), and other things.

    Can you tell me that how I can get the average of the numeric fields and the count of the rows without using the mysql functions.

    Aneal
    Aneal

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Did reset() work on $result? What was your solution? Hmmm...it doesn't sound like you understood my last post if you are asking how to get the row count.
    PHP Code:
    for ($i=0$icount($stored_records); $i++)
    {
        
    $visual += $stored_records[$i]['visual'];
        
    $graphics += $stored_records[$i]['graphics'];
    }
    $row_count count($stored_records);
    $avg_visual $visual/$row_count;
    $avg_graphics $graphics/$row_count

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Atlanta
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got the count and the average. The reset with $result didn't work either.

    Actually I can't make your codes to work for me, every time I open the file in the browser it looks like there was a continue loop, and it never shows anything on the screen, and when I did the 'ctrl+alt+del' it says that 'not responding'. So I tried the 7stud codes with some modifications and your average, and count code it just working fine. Thank you for your help. I realy appreciate for that. Now I realy can enjoy my weekend.

    I still wanted to work with that and want to improve it, look at the 7stud codes to get an idea what I'm doing. Right now it is too long, and I am including all the coumns in the while statement to use them in for loops.



    Aneal
    Aneal


Bookmarks

Posting Permissions

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