Results 1 to 39 of 39

Thread: printing results to a table

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    printing results to a table

    I have a mysql query that I want to print into a table, I can do it for cells one about each other but what if I want it to be 3 results wide and then endless deep until the results expire... who do I do this???

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    so it's like this?
    Code:
    RESULT1      RESULT2      RESULT3
    RESULT4      RESULT5      RESULT6
    .... and so on
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139
    YEAH

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Here is an example I have used:


    Code:
    ?>
       <table width="95%" padding="0" cellpadding="0" cellspacing="0">
    <?
       printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",$Link1, $Link1, $Link2);
    ?>
       </table>
    <?
    See if you understand it....

  5. #5
    But that is not dynamic, he needs to use it in a loop.

    You can use PHP's modulus operator and a counter. Pretend $result is a mysql result.

    PHP Code:
    echo '<table><tr>';

    $counter 0;
    while(
    $row mysql_fetch_array($result))
    {
        if(++
    $counter == 0// every 4th item, make a new table row
            
    echo '</tr><tr>';
        echo 
    '<td>' $row['mydata'] . '</td>';
    }

    echo 
    '</tr></table>'

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder
    But that is not dynamic, he needs to use it in a loop.

    Then he can just set the start of the loop after the <table> tag, and end if before the </table> tag.... That will work...

  7. #7
    But each "revolution" of the loop doesn't bring out three values at once

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder
    But each "revolution" of the loop doesn't bring out three values at once

    Depends on how he write the loop. There is no problem to first check how many results he fot, then use a for loop to go through it, and then use 3

    $row = mysql_fetch_array($resultat)


    and then stor the results in 3 diffrent variables, and then print them out like in my code...

  9. #9
    I don't know what you mean, why would you do that

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    So you can get all the result and print them as my code of line showed...the firs line starts the table, then you print out a whole row in one go, then next round in the loop you print the next row and so on....

  11. #11
    How..? You can't get three rows at once, unless you use 3 different variables which isn't very practical.

    Or are you saying that for the sake of argument

  12. #12
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    How about this:
    PHP Code:
    <?
      $query = "SELECT `data1` ";
      $query.= "FROM `datatable` ";
      $results = mysql_query($query) or $DBError = "DB3"; // Ignore this (its how I handle Errors)
      
      $DataCols = 1;
      while ($Result = mysql_fetch_array($results))
      { ?>
        <TR>
          <TD><?= $Result['data1']; ?></TD>
    <?  while ($DataCols < 3 AND $Result = mysql_fetch_array($results))
        { ?>
          <TD><?= $Result['data1']; ?></TD>
    <?    $DataCols++;
        }

        while ($DataCols < 3)
        { ?>
          <TD>No Data</TD>
    <?    $DataCols++;
        } ?>
        <TR>
    <?  $DataCols = 1;
      } ?>
    I've tested it and it works too .

    EDIT: Swaped round the AND statement
    Last edited by Electroman; May 2nd, 2004 at 01:47 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  13. #13
    So you'd rather do it that way?

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by Chroder
    How..? You can't get three rows at once, unless you use 3 different variables which isn't very practical.
    Why not?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder
    How..? You can't get three rows at once, unless you use 3 different variables which isn't very practical.

    Or are you saying that for the sake of argument
    I didn't say it was practical or anything. And I have told you 3 times how to do it now. I was not trying to argue with you. You started to say it was not dynamicaly, and that is just PLAIN wrong. I am uing it at my site right now. And it HELL is dynamicaly... So I will not argue now. Kiwis has 3 difrent ways to do it. And I don't care what version he uses....
    Last edited by NoteMe; May 2nd, 2004 at 05:06 PM.

  16. #16
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Mine produces the nicest looking HTML at the end . Choose mine, choose mine .


    Posted by Chroder
    So you'd rather do it that way?
    was that aimed at me or Note .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  17. #17
    Originally posted by NoteMe
    And I have told you 3 times how to do it now.
    No you haven't

    You started to say it was not dynamicaly, and that is just PLAIN wrong.
    I was responding to your first example, which was not looping through a mysql result. Sorry if I worded it wrong.

    Why not?
    Your using more memory getting three values at once. Or using a loop within a loop is kinda weird -- the whole thing can be done with one variable, one loop. Why do more work then needed? I'm sure there are situations where you would need three values at once, to compare or something, but here it's not needed.

    But that's PHP for you, multiple ways to achieve the same result.
    Last edited by Chroder; May 2nd, 2004 at 03:50 PM.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I might prefer using more memory to performing a modulo operation every loop run though...

    Besides, I think PHP uses garbage collection, which would result in the total memory consumption probably not being any different.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    Depends on what you prefer, I'd agree running out of memory isn't really a problem. From a programmers standpoint though, I just find using one value, one loop, a lot easier to read then using three values or nested loops.

    Imagine if you have to work with those values, addslashes for example. You'd have 2 more extra lines, and if you used a nested loop, then you'd have to copy+paste.

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You've got a point.


    I'd have written it your way too, btw. Was just challenging your arguments, sometimes interesting things turn up.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21
    lol I see But I get the points of NoteMe and Electroman though, wans't trying to prove anyone wrong or anything. Hope it didn't appear that way

  22. #22
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder

    I was responding to your first example, which was not looping through a mysql result. Sorry if I worded it wrong.
    '


    I was tellin how to print the table. Not how to get the results from the query......He didn't ask how to do that, so I guess he figgured that out him self. Like you didn't provide the whole code for the whole page. And if you are so good in PHP, then why tripping on me not showing the WHOLE code.

  23. #23
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder
    lol I see But I get the points of NoteMe and Electroman though, wans't trying to prove anyone wrong or anything. Hope it didn't appear that way
    It's ok...just got a bit anoyed by your first posts. But it's ok....I had been struggeling with a CSS questions for 2,5 days, and was a bit tired, so what you did was just to push me over. No hard fealings...

    ØØ

  24. #24
    I was tellin how to print the table. Not how to get the results from the query......He didn't ask how to do that, so I guess he figgured that out him self.
    He asked how to print the results from a mysql into a table. I would assume he would need to loop through the results. But maybe he didn't.

    Like you didn't provide the whole code for the whole page. And if you are so good in PHP, then why tripping on me not showing the WHOLE code.
    I'm not tripping out. I haven't said anything. I don't know how your are reading my comments but I am not intending to say anything offensive, and I am certainly not "tripping out". Sorry if it sounds that way.

    It's ok...just got a bit anoyed by your first posts. But it's ok....I had been struggeling with a CSS questions for 2,5 days, and was a bit tired, so what you did was just to push me over. No hard fealings..
    Oh, ok. Sorry for pushing you

  25. #25
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Chroder

    I'm not tripping out. I haven't said anything. I don't know how your are reading my comments but I am not intending to say anything offensive, and I am certainly not "tripping out". Sorry if it sounds that way.

    Are you not going to quit soon. The thing was that I posted a solution, then you questioned that one, then I answered the solution to that one, and then you wuestioned that one too, and I answered that one too, and then you had all ready pushed me over.

    I am using this solution my self, and it works fine. If you have comments on my code, please let me know, but you don't need to question all my answers. Let the thread starter ask questions if there is something he doesn't understand.

    It works. Final word.

  26. #26
    You're over-reacting. One, I didn't believe that your solution helped solve the problem so I posted my own. You said something about putting the loop inbetween the table tags, all I said was you'd still need three values. You said that you could do that, I asked how, and this got started.

    I've appologized many times, if you don't want to accept it then fine. Be on your way.

  27. #27
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I have said that I accepted it and no hard fealings. And I have also answered all your questions on how to use my code. What more do you want?

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Can you please stop? It seems that both of you aren't in the right mood to discuss this.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  29. #29
    Nothing. Your messages sounded like open hostility towards me and I saw no apparent reason for it.

    Originally posted by CornedBee
    Can you please stop? It seems that both of you aren't in the right mood to discuss this.
    Gladly.

  30. #30
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Sweet I have said my final word 2 times. And you didn't stop. And then CornedBee said it once, and then you stop. Hehe...that was actually fun.

  31. #31
    My "final word" was before your "final word" and then you attacked me again.

    Think what you will, say what you will, this is truly my "final words".

  32. #32
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    lol , you to are like a pair of b*tches. Maybe better than when Note & Keda get in one of these. For proof search the forums for: Note, Keda, I, Am, Right, You ,Are, Wrong, Functional, Programming, Eat, This & lots of beeps and disallowed words .

    BTW I hope you didn't think I was tring to say your solutions were wrong by posting my own solution. I just saw the two of you already fighting and noticed that both your solutions would only work if the amount of records was divisable by 3. Although Chroder's would work the last row would be messed up.

    Now you two play nice you hear .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  33. #33
    Although Chroder's would work the last row would be messed up.
    Would it?

    Pretending we had 5 rows.

    ROW 1
    1 / 4, remainder not 0
    2 / 4, remainder not 0
    3 / 4, remainder not 0,

    ROW 2
    4 / 4, remainder 0 - new row
    5 / 4, remainder not 0

    Right? Never was good at these things so you gotta tell me

  34. #34
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    But the last row only has 2 cells in, yes? so it looks strange, its like normal apart from the last two colunms wouldn't be indented. hang on I'll make a screeny.....
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  35. #35
    Oh I get what you mean, I thought you meant there would be an extra <tr> or something. Browsers just display the one cell, unless it's against the standards. Never thought of it

  36. #36
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Ow right no, just that it wouldn't appear right. I tried to stop the screen shot but it was too late. I'll just post it anyway .
    Attached Images Attached Images  
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  37. #37
    Oh, I get you now. Though it's valid HTML (I think, couldn't find anything on W3), there still a blank whitespace

  38. #38
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Its even more fun when you have lots of rows and one has just one too many cells than it should, the whole thing has a chunk sticking out of it.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  39. #39
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    It was becuase of the "probelem" you guys are talking about now I said you should check how many results you had in the query before displaying it so you could handle this in the way you wanted....

Posting Permissions

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



Click Here to Expand Forum to Full Width