PDA

View Full Version : Big Confusing PHP problem



- |Flash Man| -
May 28th, 2007, 11:09 PM
Hey all,

this is a big annoying confusing problem...so lets start:

First off i have a mysql query which displays 20 random words in a table.
The words are contained in the variable $r['words'] words being my table column in the database.

So what i want to do now is capture those exact 20 random words in a variable and send that variable with those 20 words to another php page.

On this php page i create a pdf using php which works fine and if i type a just normal text it shows in the pdf...but rather than typing normal text i want the variable and the 20 words to display in the pdf.

if anybody has any ideas on how to get my 20 words into a variable and into the other page i should be able to handle the rest.

I'll post some code below aswell:
The first page php code and mysqlquery:


<?php require_once('Connections/myconnection.php'); ?>
<?php
$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20") or die(mysql_error());
echo '<table width="466" height="256" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
if($x == 4){
echo '</tr><tr>';
$x=0;
}

echo '<td width="116.5" height="30" align="center" class="style2">'.$r['words'].'</td>'."\n";
}
echo '</tr>
</table>';
?>


that puts the query and the 20 words into a table

now is the pdf creating php page:


<?php
include('class.ezpdf.php');

switch ($_GET['type'])
{
default : $pdf = new backgroundPDF('a4', 'portrait', 'image', array('img' => 'pdfbg.jpg')); break;
case '1' : $pdf = new backgroundPDF('a4', 'portrait', 'colour', array('r' => 0.5, 'g' => 0.4, 'b' => 0.8)); break;
}

$pdf->selectFont('Helvetica.afm');
$pdf->ezText('this is the normal text i want to replace with my variable', 50);

$pdf->ezStream();

?>


So any ideas??

Thanks in advance :)

simplistik
May 29th, 2007, 08:25 AM
post it into a session. break it out into an array on the next page... or you can post it into a the url and then have the next page use $_GET to read the url and create an array off that.

also... it'd prolly be simplier to create a .txt file instead of a pdf file :lol:

- |Flash Man| -
May 29th, 2007, 08:30 AM
yeh i know but the document has some complex stuff in it...and a detailed background image and live links from within the document going to page and page...Plus looks much better as a pdf lol:hat:

I suppose i could use cookies which is a little less hassel i guess...and it doesn't have to be that secure either.

I'll try something tommorow using cookies and see how i go...i'll post back soon...Thanks for the help simplistik :)

simplistik
May 29th, 2007, 08:32 AM
if you want easy then i'd post it in the url it'd be pretty easy that way and won't require sessions.

you'd have something like


x.php?w=word1^word2^word3

on the next page


$words = $_GET['w'];
$array = explode('^',$words);
$word = $array[0];

etc...

duncanhall
May 29th, 2007, 08:36 AM
Why split it into two pages at all?



<?php
require_once('Connections/myconnection.php');
include('class.ezpdf.php');

$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20") or die(mysql_error());
$html_table = '<table width="466" height="256" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000"><tr>';
for($i=0; $r = mysql_fetch_assoc($sql); $i++){
$html_table .= '<td width="116.5" height="30" align="center" class="style2">'.$r['words'].'</td>'."\n";
}

$html_table .= '</tr></table>';

switch ($_GET['type']) {
default : $pdf = new backgroundPDF('a4', 'portrait', 'image', array('img' => 'pdfbg.jpg')); break;
case '1' : $pdf = new backgroundPDF('a4', 'portrait', 'colour', array('r' => 0.5, 'g' => 0.4, 'b' => 0.8)); break;
}

$pdf->selectFont('Helvetica.afm');
$pdf->ezText($html_table, 50);

$pdf->ezStream();

?>

- |Flash Man| -
May 29th, 2007, 09:03 PM
but how would i put that into a an onClick function? so that when i click download it would work? because i have the words all going into a $word array so if i could do duncanhalls solution it would technically work great...just the onclick is needed???

Cheers for the help so far guys :)

- |Flash Man| -
May 29th, 2007, 09:20 PM
OK here is what i have at the moment....i'm trying to just get the words visible on the pdf page.

Here is the code for the pdf creation page:


<?php
// test the table functions
include('class.ezpdf.php');

$words = unserialize( $_COOKIE['cookie1'] );
echo $words;
print $words;

$pdf =& new Cezpdf();
$pdf->selectFont('./fonts/Helvetica');
$pdf = new backgroundPDF('a4', 'portrait', 'image' , array('img'=> 'pdfbg.jpg'));
//--------------------------------------------------

// make the table
$pdf->addText(30,$y,$size,$words);
//$pdf->ezTable($words);
// do the output, this is my standard testing output code, adding ?d=1
// to the url puts the pdf code to the screen in raw form, good for checking
// for parse errors before you actually try to generate the pdf file.
if (isset($d) && $d){
$pdfcode = $pdf->output(1);
$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
echo '<html><body>';
echo trim($pdfcode);
echo '</body></html>';
} else {
$pdf->stream();
}
?>
And here is what i have on my normal page:


//this is at the very top of the page.
<?php
setcookie ("cookie1", $words);
?>

//this code is in the body of the page.
<?php require_once('Connections/myconnection.php'); ?>
<?php
$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20") or die(mysql_error());
echo '<table width="466" height="256" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
$words[] = $r['words'];//this line puts the 20 words into a random array
if($x == 4){
echo '</tr><tr>';
$x=0;
}
echo '<td width="116.5" height="30" align="center" class="style2">'.$r['words'].'</td>'."\n";
}
echo '</tr>
</table>';
$words = serialize( $words );//i serialize the array here.

?>

//this code i use to out put the array words which it does then i try and //print/echo the $_COOKIE but it comes out blank??
<?php
echo $words;
print_r($_COOKIE["cookie1"]);
?>
so my error is i see nothing on the pdf page but my bg image.
i just want to try and get the same words to appear onto the pdf.

When i echo the words from the normal page in the array $words they appear but when i echo the $_COOKIE they don't appear...it's blank...what am i doing wrong or where have i missed something.

This solution is not using duncanhall's yet but i'm sure i will use it shortly if i can't get this to work, as i want to try both at some point in time.