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???
Printable View
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???
so it's like this?
Code:RESULT1 RESULT2 RESULT3
RESULT4 RESULT5 RESULT6
.... and so on
YEAH
Here is an example I have used:
See if you understand it....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>
<?
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 % 4 == 0) // every 4th item, make a new table row
echo '</tr><tr>';
echo '<td>' . $row['mydata'] . '</td>';
}
echo '</tr></table>';
Quote:
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...;)
But each "revolution" of the loop doesn't bring out three values at once ;)
Quote:
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...;)
I don't know what you mean, why would you do that :confused:
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....;)
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 :p
How about this:
I've tested it and it works too :D.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;
} ?>
EDIT: Swaped round the AND statement
So you'd rather do it that way? :eek: :p
Why not?Quote:
Originally posted by Chroder
How..? You can't get three rows at once, unless you use 3 different variables which isn't very practical.
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....Quote:
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 :p
Mine produces the nicest looking HTML at the end ;). Choose mine, choose mine :D:D.
was that aimed at me or Note :confused:.Quote:
Posted by Chroder
So you'd rather do it that way?:eek: :p
No you haven't :)Quote:
Originally posted by NoteMe
And I have told you 3 times how to do it now.
I was responding to your first example, which was not looping through a mysql result. Sorry if I worded it wrong.Quote:
You started to say it was not dynamicaly, and that is just PLAIN wrong.
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.Quote:
Why not?
But that's PHP for you, multiple ways to achieve the same result.
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.
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.
You've got a point.
I'd have written it your way too, btw. Was just challenging your arguments, sometimes interesting things turn up.
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 :)
'Quote:
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.
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...;)Quote:
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 :)
ØØ
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.Quote:
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.
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.Quote:
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.
Oh, ok. Sorry for pushing you :pQuote:
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..
Quote:
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.
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. :rolleyes:
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?
Can you please stop? It seems that both of you aren't in the right mood to discuss this.
Nothing. Your messages sounded like open hostility towards me and I saw no apparent reason for it.
Gladly.Quote:
Originally posted by CornedBee
Can you please stop? It seems that both of you aren't in the right mood to discuss this.
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. :rolleyes:
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". :rolleyes:
lol :D:D, 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 :D.
Would it?Quote:
Although Chroder's would work the last row would be messed up.
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 :p
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.....
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 ;)
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 :).
http://www.vbforums.com/attachment.p...postid=1682100
Oh, I get you now. Though it's valid HTML (I think, couldn't find anything on W3), there still a blank whitespace :D
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.
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....;)