PHP nOOb seek help from ancient PHP masters...
Posted: Sat Feb 22, 2003 2:14 pm
[long post starts here]
This post is about changing an ODBC/PHP script to work in a PHP/MySQL environment...
A while back I picked up a script to print PDF labels with PHP/MySQL using a great class which I found at http://www.fpdf.org
The original script was for a PHP/ODBC situation - this is the script I found;
I wanted to change this so that it would work with PHP/MySQL - I've changed it to look like this;
In my typical newbieness style I find I'm getting an error straight away on line 7 (iow - I haven't made past first base!).
Here's the error;
If you've gotten this far thanks for reading - I hope someone can help me get to line 8..!
kamm...
[/long post ends here]
This post is about changing an ODBC/PHP script to work in a PHP/MySQL environment...
A while back I picked up a script to print PDF labels with PHP/MySQL using a great class which I found at http://www.fpdf.org
The original script was for a PHP/ODBC situation - this is the script I found;
Code: Select all
<?php
function odbc_fetch_array($res) {
$row = array();
$result = array();
if ($result = odbc_fetch_row($res)) {
$nf = odbc_num_fields($res)+1;
for($count=1; $count < $nf; $count++) {
$field_name = odbc_field_name($res, $count);
$field_value = odbc_result($res, $count);
$rowї$field_name] = $field_value;
}
return $row;
}
}
// Prints to an Avery 5160 label sheet which is a label
// 2 5/8" wide by 1" tall.
function Avery5160($x, // X co-ord of label (0-2)
$y, // Y co-ord of label (0-9)
&$pdf,
$Data) // String w/ line breaks to print
{
$LeftMargin = 4.2;
$TopMargin = 12.7;
$LabelWidth = 66.6;
$LabelHeight = 25.45;
// Create Co-Ords of Upper left of the Label
$AbsX = $LeftMargin + (($LabelWidth + 4.22) * $x);
$AbsY = $TopMargin + ($LabelHeight * $y);
// Fudge the Start 3mm inside the label to avoid alignment errors
$pdf->SetXY($AbsX+3,$AbsY+3);
$pdf->MultiCell($LabelWidth-8,4.5,$Data);
return;
}
function PrintAddressLabels($SelectStmt)
{
global $cnx; // database conneciton in odbcinc.php
$pdf=new FPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
$cur = odbc_Exec($cnx,$SelectStmt);
if (!$cur) {
echo "Database Error";
return;
}
$x = 0;
$y = 0;
while (TRUE) {
if ($row = odbc_fetch_array($cur) ) {
$LabelText = sprintf("%s\n%s\n%s, %s, %s",
$rowї'MailName'],
$rowї'Address'],
$rowї'City'],$rowї'State'],$rowї'Zip']);
Avery5160($x,$y,$pdf,$LabelText);
$y++; // next row
if ($y == 10 ) { // end of page wrap to next column
$x++;
$y = 0;
if ($x == 3 ) { // end of page
$x = 0;
$y = 0;
$pdf->AddPage();
}
}
} else {
// Error quit printing
break;
}
}
$pdf->Output();
}
?>I wanted to change this so that it would work with PHP/MySQL - I've changed it to look like this;
Code: Select all
<?php
require('../fpdf.php');
define('FPDF_FONTPATH','font/');
include ('db.php');
include ('error.php');
function mysql_fetch_array($res) {
$row = array();
$result = array();
if ($result = mysql_fetch_row($res)) {
$nf = mysql_num_fields($res)+1;
for($count=1; $count < $nf; $count++) {
$field_name = mysql_field_name($res, $count);
$field_value = mysql_result($res, $count);
$rowї$field_name] = $field_value;
}
return $row;
}
}
// Prints to an Avery 5160 label sheet which is a label
// 2 5/8" wide by 1" tall.
function Avery5160($x, // X co-ord of label (0-2)
$y, // Y co-ord of label (0-9)
&$pdf,
$Data) // String w/ line breaks to print
{
$LeftMargin = 4.2;
$TopMargin = 12.7;
$LabelWidth = 66.6;
$LabelHeight = 25.45;
// Create Co-Ords of Upper left of the Label
$AbsX = $LeftMargin + (($LabelWidth + 4.22) * $x);
$AbsY = $TopMargin + ($LabelHeight * $y);
// Fudge the Start 3mm inside the label to avoid alignment errors
$pdf->SetXY($AbsX+3,$AbsY+3);
$pdf->MultiCell($LabelWidth-8,4.5,$Data);
return;
}
function PrintAddressLabels($SelectStmt)
{
//global $cnx; // database conneciton in mysqlinc.php
// Open a connection to the DBMS
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die("Could not connect to database");
if (!mysql_select_db("winestore", $connection))
showerror();
$pdf=new FPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
//$cur = mysql_Exec($cnx,$SelectStmt);
//
//if (!$cur) {
//echo "Database Error";
//return;
//}
$x = 0;
$y = 0;
while (TRUE) {
if ($row = mysql_fetch_array($cur) ) {
$LabelText = sprintf("%s\n%s\n%s, %s, %s",
$rowї'firstname'],
$rowї'surname'],
$rowї'email'],$rowї'city'],$rowї'city']);
Avery5160($x,$y,$pdf,$LabelText);
$y++; // next row
if ($y == 10 ) { // end of page wrap to next column
$x++;
$y = 0;
if ($x == 3 ) { // end of page
$x = 0;
$y = 0;
$pdf->AddPage();
}
}
} else {
// Error quit printing
break;
}
}
$pdf->Output();
}
?>Here's the error;
Code: Select all
Fatal error: Cannot redeclare mysql_fetch_array() in D:\Program Files\Apache Group\Apache2\htdocs\fpdf151\winepdf\envelope.php on line 7If you've gotten this far thanks for reading - I hope someone can help me get to line 8..!
kamm...
[/long post ends here]