Jump to content

Retrieving data from MySQL to table


Deks

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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++;

}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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');
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.