Page 1 of 1

PHP String data not printing

Posted: Wed Sep 02, 2009 3:27 pm
by dreagose
I am having a problem with two sections of code that work only sometimes.

Background:
* This data is being pulled from an old Dbase3 table.
* The data will print with a simple echo statement ie: echo $string;
* it requires a few if() statements to verify that data exists before printing..
* the data is being printed in a table.

here is some of the code:
if ($rna)
{
echo "<table border='0'>";
echo "<tr><td><strong>Install notes:</strong>";
print "<br>Note #$rna - $rnta";
if ($rnb) print "<br>Note: #$rnb - $rntb";
if ($rnc) print "<br>Note: #$rnc - $rntc";
echo "</td></tr></table>";
}
the 3 letter variables (ie: $rna) are printing ok. they are only numbers but the 4 letter variables (ie: $rnta) are not. I can name them whatever and it does not change anything. The string lengths are aprox 50 chars each.

I am also having the same issue with another section. Also inside a table.

echo "<tr VALIGN='TOP'>
<form action='http://www.cartserver.com/sc/cart.cgi' onSubmit='javascript:pageTracker._linkByPost(this)' method=POST>
<td width='83'><strong>$fstag</strong></td>
<td>$fpd</td><td align='right' width='65'><strong>$fpr</strong></td>
<td valign='middle' width='30'><input type='TEXT' name='op1' value='1' size='2' /></td>
<input type='hidden' name='item' value='s-3166^$fstag^$fpd^$fpr^op1^' />
<td valign='middle' width='64'><input border='0' type='image' name='add' src='/images/addtocart.gif' width='93' height ='24' /></td></form>
</tr>";

Now, there are 2 of each of the above lists for both front and back and if there is only one available the code works.
I can provide further info if needed. me -> :banghead:
Anybody have any clue? I think its the variable data but not sure.
I am VERY new to PHP and am learning as I program.

Thanks

Re: PHP String data not printing

Posted: Wed Sep 02, 2009 5:10 pm
by cpetercarter
When you hit a problem like this, it is always a good idea to disentangle the HTML from the php, so that you are quite clear what is going on.

So, what happens if you replace:

Code: Select all

if ($rnb) print "<br>Note: #$rnb - $rntb";
if ($rnc) print "<br>Note: #$rnc - $rntc";
with

Code: Select all

 
if ($rnb) print "<br />Note :#".$rnb." - ".$srntb;
if ($rnc) print "<br />Note :#".$rnc." - ".$srntc;
 

Re: PHP String data not printing

Posted: Wed Sep 02, 2009 5:32 pm
by dreagose
I have changed the code as you said and it still fails. The server builds the HTML up to the point of failure and just stops. I even have tried echo in place of print and it does the same thing.

here is the updated code:

print "<br />Note: #".$fna." - ".$fnta;
if ($fnb) print "<br />Note: #".$fnb." - ".$fntb;
if ($fnc) print "<br />Note: #".$fnc." - ".$fntc;

this problem is driving me :crazy:

Re: PHP String data not printing

Posted: Thu Sep 03, 2009 1:41 am
by cpetercarter
So, when you run the code, you see some table rows which look like:

Note: #15 -

with nothing following the '-' to represent the four-letter variable eg $fnta?

It is worth quickly looking at the source code for the webpage (view -> page source in Firefox, something similar in IE) to confirm that there isn't some silly display problem in the browser. You could also try clearing the browser cache in case the problem is that it is showing an old version of the web page.

Assuming that there isn't a browser issue, then the problem must be that the $fnta variable is empty. You could go back and check the code that takes the values from the database - you are looking for things like typos (eg $ftna instead of $fnta), or a bit of code which alters the values (eg $fnta = ''), or a bit of code where you have written eg if ($fnta = '') when you meant if ($fnta == ''). Or perhaps the code which you have quoted is part of a function and you have forgotten to pass the variable $fnta to the function? In short, you have to go back and insert 'echo $fnta' at every stage on its journey until you find the point at which it suddenly doesn't contain a value any more. Then you will have found the problem.

Re: PHP String data not printing

Posted: Thu Sep 03, 2009 9:04 am
by dreagose
I've found the problem!!! :D

After reviewing the data over and over again I noticed that it was loosing string data someplace.

remembering previous ASP writing I looked into declaring the variables and found this:

Code: Select all

$string = (string) "some data"
I put that into the code I was using and it works like a champ. I figure that the server was loosing the information someplace because I had checked and rechecked my code and was unable to find anyplace that I had cleared that data.

here is an example of how I used it:

Code: Select all

$fnta = (string)$rn->Fields("note");
I thank you for your help and I hope that this may sometime fix someone else's headache