Jump to content


Photo

Big Confusing PHP problem


  • Please log in to reply
27 replies to this topic

#1 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 28 May 2007 - 11:08 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 :dance:

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 May 2007 - 11:40 PM

Well, to get the 20 randomly selected words, you'll have to use an array to hold those twenty words and store each word in the array on each run of the loop.

Then you can either use the query string of the url, sessions, or cookies to pass them to the pdf page. Alternatively, you could use a database, possibly with serialize() and the user's ip (or sessions or cookies) to track the user from page 1 to the pdf page.

#3 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 29 May 2007 - 12:38 AM

but is it possible to have a variable in a cookie (the variable being the mysql query results) when the cookie code is the first bit of code on the page?

I mean how will that cookie know whats in the variable?

Cheers, :dance:

#4 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 29 May 2007 - 03:53 AM

Well you have a few options if you want to use a cookie.

As you are looping through the query results, you store the words into an array:

for($x=0; $r = mysql_fetch_assoc($sql); $x++){
	$words[] = $r['words'];
	if($x == 4){
		echo '</tr><tr>';
		$x=0;
	}

Once you have finished looping around displaying them all you, you will have the $words array holding all 20 words, at this stage you can do a few things:

}
echo '</tr>
</table>';

$words = implode( ',' $words );
#OR
$words = serialize( $words );

?>

Now with the first option you will have a string of all of the words, all seperated by a ,. And with your second option you will have all the words stored in a serialized array.

Either way you choose, you will have your results ready to be put into your cookie, now onto your pdf page.

Then on your pdf page you simply collect the results from the cookie and depending on what you did, you can access them:

Stored in the comma seperated string:
$words = explode( ',', $_COOKIE['c_words'] );

Or stored as a serialized array:
$words = unserialize( $_COOKIE['c_words'] );

Either way you have the words stored in array on your pdf page ready to use!

Matt

#5 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 May 2007 - 12:56 PM

but is it possible to have a variable in a cookie (the variable being the mysql query results) when the cookie code is the first bit of code on the page?

I mean how will that cookie know whats in the variable?

Cheers, :lol:

Set the cookie before you output anything using setcookie(). Then use the $_COOKIE superglobal to modify its data using something similar to what Matt did.

#6 The Creator

The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 29 May 2007 - 01:29 PM

or maybe u could assign each of the words a 3 letter code and then add all the codes into one big variable which u look at using the $_GET function and then decode it on the other page...

you will also need to have one common character at the end of each variable which u might be able to add later

for example

$amazing = uy7#
$sheep = 89x#
$umbrella =56U#

etc

and then select 20 of them and add them into one big variable

$variable = $amazing+$sheep+$umbrella;

//this would equal uy789x56U

then they click the link which is

<a href="page?variable=$variable">Link</a>

and then at the other side

$variable = $_GET[variable];

and then

$final = explode('#', $variable);


something like that anyway,

have fun!

#7 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 May 2007 - 04:54 PM

I have the feeling that this isn't some kind of guessing game, so there should be no need for encryption.

Besides, the basic principle of this has already been suggested ;)

Then you can either use the query string of the url, sessions, or cookies to pass them to the pdf page.



#8 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 29 May 2007 - 08:47 PM

Hey Peeps ;)

Thanks for the fast replies and help...i have been trying Mr.Matts solution to the problem and i have the words into the $words array and when i echo that variable it displays the 20 words as it should...but when i put it into my cookie as:

<?php
setcookie ("cookie1", $words);
?>
This code sits at the very top of the page.

if at the bottom of the page i try and echo that $_COOKIE to see that the words have actually gone into the cookie correctly it shows up blank.
see my code:
<?php
//echo $words; this echo's the $words array
//echo $_COOKIE["cookie1"]; 
print_r($_COOKIE["cookie1"]);//this displays nothing it's just blank?? Why??
?>

and on my pdf creation page i try to get the words to display onto the page(doesn't have to be in a table just want to get the words onto the pdf so that bit works) see the code to the whole 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();
}
?>
All this gives me is just the pdf with my bg image shown...there is no text from the cookie displaying on the pdf page.

Any ideas where i'm going wrong??

Thanks again for the help peeps :D

#9 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 May 2007 - 01:54 AM

Could you show us the full code of the page where you set the cookies. I have a sneaky suspicion you're setting it wrong.

Till then, you can try the grand old-fashioned way of making sure you have cookies enabled.
if($_COOKIE['test']){
	die('<pre>'.print_r($_COOKIE,1).'</pre>');
}

setcookie('test', 'A String.', 0); // I like to set the expire time... dunno why...
echo 'No cookie, refresh the page.';

Edited by rc69, 30 May 2007 - 01:55 AM.


#10 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 02:00 AM

i tried the check you said and it's coming up with no cookie set refresh the page so i must be setting it wrong :( i'll post the full code of the first page asap bare with me....Thanks :P

#11 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 02:09 AM

OK well without showing my layout etc...(sorry due to privacy reasons)

Here is the exact code layout just with a terrible table design.
But the exact code the way it is in my normal page is below:

<?php
if($_COOKIE['cookie1']){
	die('<pre>'.print_r($_COOKIE,1).'</pre>');
}

setcookie ("cookie1", $words, 0);//setcookie('test', 'A String.', 0); // I like to set the expire time... dunno why...
echo 'No cookie, refresh the page.';


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
	<td><?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'];
	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 );

