create html table WITHOUT pear
Posted by bschultz (bschultz), 8 October 2003my host doesn't have the table libraries of pear installed...so what function am I looking for to create an html table without it?
Everything I've found uses pear.
Posted by admin (Graham Ellis), 8 October 2003You don't need a function (for once !), you can simply print out your own <table> <tr> <td> </td> </tr> and </table> tags - it's how we do it on many of our web pages. If you want something slightly more sophisticated, you could write your own functions called cell, row and table.
Here's a table produced by print calls, by way of example:
Posted by bschultz (bschultz), 8 October 2003thanks, Graham.
One slight addition to the problem. I want to put a for loop in the table to display mysql table results. Inside each table cell (html) I want to step through the variables like this:
Make sense? Plus, I'd want to loop through the html row, not the column.
Posted by admin (Graham Ellis), 8 October 2003My example was just an illustration that put numbers into the cells - you can put anything in there, including the results of MySQL queries. Bear in mind that if the text includes characters like < and &, you'll need to also use htmlspecialchars to convert those into HTML-acceptable sequences.
The example loops through rows, and within rows through columns, and that's the order that a table is generated. If you data is available column by column not row by row, you'll need to make up an array of rows and use an operator such as .= to add to each row in turn, then once you have completed your array write a loop to output the table row by row.
Posted by bschultz (bschultz), 30 October 2003It's been a while since I've had time to work on this. Here's where I am now:
That does a row at a time...If I add to the <15 (say <30) it just puts them all in 1 row. I want 1-7 on 1 row, and 8-14 on the second row. I can't for the life of me find on php.net how to make 8-14 on row number 2, or how to add a third row in the future. I know that an if statement owuld handle the second row, but I'd like to write this for the future, too.
Can somebody give me a shove in the right direction? I've seen lots on how to step through the whole mysql table in an html table, but nothing on how to specify which mysql column goes in which html cell.
Thanks for the help...AS ALWAYS! So much more to learn.
Posted by admin (Graham Ellis), 30 October 2003You want to end one row and start the next by adding in </tr><tr> between the rows. In code, if you want to have 7 cells per row, you can include the following within your loop:
if (++$nc%7 == 0) print ("</tr><tr>");
Posted by bschultz (bschultz), 30 October 2003thanks, Graham...but that's producing a strange result.
to see what I mean.
The "202" just above the bigger table should be located in the top html row, farthest column on the right (blank cell right now).
Here's the php code that produces that:
Two questions: Why is it doing that? And how can I add in the athletes names to the table. I'm doing that in straight html right now. This is what it should look like
I manually created that table, and insert the echo statement into each html table cell manually. LOTS OF WORK!
Posted by admin (Graham Ellis), 30 October 2003You need to put the extra line that ends the row and starts a new row outside of the the code that starts and end a table cell; the extra line I gave you should be the very last thing in the loop. Otherwise you have bad HTML and I'm not suprised you get wacky results!
Posted by bschultz (bschultz), 2 November 2003where can I find documentation on the line:
if (++$nc%7 == 0) print ("</tr><tr>");
That did the trick, but I don't understand why.
If I have a nested loop, can I use (++$a%7 == 0) and define which variable I want to manipulate?
I'm trying this:
and it's displaying 11-189 in 1 row and then repeating the same for every player name.
Posted by admin (Graham Ellis), 2 November 2003The code you have posted will do exactly thge same thing whether or not you have the if (++$a%7 == 0) print ("</tr><tr>"); line ... in summary, you have
1. An outer loop going round with $b running from 250 to 276, each loop starting a new row and within the row
2. An inner loop going round from 11 to 189, each loop generating a table data cell.
Nothing in the inner loop uses the variable $b, so no matter what value $b happens to take you're producing identical resuits.
Also, you haven't balanced your open row <tr> with a close row </tr> at the end of the outer loop just before the final endfor, so your HTML will be 'malformed', and different browsers may display the results very differently.
OK - so what does THAT if (++$a%7 == 0) print ("</tr><tr>"); line do?
"Take the contents of the variable $a, add one to it, and divide by 7. If the REMAINDER is zero, do the print." In your latest example, $a always has the value 190 when this line is run as it's left that way at the end of your loop. $a becomes 191, which when divided by 7 gives a 27 remainder 2. 2 is not 0, so no print.
Posted by bschultz (bschultz), 2 November 2003Graham,
Sorry I've been such a pest, but I still have a hard time figuring out nested loops. I'm alright on a single loop...but...
One of the guys on my hosts forum ( http://forums.hmdns.net/ ) got me going in a differant direction, and we got it:
Posted by admin (Graham Ellis), 3 November 2003On our training courses, "loops within loops" is often the point at which trainees who are relatively new to programming need to pause for a breather; you're not alone in this .... glad to see that someone has provided the code that you needed.
There's a saying that I'll need help attributing: "Give a man a loaf, and you feed him for a day ... give him a seed and you'll feed him for ever". My answers on Opentalk are written, where practical, as seeds; I try to help posters learn how and why, rather than simply writing a bit of code to solve the immediate problem; sometimes this needs a bit more effort and takes a bit longer than just throwing out a loaf of bread. But I also appreciate that if a problem needs an urgent solution, with customers screaming for attention, a loaf of bread can be an excellent short-term fix
I see that you're now looking to add an extra column (season averages) onto the end of each row, and ask "Would I have to add a third loop". No, you wouldn't; this is something you'll be doing once per row, and so it should be placed in your already existing outer loop ... towards the bottom but before you output the </tr>.
Posted by bschultz (bschultz), 3 November 2003Thanks Graham...I'm not starving right now. No need for the whole loaf yet!
Wouldn't though, I need that third loop. Yes, it's one per row, but if it's not a loop, it will display the season averages for the first player, for EVERY player. I need divided1 and divided2 for the first player, 3 and 4 for the second player, etc.
gives me the 7 rows of total raw numbers to the right of the player names...but then would the averages loop be something like this:
I think this is the loop structure that I need:
create player name, then wait for 2nd and 3rd loops to do their thing.
create the seven columns of raw total stats, then wait.
create 2 rows of average stats, end the row, go back to loop #1
Does that look right to you? Yes my head is spinning right now...
Posted by admin (Graham Ellis), 4 November 2003You might find it helpful to draw yourself a simple flowchart on a sheet of paper ... and then covert that into code. Each time you have a line going back up on your flowchart, it's a loop! I sometimes find it useful to write "pseudocode" too - where the code structure can be planned without getting bogged down in the detail.
Also - a further tip - the number of loops you have really doesn't matter - it's how they're nested that's important - whether one ends before the next starts, or whether one is enclosed in another. Here's what I think your pseudocode might look like:
Posted by bschultz (bschultz), 4 November 2003I GOT IT!
Graham, thank you so much...!!!
Posted by bschultz (bschultz), 4 December 2003I'm going to combine a couple of threads here. A while ago, I had asked about a loop in the mysql forum on adding up the value of ALL columns named T1:
Here's that code (with the loop that works):
Now, I realized that I can combine two problems...and hopefully get one solution.
Using the html table loop above (previous post), I'm echoing "overall_sum10". In the above code (this post), the overall_sum is now in an array. So, I need $overall_sum.
is the code that steps through the overall sums. But I'm trying to loop through the newly formed array with something like this:
And it isn't working. Am I on the right path?
Posted by admin (Graham Ellis), 6 December 2003You can simply loop through the array indexes, and replace any references you would make to your made-up variable containing the name of another variable with the actual array element reference.
Hmm. Much easier to give you an example than to say! Here's an example - a loop that you might add just before you output your </table> tab to output a row of table data cells to report the column totals:
Posted by bschultz (bschultz), 6 December 2003Graham,
I've tried that:
but I get a parse error on this line:
Posted by admin (Graham Ellis), 6 December 2003The question you posted yesterday talks about (and uses) a variable called $overall_sum, but the code you have just posted and you used earlier in this thread used a whole lot of individual variables with names like $overall_sum10. There is quite a difference!
Making up one variable name in another does work in PHP but it is a bit of a strange and unusual practice. I *am* aware that it's something you picked up from another thread here and it's working for you, but I stick with my advise in my previous post and suggest that you switch to using arrays so that the notation you use will be of the form $overall_sum[$k].
The immediate cause of your syntax error is that the dot operator joins strings. The item you have to the left of the dot is a string, but the [$a] to the right is not - you would need to write it in double quotes - "[$a]" and that line would then work. You will also need to sort out other issues too - like you seem to be genarating 28 <tr> tags in a loop, but only one </tr> tag outside the loop.
Hope this helps .... just wish I could do a live demo with a whiteboard and build up an example to show you this, but you're a long way from us in Melksham. I'll be in California and probably Florida too in the next couple of months, and visit the DC area from time to time - but I think you're a very long way from all of those ....
Posted by bschultz (bschultz), 6 December 2003You're right, I am several thousand miles away from all of those places.
If I just print out "$overall_sum" (separate from the loop in question) it does work, but not if I print out "$overall_sum[$a]" inside the loop. All I get then is a blank table cell.
The last line in that code will print out what I want...but nothing inside the html table shows up.
As for the lack of closing </tr>'s, I thought I read that they aren't needed. Maybe I am mistaken.
Posted by admin (Graham Ellis), 7 December 2003I think you'll find it works better if your replace
Which is easier, cleaner, and more conventional ... as well as copying the code that you say does work outside the loop
As to the </tr> ... yes, it's usually OK without but it's better to close tags that you open - if you don't you could get some browser specific problems and your code will be rejected by various HTML standards checkers. It's also a good idea to be consistent, and your code as posted has a </tr> tag for the last row, but not for any other. Anyway - it's your choice; I'm just telling you why I advised as I did!
Posted by bschultz (bschultz), 8 December 2003That did it.
Cut 3058 lines down to 101!
Thanks (as always!)
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho