Deks Posted October 7, 2011 Share Posted October 7, 2011 Hello, can someone help me with my proggraming problem please. Am new in this so i still have lots problems. I need to put all data from my MySQL table in one table in html. There is catch... i need to design output to give me table with x rows and 3 columns, so it means while reading data from database it need put every 4th in new row. And if there isnt 3 data (or better say if there isnt 3 full <td>) in row code will automatically add empty <td> or 2 in table. So...did somebody do this before or have some code ? All i got for now is $td ='<td align="center"><img src="'.$row['name'].'.png" /> <p style="font-family:Verdana, Geneva, sans-serif; font-size:15px"> Number: <b style="font-size:40px;">'.$row['serial'].'</b></p> <br><img width="90" height="29" src="'.$logo.'"/></td>'; $tr ='<tr>'.$td.'</tr>'; $html='<table border="1" align="center">'.$tr.'</table>'; Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/ Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 The simplest way is to create a counter for when you loop through the data. Here is a VERY simple example $data = array(1,2,3,4,5,6,7,8,9,10); echo '<table border="1" cellpadding="10"> <tr>'; $count = 0; // Looping the data foreach ($data as $value) { // if the count is 3, close the row, create a new one and reset $count to 0 if ($count == 3) { echo '</tr><tr>'; $count = 0; } // Output the data in a td and increase the count echo "<td>{$value}</td>"; $count++; } // check to see if the last count was 3 if it was then its all good otherwise it isnt if ($count < 3) { echo "<td colspan=\"{(3-$count)}\"> </td>"; } echo '</tr> </table>'; Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276693 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Hi, thanks for reply.. I ll try it but i think it wont work because i need to implement this code in TCPDF template. Sorry, my bad i didnt put that my problem description. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276694 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 That code is just an example of a way for your question to be achieved. Nothing is stopping you from writing it to a variable and passing it into TCPDF Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276696 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Yes true. Thanks lot for giving me example code. I hope TCPDF wont be harsh and it will exept some version of this code. Thanks man u help me lot. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276699 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 TCPDF is pretty forgiving, if it doesnt like something it usually ignores it. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276700 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Yes. I tasted him But i also know that he wont do something in html if tags are not closed. In your example u dont close table? Its like auto close? BTW if you are familiar with TCPDF can u advice me what command should i use for writing html. WriteHTMLCell, WriteHTML or some other maybe? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276708 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Either should be fine, it really depends on what your outcome is to be. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276711 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Well i make this is PHP : $logo ='QRcodovi/APPlogo.png'; echo '<table border="1" align="center"><tr>'; $count = 1; $result = mysql_query("SELECT * FROM custom"); while($row = mysql_fetch_array($result)){ if($count == 4){ $count = 1; echo '</tr><tr>'; } echo '<td align="center"><img src="QR/'.$row['name'].'.png"/> <p style="font-family:Verdana, Geneva, sans-serif; font-size:15px"> Inv.br: <b style="font-size:40px;">'.$row['name'].'</b></p> <br><img width="90" height="29" src="'.$logo.'"/></td>'; $count++; } But am not sure how to use it in TCPDF with using $pdf->writeHTML($string, true, false, true, false, ''); Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276724 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Rewrite echo on this line echo '<table border="1" align="center"><tr>'; to $string = '<table border="1" align="center"><tr>'; Then with all the following echo's change them to $string .= This will append each of the strings onto $string. $logo ='QRcodovi/APPlogo.png'; $string = '<table border="1" align="center"><tr>'; $count = 1; $result = mysql_query("SELECT * FROM custom"); while($row = mysql_fetch_array($result)){ if($count == 4){ $count = 1; $string .= '</tr><tr>'; } $string .= '<td align="center"><img src="QR/'.$row['name'].'.png"/> <p style="font-family:Verdana, Geneva, sans-serif; font-size:15px"> Inv.br: <b style="font-size:40px;">'.$row['name'].'</b></p> <br><img width="90" height="29" src="'.$logo.'"/></td>'; $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276726 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Thanks man , you are the best. Now i only need to put to add me new page after 9 datas and put some designs and it will be perfect. Thanks lot man! Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276727 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 If you need anymore help you know where to go (as bad as that sounds) Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276728 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Dunno did i understood good but don't worry i know how to get my final code. My only problem was TCPDF who is harsh on me all time Thanks for helping man. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276733 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Sorry for bothering but am stuck again. Am trying to put on each page 9 datas, but problem is that my <table> is in front of while function(i think thats main problem). $logo ='QRcodovi/APPlogo.png'; $table = '<table border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; $count_td = 1; $count_page = 1; $result = mysql_query("SELECT * FROM custom"); while($row = mysql_fetch_array($result)){ if($count_td == 4){ $count_td = 1; $table .= '</tr><tr>'; } if($count_page == 10){ //after each 9 data new page need to be created and 10th data placed on new page $count_page = 1; $pdf->AddPage('P', 'A5'); } $table .= '<td align="center"><img src="QR/'.$row['name'].'.png"/> <p style="font-family:Verdana, Geneva, sans-serif; font-size:15px"> Inv.br: <b style="font-size:40px;">'.$count_page.'</b></p> // testing counter <br><img width="90" height="29" src="'.$logo.'"/></td>'; $count_td++; $count_page++; } $pdf->writeHTML($table, true, false, true, false, ''); Can it be solved on this way? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276770 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 You can, when you add the new page. You can close the existing table and open up a new one. SHOULD work if($count_page == 10){ //after each 9 data new page need to be created and 10th data placed on new page $count_page = 1; $table.= '</tr></table><table border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; $pdf->AddPage('P', 'A5'); } Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276774 Share on other sites More sharing options...
Deks Posted October 7, 2011 Author Share Posted October 7, 2011 Sorry but this code doesnt work... lots of errors. Code i have gave writes pdf but he puts first page blank and then all data puts on second page.... Think there is something missing here.. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276785 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Post the code here and we can have a look. Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1276788 Share on other sites More sharing options...
Deks Posted October 8, 2011 Author Share Posted October 8, 2011 Hi, here is my whole php code for TCPDF template. <?php require_once('../../php/tcpdf/config/lang/eng.php'); require_once('../../php/tcpdf/tcpdf.php'); //connection to database include "connect.php"; // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); //set auto page breaks $pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM); // set document information //$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // set default header data //$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING); // set header and footer fonts //$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); //$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // remove default header/footer $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); // set default monospaced font //$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); //set margins //$pdf->SetMargins(0, PDF_MARGIN_LEFT, 0); //$pdf->SetMargins(20, PDF_MARGIN_TOP, 20); $pdf->SetMargins($left=0, $top=0, $right=0, $keepmargins=false); //set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set some language-dependent strings $pdf->setLanguageArray($l); // set font //$pdf->SetFont('helvetica', '', 10); //---------------------------------------------------------- // add a page $pdf->AddPage('P', 'A5'); //$pdf->Cell(0, 0, 'A5 PORTRAIT', 1, 1, 'C'); //=========================================================== $logo ='QR/APPlogo.png'; $table = '<table width="285" border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; //counters $count_td = 1; $count_page = 1; //retrieve data from database $result = mysql_query("SELECT * FROM custombtn"); while($row = mysql_fetch_array($result)){ if($count_td == 4){ //after each 3 data start new row $count_td = 1; $table .= '</tr><tr>'; } if($count_page == 10){ //after each 9 data new page need to be created and 10th data placed on new page--doesnt work $count_page = 1; $table .= '</tr></table><table width="285" border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; $pdf->AddPage('P', 'A5'); } $table .= '<td width="95" align="center"><img src="QR/'.$row['name'].'.png"/> <p style="font-family:Verdana, Geneva, sans-serif; font-size:15px"> Inv.br: <b style="font-size:40px;">'.$count_page.'</b></p> <br><img width="90" height="29" src="'.$logo.'"/></td>'; $count_td++; $count_page++; } $pdf->writeHTML($table, true, false, true, false, ''); $pdf->lastPage(); $pdf->Output('PDFTemplate.pdf', 'I'); ?> I little adjust folders from before (put all to QR), it work when i dont put IF function for new page. So.. i tried with : if($count_page == 10){ //after each 9 data new page need to be created and 10th data placed on new page--doesnt work $count_page = 1; $table .= '</tr></table><table width="285" border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; $pdf->AddPage('P', 'A5'); } and also.... if($count_page == 10){ //after each 9 data new page need to be created and 10th data placed on new page--doesnt work $count_page = 1; $table .= '</tr></table>'; $pdf->AddPage('P', 'A5'); $table .='<table width="285" border="0" cellpadding="0" cellspacing="10" align="center"><tr>'; } but...no effects, code for creating new page after each 9 data and placing next data to new page doesnt work. Sorry for bothering and thanks for helping. Regards Quote Link to comment https://forums.phpfreaks.com/topic/248611-retrieving-data-from-mysql-to-table/#findComment-1277184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.