?></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
</table>
<?php
//Then in the main body, you can place:
echo $words;
//echo $_COOKIE["cookie1"]; 
print_r($_COOKIE["cookie1"]);
?>
</body>
</html>

Am i setting it incorrectly??
Cheers :P

Edited by °•Guru•°, 30 May 2007 - 02:10 AM.


#12 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 30 May 2007 - 02:28 AM

The set cookie code is fine, its just in the wrong place. It can't be set at the top, because $words at that stage does not exist.

<?php
if($_COOKIE['cookie1']){
	die('<pre>'.print_r($_COOKIE,1).'</pre>');
}

echo 'No cookie, refresh the page.';


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
	<td><?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'];
	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 );
setcookie ("cookie1", $words); #Moved to here below $words

?></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
</table>
<?php
//Then in the main body, you can place:
echo $words;
//echo $_COOKIE["cookie1"];
print_r($_COOKIE["cookie1"]);
?>
</body>
</html>

That should be what you need.

#13 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 02:53 AM

hmmm okay i tried that...it now pops up with this error?

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\my_folder\index2.php:7) in C:\wamp\www\my_folder\index2.php on line 92

i'm sure this is a basic problem that happens all the time...i'll keep trying somethings but any ideas??

Cheers

#14 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 30 May 2007 - 03:03 AM

oh yea cookies have to be sent before any output. OK give me a few mins.

<?php

require_once('Connections/myconnection.php');

if($_COOKIE['cookie1']){
	die('<pre>'.print_r($_COOKIE,1).'</pre>');
}

$sql = mysql_query( "SELECT * FROM mytable ORDER BY RAND() LIMIT 20" ) or die( mysql_error() );

$t  = '<table width="466" height="256" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">';
$t .= '<tr>';

for( $x=0; $r = mysql_fetch_assoc( $sql ); $x++ ) {
	$words[] = $r['words'];
	if( $x == 4 ){
		$t .= '</tr><tr>';
		$x=0;
	}
	$t .= '<td width="116.5" height="30" align="center" class="style2">'.$r['words'].'</td>'."\n";
}

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

$words = serialize( $words );

setcookie ( "cookie1", $words, 0 );//setcookie('test', 'A String.', 0); // I like to set the expire time... dunno why...

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
	<td><?=$t?></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
</table>
<?php
//Then in the main body, you can place:
echo $words;
//echo $_COOKIE["cookie1"];
print_r($_COOKIE["cookie1"]);
?>
</body>
</html>

What I have done is stick the PHP bit up the top and stored the output into a variable (so it doesn't get sent echoed out), then I have set the cookie, then down the page where you had the php I just replaced with the variable so the table will show there with the words.

Matt

Edited by Mr. Matt, 30 May 2007 - 03:08 AM.


#15 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 03:09 AM

yeh i was just thinking that...in one of my previous posts i think i said how would you try and get the $words variable to the top as it has been processed yet...thats what's been bothering me because when i echoed it it just came out as ARRAY[]....so yeh i'll keep thinking too. Cheers for the help so far :P

#16 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 30 May 2007 - 03:12 AM

look at my edited post above yours.

#17 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 03:19 AM

great! The cookie is being sent out now :( Thanks :P

But now this
<?=$t?>
script which you have added does not load my table or the words in the table the area is just blank...

I know you've helped me out alot with this one...so if ya get the time/patience to check this...i'll see if i can beat you back here with a solution aswell...Hopefully i can do it :D

Cheers

#18 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 30 May 2007 - 03:23 AM

After your setcookie, do:

echo $t;

and let me know what gets echoed out, you may have short tags turned off, let me know.

Also instead of

<?=$t?>

try

<?php echo $t; ?>

instead.

Edited by Mr. Matt, 30 May 2007 - 03:24 AM.


#19 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 03:25 AM

cool i got it...i needed to change
<?=$t?>
to
<?php echo $t ?>

Thanks for the help...i'll keep trying with creating the pdf now what not...i'll post back with hopefully my success from your help!

Thanks peeps :P and mr matt!

lol blast you beat me! lol

#20 °•Guru•°

°•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 30 May 2007 - 03:47 AM

header("Content-type: application/pdf");
  header("Content-Length: ".strlen(ltrim($tmp)));
  $fileName = (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  header("Content-Disposition: inline; filename=".$fileName);
  if (isset($options['Accept-Ranges']) && $options['Accept-Ranges']==1){
	header("Accept-Ranges: ".strlen(ltrim($tmp))); 
  }

thats the code off the pdf creating class.

and the error i get when creating the pdf is as follows:
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\myfolder\printVersion1.php:5) in C:\wamp\www\myfolder\class.pdf.php on line 1916

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\myfolder\printVersion1.php:5) in C:\wamp\www\myfolder\class.pdf.php on line 1917

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\myfolder\printVersion1.php:5) in C:\wamp\www\myfolder\class.pdf.php on line 1919

So the cookie is attempting to modify the header which has already been done by the class code as thats the first include (right at the top first thing to be processed).

Any workarounds i'll attempt moving some things around and see how i go with it.

Cheers :P

Edit again ---
I changed the serialze and unserialize to implode and explode and that has given the words the correct print $_COOKIE format i need...BUT in the pdf it displays the text Array instead of the 20 words?? So close yet soo far....any ideas?

Cheers...Hopefully i can crack this one too :(

Edited by °•Guru•°, 30 May 2007 - 04:00 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users