views:

255

answers:

2

Hello,

I have a mysql database which as one of the fields contains a html description. This description is not in my control, and is obtained and inserted automatically. An example of one of these descriptions is here:

http://www.nomorepasting.com/getpaste.php?pasteid=22492

The data is originally exported from an access database, and seems to remain intact. An example of the exported data is here:

http://www.yousendit.com/transfer.php?action=batch_download&batch_id=TTZtWmdsT01kMnVGa1E9PQ

I am trying to output the variable containing the html description into a popupwindow, to display it as is. The code I am trying to use to do this is here:

http://www.nomorepasting.com/getpaste.php?pasteid=22498

However it produces the following html code:

http://www.nomorepasting.com/getpaste.php?pasteid=22462

There is an unclosed style tag which prevents the rest of the page from displaying, and the popup winbdow from opening. I have narrowed this down to a php problem as far as I can tell, because the data seems fine in mysql.

edit:

I just attempted to select only article_Desc from the database with this code:

http://www.nomorepasting.com/getpaste.php?pasteid=22494

Which produced this as a result:

http://www.nomorepasting.com/getpaste.php?pasteid=22496

edit2:

There seem to be a problem with the countrycode variable containing the style tag. When I remove this, the picture is displayed and the popupwindow is created, only with html results much like the last link I pasted. The data seems correct in the database, so what could be causing this problem?

+3  A: 

Reminds me a little of this question.

You see here in PHP code

child1.document.write(' . json_encode($row2["ARTICLE_DESC"]) . ');

and here in HTML code

child1.document.write("");

This means that json_encode($row2["ARTICLE_DESC"]) outputs "". And since json_encode('') outputs "", this means $row2["ARTICLE_DESC"] is empty.

EDIT:

$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM Auctions WHERE ARTICLE_NO ='$pk'";

There is no need for the second sql2 since the first $sql should already include the ARTICLE_DESC. So

  1. The field ARTICLE_DESC is empty for that ARTICLE_NO.
  2. ARTICLE_DESC is not the correct name.
  3. ARTICLE_DESC is in a different table.

EDIT on EDIT:

$query = "SELECT article_desc FROM Auctions WHERE ARTICLE_NO ='220288560247'";

The name is article_desc not ARTICLE_DESC.

EDIT on COMMENTS:

Change all occurrences of ARTICLE_DESC to article_desc.

EDIT Now that you got the html code, you need to replace

json_encode($row['article_desc'])

with this

str_replace(array("\n", "\r", "\t"), array('', '', ''), $row['article_desc']);

which can be done in a nice function. Just remember all the html code you print is in

<!-- text -->

so you wont actually see anything in yer new window ...

<!-- +++++++++++++++++++++++++ Bitte ändern Sie im eigenen Interesse nichts an diesem Code! ++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++ Das kann massive Fehldarstellungen ihrer Auktion zur Folge haben! +++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++ ++++++++++++++++++++++++++ Ihr Supreme Team +++++++++++++++++++++++++++++++++++++++++ -->
OIS
Sorry, I did not update the link enough. The whole pointo f the question is that article_desc is not empty and that I am not using json_encode, i will fix this.
Joshxtothe4
So you gave me -1 because your question was misguiding?!
OIS
No? You have been very helpful to me, I gave you one up, I am sorry that it was -1 it was not an intention and should be fixed now.
Joshxtothe4
I am confused about your second edit, because without the json encode, the resulting html in the last link I posted is what gets assigned to document.write, indicated article_desc is correct and not empty?
Joshxtothe4
I dont understand "indicated article_desc is correct and not empty?", but its a case of wrong case. :P When you get the result you query in lower case, and when you dont get a result you query in UPPER CASE.
OIS
OK, I have made sure it is all lowercase, but the same thing is happening, although the problem has been narrowed down now.
Joshxtothe4
No, just article_desc. Auctions must still have a large A, and ARTICLE_NO must be upper case.
OIS
+2  A: 

The CSV file is a bit of a mess. It appears that the fields are separated by tabs and not enclosed by anything. This may be OK for simple data but when you start putting HTML in you are going to have problems - looking at one of your other questions it looks like the CSV parser is getting confused and splitting one field of HTML into several - this seems to happen everytime it encounters a tab (and maybe also double quote?)

Are you able to change the format of the CSV file? I would suggest that you use commas to separate the fields, and that you define an enclosure character (e.g. a double quote) for more complex strings like HTML - I think that you will need to make sure that any double quotes within the string are also escaped. Also bear in mind that you may need to remove or escape line breaks from within these strings depending on what is parsing it, but I'm not completely sure of this.

Edit after your comment

You don't need to worry about a string containing the delimiter, as long as it is enclosed by charactor. If you needed a row containing three fields, and the three string values were:

String1
String2
Stri,ng3

The following is not valid and would be seen by the parser as four fields

String1,String2,Stri,ng3

The following is valid because the delimeter is 'enclosed' by a double-quote

"String1", "String2", "Stri,ng3"

It gets trickier then when you want to have a double quote within the string - this would then need to be escaped. If you wanted to represent

String1
String"2

the CSV field could be escaped like

"String1", "String\"2"

If I remember correctly you were importing into MySQL using LOAD DATA?, so for the above examples you might want to use options like

FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\'

I'm not sure how MySQL would treat line-breaks within enclosed strings, I can't seem to find much about this. If it reads the file line-by-line you might need have problems.

Edit 2

If the remaining problems are line-break related you could do a string replace of \n with \\n and \r with \\r in the code that exports each field to CSV

Tom Haigh
OK, this could be the problem, I had thought tabs were the best thing to use for a delimiter, when dealing with data that could contain any other kind of character. I had not thought that the html would contain tab characters. Since the html could contain anything, I don't know what delimiter to use
Joshxtothe4
\t and \n would be taken care of by json_encode ?And thats not the problem here either, as you can see in the HTML code.
OIS
Then what is causing part of the html to end up in COUNTRYCODE?
Joshxtothe4
It's ending up in country code because the csv parser thinks that the HTML field is actually several fields, so it gets out of sync and the countrycode data gets lost
Tom Haigh
@OIS - I think the problem is that the MySQL database has bad data in it - the json_encode is not really relevant at this point. Unless I've massively misunderstood the issue.
Tom Haigh
You seem to be on the money. Selecting article_desc after importin in the original tab delimtied returns this: http://www.nomorepasting.com/getpaste.php?pasteid=22502 I am getting strange results and errors when trying to input with commas or semicolons regardless of enclosing them.
Joshxtothe4
could you put up an example of your lastest CSV file, and the create table script for the mysql table ?
Tom Haigh
It must be a problem with newline characters in the records..
Joshxtothe4
I have to go now but will do so first thing on monday, thankyou for your help so far!
Joshxtothe4
it seems to be working now with enclosing and semicolons. it will not let me export with commas as the delimiter for some reason. the file is at http://www.yousendit.com/download/TTZueEVYT2J1Yk1LSkE9PQ .I get an error from the main file but it works if i call get_auction by itself, I am unsure why?
Joshxtothe4
That file doesn't contain any html?
Tom Haigh
It must, it is the basis of what I am now using? Not all records have html, search for <title> or such. Do you know why it is half the size of the tab delimited files? While I can get popup windows to work it does not seem to display the full html, but it display using the code in my 2nd last link.
Joshxtothe4
The problem I mentioned above seems to be Unicode related, as the popwindow has not html character including and after the first unicode character as where my test.php is just echoing, not using json_encode, it just displays characters as questions marks or something.
Joshxtothe4