jesushax Posted July 9, 2008 Share Posted July 9, 2008 im having trouble with my table code i have the letter grouping sorted but my table is only printing two records per row and ive told it to do 3 and i need a way to insert blank cells when the records dont run up to 3 heres what i have so far <?php include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/header.php'); echo '<p>Browse & print company details by clicking a company name below</p><a name="top">'."\n"; echo '<p>'."\n"; for ($x=65; $x<=90; $x++) { echo '<a href="#'.chr($x).'">'.chr($x)."</a>"; if ($x < 90) { echo " | "; } } echo '</p>'."\n"; $res = mysql_query("SELECT `CompanyName`,`CompanyID` FROM `tblDirectory` ORDER BY `CompanyName` ASC") or die(mysql_error()); $col = 1; $colMax = 3; echo "<table border=\"0\">\n"; while($row=mysql_fetch_array($res)) { $strCurLetter = substr($row["CompanyName"],0, 1); if ($strCurLetter!=$strLastLetter) { echo '<tr><td style="height:25px;" colspan="'.$colMax.'">'."\n"; echo '<strong>'.$strCurLetter.'</strong>'."\n"; echo '<a name="'.strtoupper($strCurLetter).'"></a> <a href="#top"><small>Back To Top</small></a><hr />'."\n"; echo '</td></tr>'."\n"; } if ($col == 1) {echo " <tr>\n"; } echo '<td><a class="smallG" href="record.php?ID='.$row["CompanyID"].'">'.$row["CompanyName"].'</a></td>'."\n"; $col++; if ($col == $colMax) { echo " </tr>\n"; $col = 1; } $strLastLetter = $strCurLetter; } echo "</table>\n"; ?> <?php include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/footer.php'); ?> Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/ Share on other sites More sharing options...
jesushax Posted July 9, 2008 Author Share Posted July 9, 2008 can no one help me out here? im sure theres alot of you out there that know more than me :| thanks Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585322 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 give us some sample rows from the database and the desired final output Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585325 Share on other sites More sharing options...
jesushax Posted July 9, 2008 Author Share Posted July 9, 2008 heres exactly what it outputs as you see, my colmax is 3 but its only outputting 2 columns and i need to know how to insert blank cells for the rows that dont reach 3 columns Thanks <p> <a href="#A">A</a> | <a href="#B">B</a> | <a href="#C">C</a> | <a href="#D">D</a> | <a href="#E">E</a> | <a href="#F">F</a> | <a href="#G">G</a> | <a href="#H">H</a> | <a href="#I">I</a> | <a href="#J">J</a> | <a href="#K">K</a> | <a href="#L">L</a> | <a href="#M">M</a> | <a href="#N">N</a> | <a href="#O">O</a> | <a href="#P">P</a> | <a href="#Q">Q</a> | <a href="#R">R</a> | <a href="#S">S</a> | <a href="#T">T</a> | <a href="#U">U</a> | <a href="#V">V</a> | <a href="#W">W</a> | <a href="#X">X</a> | <a href="#Y">Y</a> | <a href="#Z">Z</a></p> <table border="0"> <tr><td style="height:25px;" colspan="3"> <strong>A</strong> <a name="A"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=65">A & J Joinery Services</a></td> <td><a class="smallG" href="record.php?ID=38">A. M. Hardy Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=28">Abbey Electrical Services</a></td> <td><a class="smallG" href="record.php?ID=36">AJC Developments</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=46">Applewood Joiners & Builders Ltd</a></td> <td><a class="smallG" href="record.php?ID=14">Arnott Plant Hire Ltd</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>B</strong> <a name="B"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=25">Bathroom & Ceramic Creations Ltd</a></td> <td><a class="smallG" href="record.php?ID=7">Binks Building Services</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=40">Blaydes Decorating</a></td> <td><a class="smallG" href="record.php?ID=34">Bridgewood Contracts Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=6">Byron Roofing (Hull) Ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>C</strong> <a name="C"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=16">C.E. Pittawat & Son Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=74">Clark Electrical UK Limited</a></td> <td><a class="smallG" href="record.php?ID=75">Clarkes Environmental Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=4">Classical Gas Ltd</a></td> <td><a class="smallG" href="record.php?ID=58">Concept Technical Services</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=11">Coniston Services Ltd</a></td> <td><a class="smallG" href="record.php?ID=66">Constables</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=53">Contract Flooring & Interiors Ltd</a></td> <td><a class="smallG" href="record.php?ID=77">Core Electrical Services Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=22">Corner2Corner</a></td> <tr><td style="height:25px;" colspan="3"> <strong>D</strong> <a name="D"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=2">Danlex Services Ltd t/a Barkers Contractors</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=45">Design & Build Property Development</a></td> <td><a class="smallG" href="record.php?ID=64">Direct Property Services</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=55">DLS (Hull) LTD </a></td> <tr><td style="height:25px;" colspan="3"> <strong>E</strong> <a name="E"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=62">East Yorkshire Tiliing Co</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=80">Edifice Management Ltd</a></td> <td><a class="smallG" href="record.php?ID=30">Excel Electrical</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>F</strong> <a name="F"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=47">Fast React Plumbing</a></td> <td><a class="smallG" href="record.php?ID=19">Finesse</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=79">Floors 2 Go</a></td> <tr><td style="height:25px;" colspan="3"> <strong>G</strong> <a name="G"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=69">Green Build Solutions Ltd</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>H</strong> <a name="H"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=20">HD Decorating Services Ltd</a></td> <td><a class="smallG" href="record.php?ID=51">Hessle Interiors</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=1">Hodgsons & Sons Developments Ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>J</strong> <a name="J"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=78">J & H Building Services Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=57">J P M Plumbing & Property Repairs</a></td> <td><a class="smallG" href="record.php?ID=44">J R Wright Decorators</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=10">JE Builders</a></td> <td><a class="smallG" href="record.php?ID=37">Jeffery Hirscher</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=9">Johnson The Decorator</a></td> <td><a class="smallG" href="record.php?ID=12">JP Developers Limited</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>K</strong> <a name="K"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=41">Kinetic Concepts Limited</a></td> <td><a class="smallG" href="record.php?ID=63">Kingstown Works Limited</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>M</strong> <a name="M"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=48">M J Cuthbert Building Services Ltd</a></td> <td><a class="smallG" href="record.php?ID=23">M.B.Roche & Sons Limited</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=52">Midas Homes Humberside Ltd</a></td> <td><a class="smallG" href="record.php?ID=29">Mitchell James Contractors</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>N</strong> <a name="N"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=3">Natural Gas services Ltd</a></td> <td><a class="smallG" href="record.php?ID=21">Nortech Services Ltd</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>O</strong> <a name="O"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=56">Overment Electrical Contractors ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>P</strong> <a name="P"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=24">PAGE Builders Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=39">PAS Portable Accomodation Serices LTD</a></td> <td><a class="smallG" href="record.php?ID=32">Pheonix Gas and Plumbing Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=26">Phoenix gas and plumbing ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>R</strong> <a name="R"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=81">R Farley</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=35">Rawfield Uk Ltd</a></td> <td><a class="smallG" href="record.php?ID=42">Reliance Electrical Ltd.</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=59">Richard Wordsworth Ltd</a></td> <td><a class="smallG" href="record.php?ID=82">Rok Building Limited</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=76">Romar Construction Ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>S</strong> <a name="S"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=18">Sangiwn Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=50">Sapphie Construction (hull) Ltd</a></td> <td><a class="smallG" href="record.php?ID=17">Screed Flow Ltd</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=13">Services Seven Ltd</a></td> <td><a class="smallG" href="record.php?ID=60">Shaun Fitzgerald Building Services</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=43">Shire Property Services Ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>T</strong> <a name="T"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=15">T.M.L. Engineering</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=61">The Hull Flooring Company ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>U</strong> <a name="U"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=67">UBS Hull (Ltd)</a></td> </tr> <tr> <td><a class="smallG" href="record.php?ID=5">Universal Lifting & Moving Services Ltd</a></td> <tr><td style="height:25px;" colspan="3"> <strong>V</strong> <a name="V"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <td><a class="smallG" href="record.php?ID=49">Vale Developments Cotractors Ltd</a></td> </tr> <tr><td style="height:25px;" colspan="3"> <strong>W</strong> <a name="W"></a> <a href="#top"><small>Back To Top</small></a><hr /> </td></tr> <tr> <td><a class="smallG" href="record.php?ID=33">Westland Shopfitters</a></td> </table> Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585334 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 try moving the $col++ instead of if ($col == 1) {echo " <tr>\n"; } echo '<td><a class="smallG" href="record.php?ID='.$row["CompanyID"].'">'.$row["CompanyName"].'</a></td>'."\n"; $col++; if ($col == $colMax) { echo " </tr>\n"; $col = 1; } do if ($col == 1) {echo " <tr>\n"; } echo '<td><a class="smallG" href="record.php?ID='.$row["CompanyID"].'">'.$row["CompanyName"].'</a></td>'."\n"; if ($col == $colMax) { echo " </tr>\n"; $col = 1; }else { $col++; } when you hit column 2, you are applying ++ (making it == 3), then testing to see if it is equal to colMax... which it is, so it closes the <tr>. You need to test the column number, then increment it. You need to encase it in the else statement so that when $col is reset to 1, $col++ doesn't bump it up to 2. Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585370 Share on other sites More sharing options...
jesushax Posted July 9, 2008 Author Share Posted July 9, 2008 hey thanks, that did the trick now for rows that dont enough records to make 3 so i need some sort of if loop? that will check if the current row has 3 columns in it if it does not then insert some blank <td> </td> any ideas? Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585391 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 try playing around with str_repeat in the first if statement. <?php if ($strCurLetter!=$strLastLetter) { if($col != 1){ echo str_repeat('<td> </td>',4-$col).'</tr>'; $col = 1; } echo '<tr><td style="height:25px;" colspan="'.$colMax.'">'."\n"; echo '<strong>'.$strCurLetter.'</strong>'."\n"; echo '<a name="'.strtoupper($strCurLetter).'"></a> <a href="#top"><small>Back To Top</small></a><hr />'."\n"; echo '</td></tr>'."\n"; } ?> so if the next letter is reached (triggering the if statement), the previous row would still be have an unclosed <tr> tag. So we figure out how many more cells we need to add to make it 3 (4-$col), echo them out and the closing <tr> tag. Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585432 Share on other sites More sharing options...
jesushax Posted July 9, 2008 Author Share Posted July 9, 2008 thanks alot much appreciated EDIT:: Spoke to soon, the code i just added doesnt count for the last record, if the last record has been reached we dont check to see if the table row has been completed becuase its in the next records loop Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585441 Share on other sites More sharing options...
mbeals Posted July 9, 2008 Share Posted July 9, 2008 then after the while loop ends put the same thing that is in the if statement <?php while($row = mysql_fetch_array(){ do a bunch of stuff } if($col != 1){ echo str_repeat('<td> </td>',4-$col).'</tr>'; $col = 1; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585450 Share on other sites More sharing options...
jesushax Posted July 9, 2008 Author Share Posted July 9, 2008 thankyou VERY VERY MUCH Link to comment https://forums.phpfreaks.com/topic/113889-solved-help-with-my-table-code/#findComment-585462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.