Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Unexpected character when displaying db line.

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
blingbling!!
Smarty Rookie


Joined: 30 Jul 2003
Posts: 5

PostPosted: Fri Aug 01, 2003 4:37 pm    Post subject: Unexpected character when displaying db line. Reply with quote

Hi.

Posting this here cos i think this might be a bug. I have found a fix for the problem to prevent this happening on my website, and i have included this as well.
The basic problem is arising in a situation where i am getting an address from a MySQL db. The Address needs to be displayed on one line, so i need to put commas after each address field, such as

<address_name>, <address_line_1>, <address_line_2>, <address_town>, <address_ZIP>

It's no good hard-coding these into the template, because some addresses don't have an <address_line_2> and in this scenario I end up with 2 commas.
Ok, here is the code that gives me the unexpected results (i.e. the 'unfixed' version). There can be several addresses on each page, and these are sub-divided into NHS and Private. As in loop round the addresses in the MySQL result set, i do the formatting i need to do on individual fields within the result arrays, then amalgamte the single-dimension associative arrays into a multi-dimensional array to be passed to smarty.

[php:1:115eaa5649]
for ($i = 0;$i < $num_clin;$i++) {
$l = mysql_fetch_array($r2, MYSQL_ASSOC);
//$logger->log_array($l);
$np = $l['nhs_private'];
if ($np == "") {
$np = "N";
}
$np = strtoupper($np);
//Add a comma to the end of the end of the address
//components
if (trim($l['ad1']) != "") {
$l['ad1'] .= ",";
}
if (trim($l['ad2']) != "") {
$l['ad2'] .= ",";
}
if (trim($l['town']) != "") {
$l['town'] .= ",";
}
switch ($np) {
case "N":
$nhs_clinic_array[$j] = $l;
break;
case "P":
$private_clinic_array[$k] = $l;
$k++;
break;
}
}
[/php:1:115eaa5649]

Here is a section from the template used to display the results

[php:1:115eaa5649]
{section name=outer loop=$nhs_clinic_array}

{if $smarty.section.outer.first}
<tr>
<td colspan="3" width="100%" class="{$site_section}subheading">
NHS Practice
</td>
</tr>
<tr>
</tr>
{/if}

<tr>
<td colspan="2" width="50%" class="{$site_section}">
<div class="data">
{$nhs_hosp_link[outer]}
{$nhs_clinic_array[outer].ad1}
{$nhs_clinic_array[outer].ad2}
{$nhs_clinic_array[outer].town}
{$nhs_clinic_array[outer].postcode}
</div>
{* CODE OMMITED HERE FOR THE SAKE OF BREVITY *}
</td>
</tr>
{/section}
[/php:1:115eaa5649]

Here is a sample of the HTML output from all this. In this example, the address does NOT contain a value for 'ad2' (i.e. <address_line_2>), and you can see that we have ended up with a question mark (?) instead of nothing. I have checked the DB, and there is definatly no question mark in this field!!! Also - the value in the db is an empty string (""), not NULL. So the question is: where is the question mark coming from!!!

[php:1:115eaa5649]
<tr>
<td colspan="2" width="50%" class="consultant">
<div class="data">
<a href="data2/BROMH5.html">Neath Port Talbot Hospital</a>
Baglan Way,
?
Port Talbot,
SA12 7BX
</div>
<!-- Some HTML removed as per above -->

</td>
[/php:1:115eaa5649]

This next code sample is the same php code as above, but i've included the stuff i put in there to stop the question marks appearing.

[php:1:115eaa5649]
for ($i = 0;$i < $num_clin;$i++) {
$l = mysql_fetch_array($r2, MYSQL_ASSOC);
//$logger->log_array($l);
$np = $l['nhs_private'];
if ($np == "") {
$np = "N";
}
$np = strtoupper($np);
if (trim($l['ad1']) != "") {
$l['ad1'] .= ",";
}
else {
$l['ad1'] = "";
}
if (trim($l['ad2']) != "") {
$l['ad2'] .= ",";
}
else {
$l['ad2'] = "";
}
if (trim($l['town']) != "") {
$l['town'] .= ",";
}
else {
$l['town'] = "";
}
switch ($np) {
case "N":
$nhs_clinic_array[$j] = $l;
$j++;
break;
case "P":
$private_clinic_array[$k] = $l;
$k++;
break;
}
}
[/php:1:115eaa5649]
I have tested with Smarty version 2.2 and version 2.5, and they both behave the same way.

I hope this is usefull to you, please contact me if you require any further info.

Many thanks

Robin
Back to top
View user's profile Send private message
blingbling!!
Smarty Rookie


Joined: 30 Jul 2003
Posts: 5

PostPosted: Fri Aug 01, 2003 5:04 pm    Post subject: Reply with quote

Hmmm.....

Ok, looks like I might have made a blunder here, but i'm not too sure....

I'm using Mozilla Firebird as my browser, and i've just been over to my freind's machine and looked at the page in question through IE6, and the question mark is not there!!! This seems to point to a bug in Mozilla Firebird - right????? I't still pretty wierd behaviour, and i don't quite know what to make of it now.

Answers on a postcard......

Robin
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP