PHP nOOb seek help from ancient PHP masters...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kammy
Forum Newbie
Posts: 6
Joined: Sun Feb 02, 2003 5:39 am

PHP nOOb seek help from ancient PHP masters...

Post by kammy »

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

Code: Select all

<?php 
function odbc_fetch_array($res) &#123; 
$row = array(); 
$result = array(); 
if ($result = odbc_fetch_row($res)) &#123; 
$nf = odbc_num_fields($res)+1; 
for($count=1; $count < $nf; $count++) &#123; 
$field_name = odbc_field_name($res, $count); 
$field_value = odbc_result($res, $count); 
$row&#1111;$field_name] = $field_value; 
&#125; 
return $row; 
&#125; 
&#125; 
// 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 
&#123; 
$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; 
&#125; 

function PrintAddressLabels($SelectStmt) 
&#123; 
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) &#123; 
echo "Database Error"; 
return; 
&#125; 

$x = 0; 
$y = 0; 
while (TRUE) &#123; 
if ($row = odbc_fetch_array($cur) ) &#123; 
$LabelText = sprintf("%s\n%s\n%s, %s, %s", 
$row&#1111;'MailName'], 
$row&#1111;'Address'], 
$row&#1111;'City'],$row&#1111;'State'],$row&#1111;'Zip']); 
Avery5160($x,$y,$pdf,$LabelText); 

$y++; // next row 
if ($y == 10 ) &#123; // end of page wrap to next column 
$x++; 
$y = 0; 
if ($x == 3 ) &#123; // end of page 
$x = 0; 
$y = 0; 
$pdf->AddPage(); 
&#125; 
&#125; 
&#125; else &#123; 
// Error quit printing 
break; 
&#125; 

&#125; 
$pdf->Output(); 
&#125; 

?>

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) &#123; 
$row = array(); 
$result = array(); 
if ($result = mysql_fetch_row($res)) &#123; 
$nf = mysql_num_fields($res)+1; 
for($count=1; $count < $nf; $count++) &#123; 
$field_name = mysql_field_name($res, $count); 
$field_value = mysql_result($res, $count); 
$row&#1111;$field_name] = $field_value; 
&#125; 
return $row; 
&#125; 
&#125; 
// 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 
&#123; 
$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; 
&#125; 

function PrintAddressLabels($SelectStmt) 
&#123; 
//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) &#123; 
//echo "Database Error"; 
//return; 
//&#125; 

$x = 0; 
$y = 0; 
while (TRUE) &#123; 
if ($row = mysql_fetch_array($cur) ) &#123; 
$LabelText = sprintf("%s\n%s\n%s, %s, %s", 
$row&#1111;'firstname'], 
$row&#1111;'surname'], 
$row&#1111;'email'],$row&#1111;'city'],$row&#1111;'city']); 
Avery5160($x,$y,$pdf,$LabelText); 

$y++; // next row 
if ($y == 10 ) &#123; // end of page wrap to next column 
$x++; 
$y = 0; 
if ($x == 3 ) &#123; // end of page 
$x = 0; 
$y = 0; 
$pdf->AddPage(); 
&#125; 
&#125; 
&#125; else &#123; 
// Error quit printing 
break; 
&#125; 

&#125; 
$pdf->Output(); 
&#125; 

?>
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!). :oops:

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 7

If you've gotten this far thanks for reading - I hope someone can help me get to line 8..!

kamm...

[/long post ends here]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

mysql_fetch_array() is a function which already exists in PHP - therefore you are going to have to name your function something else because you can't overwrite this PHP function with a user defined function.

Mac
kammy
Forum Newbie
Posts: 6
Joined: Sun Feb 02, 2003 5:39 am

Post by kammy »

obviously it's not gonna be as easy as changing it from;

mysql_fetch_array()

to say,

mysql_getit_array

If I do that although I don't get any errors - I just get nothing in the output

with view source I get this;

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"></HEAD>
<BODY></BODY></HTML>
kamm...
have I made it past line 7?
kammy
Forum Newbie
Posts: 6
Joined: Sun Feb 02, 2003 5:39 am

Post by kammy »

anyone else got any tips for sorting this..?

cheers
kamm...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The short answer is though that I think you should just delete that mysql_fetch_array function because it defines a function that does exactly what PHP's mysql_fetch_array() function does.

It's probably there because there isn't an ODBC version of this MySQL specific function.

Mac
Post Reply