Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
create html table WITHOUT pear

Posted by bschultz (bschultz), 8 October 2003
my 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.

THanks.

Posted by admin (Graham Ellis), 8 October 2003
You 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:

Code:
print ("<table border=1>");
for ($k=10; $k<=100; $k+= 10) {
    print ("<tr>");
         for ($j=1; $j<6; $j++) {
                 print ("<td>".$k*$j."</td>");
          }
    print ("</tr>");
     }
print ("</table>");


Posted by bschultz (bschultz), 8 October 2003
thanks, 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:

Code:
for($a = 1; $a < 270; $a++) {

echo "$overall_sum[$i]";
}


Make sense?  Plus, I'd want to loop through the html row, not the column.



Posted by admin (Graham Ellis), 8 October 2003
My 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 2003
It's been a while since I've had time to work on this.  Here's where I am now:

Code:
 <tr><td><font size='2'>Player 1</font></td>
<?php

for($a = 10; $a < 15; $a++) {

$varname = "overall_sum" . $a;
echo "<td><div align=\"center\">";
if ($$varname)
print $$varname;
echo "</div></td>";
}
?>
</tr>


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.

Brian

Posted by admin (Graham Ellis), 30 October 2003
You 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 2003
thanks, Graham...but that's producing a strange result.

visit http://www.brainerdwarriors.org/statstest.php

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:

Code:
<table border="2" cellpadding="2" width="70%" style="border-collapse: collapse" bordercolor="#111111" cellspacing="0">



<?php
for($a = 10; $a < 110; $a++) {
$varname = "overall_sum" . $a;
echo "<td><div align=\"center\">";
if (++$nc%5 == 0) print ("</tr><tr>");
if ($$varname)
print $$varname;
echo "</div></td>";
}
?>
</tr>


</table>


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

http://www.brainerdwarriors.org/sports/bsoccer/0304/stats.php

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 2003
You 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 2003
where 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:

Code:
<?
for ($b = 250; $b < 277; $b++):
 
   echo "<tr>";

       echo "<td>" . ${"Player$b"} . "</td>";

       for ($a = 11; $a < 190; $a++):
       echo "<td>";

       if  (${"overall_sum$a"}):
           echo ${"overall_sum$a"};
       else:
                       echo "-";
       endif;

       echo "</td>";
   endfor;

    if (++$a%7 == 0) print ("</tr><tr>");
   
endfor;


?>


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 2003
The 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 2003
Graham,

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:

Code:
for ($b = 250; $b < 277; $b++) {
   echo "<tr>";
   echo "<td>" . ${"Player$b"} . "</td>";
   $startaval = ($b - 250) * 7 + 1;
   $endaval = $startaval + 7;
   for($a = $startaval; $a < $endaval; $a++) {
       $varname = "overall_sum" . $a;
       echo "<td><div align=\"center\">"; if ($$varname)
       print $$varname;
       echo "</div></td>";
   }
   print ("</tr>");
}



Thanks Graham!

Posted by admin (Graham Ellis), 3 November 2003
On 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 2003
Thanks 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.

This line:
Code:
$startaval = ($b - 250) * 7 + 1;


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:

Code:
startavg = ($b +1)


I think this is the loop structure that I need:

first loop
  create player name, then wait for 2nd and 3rd loops to do their thing.

second loop
  create the seven columns of raw total stats, then wait.

third loop
   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 2003
You 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:

Code:
output table start
[Extra code to write out column titles could go here]

for each row {
       output <tr>
       output left hand label cell
       set running total to zero
       for each data column {
               output each data cell
               add data cell value to running total
               }
       calculate average from running total divided by data cell count
       output avearge cell
       output </tr>
       }

output table end


Posted by bschultz (bschultz), 4 November 2003
I GOT IT!  

Code:
<?
for ($b = 250; $b < 277; $b++) {
   echo "<tr>";
   echo "<td>" . ${"Player$b"} . "</td>";
   $startaval = ($b - 250) * 7 + 1;
   $endaval = $startaval + 7;
   for($a = $startaval; $a < $endaval; $a++) {
       $varname = "overall_sum" . $a;
       echo "<td><div align=\"center\">"; if ($$varname)
       print $$varname;
       echo "</div></td>";
       }
       
       $startavg = ($b - 250) * 2 + 1;
   $endavg = $startavg + 2;
   for($c = $startavg; $c < $endavg; $c++) {
       $varname1 = "divided" . $c;
       echo "<td><div align=\"center\">"; if ($$varname1)
       print $$varname1;
       echo "</div></td>";
   }
   print ("</tr>");
}


?>


Graham, thank you so much...!!!



Posted by bschultz (bschultz), 4 December 2003
I'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):
Code:
for ($colnum =1; $colnum <=100; $colnum++) {
    $column_name = "T".$colnum;

$overall_sum[$colnum] = 0;
foreach ($tablenames as $table) {
$query = "SELECT SUM(".$column_name.") as sum FROM ".$table." ";
@$result_array = mysql_fetch_array(mysql_query($query));
$overall_sum[$colnum] += $result_array[sum];

}
}



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[10].  

Code:
  $varname = "overall_sum" . $a;


is the code that steps through the overall sums.  But I'm trying to loop through the newly formed array with something like this:

Code:
  $varname = "overall_sum" . [$a];


And it isn't working.  Am I on the right path?


Posted by admin (Graham Ellis), 6 December 2003
You 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:

Code:
print ("<tr>");
for ($colnum = 1; $colnum <= 100; $colnum++) {
       print ("<td>$overall_sum[$colnum]</td>");    
}
print ("</tr>");



Posted by bschultz (bschultz), 6 December 2003
Graham,

I've tried that:

Code:
for ($b = 250; $b < 277; $b++) {
   echo "<tr>";
   echo "<td>" . ${"Player$b"} . "</td>";
   $startaval = ($b - 250) * 7 + 1;
   $endaval = $startaval + 7;
   for($a = $startaval; $a < $endaval; $a++) {
  $varname = "overall_sum".[$a];
  echo "<td><div align=\"center\">"; if ($$varname)
  print $$varname;
  echo "</div></td>";
  }



}
   print ("</tr></table>");


but I get a parse error on this line:

Code:
 $varname = "overall_sum".[$a];


Posted by admin (Graham Ellis), 6 December 2003
The 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 2003
You're right, I am several thousand miles away from all of those places.

If I just print out "$overall_sum[2]" (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.

Code:
for ($b = 250; $b < 277; $b++) {
   echo "<tr>";
   echo "<td>" . ${"Player$b"} . "</td>";
   $startaval = ($b - 250) * 7 + 1;
   $endaval = $startaval + 7;
   for($a = $startaval; $a < $endaval; $a++) {
  $varname = "overall_sum . '[$a]'";
  echo "<td><div align=\"center\">"; if ($$varname)
  print $$varname;
  echo "</div></td>";
  }



}
   print ("</tr></table>");

print $overall_sum[2];


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 2003
I think you'll find it works better if your replace

Code:
 $varname = "overall_sum . '[$a]'";
   echo "<td><div align=\"center\">"; if ($$varname)
   print $$varname;


by

Code:
   echo "<td><div align=\"center\">"; if ($overall_sum[$a]);
   print $overall_sum[$a];


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 2003
That did it.

Cut 3058 lines down to 101!  

Thanks (as always!)



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho