Page 1 of 1

Problems with apostrophs and arrays

Posted: Tue 30. Mar 2004, 19:15
by Jérôme
Hi folks,

I need your help :).

In a script, I build an array out of the values that are stored in the database. I do this with the following function:

Code: Select all

function products2array ($db)
{
	$array = array();
	
	// Basic product information
	
	$query = "SELECT * FROM phpwcms_products ORDER BY prod_id";
	
	$result = mysql_query($query);
	
	
	
	while ($row = mysql_fetch_assoc($result))
	{
		$array[$row["product_name"]]["category"] = $row["category"];
		$array[$row["product_name"]]["system_req"] = $row["system_req"];
		$array[$row["product_name"]]["contact_name"] = $row["contact_name"];
		$array[$row["product_name"]]["prices"] = array();
		
		// Add product price information to array
		$query2 = "SELECT * FROM ".DB_PREPEND."phpwcms_products_prices WHERE prod_id = ".$row["prod_id"]." ORDER BY display_pos";
		
		$result2 = mysql_query($query2);
		
		while ($row2 = mysql_fetch_assoc($result2))
		{
			$array[$row["product_name"]]["prices"][$row2["display_pos"]]["description"] = $row2["description"];
			$array[$row["product_name"]]["prices"][$row2["display_pos"]]["value"] = $row2["value"];
		}
	}
	
	return $array;
}
This produces an array like the following one:

Code: Select all

[bit-ACL-Explorer] => Array
        (
            [category] => Notes
            [system_req] => Lotus Notes 4.5 oder höher
            [contact_name] => Roland Hein
            [prices] => Array
                (
                    [1] => Array
                        (
                            [description] => Server: 1 Lizenz
                            [value] => 460
                        )

                    [2] => Array
                        (
                            [description] => Server: 2 Lizenzen
                            [value] => 322
                        )
                )
        )
At another place, I need this array and use it like this:

Code: Select all

function products_get_category ($product_name, $array)
{
	return $array[$product_name]["category"];
}
Until now, everything is okay.

Unfortunately, it could also be possible that I've got a product name with an apostroph ' in it. This would produce an array like the following:

Code: Select all

          [bit-Mail'n'Time] => Array
        (
            [category] => Notes
            [system_req] => Lotus Notes Clientversionen 4.5 oder höher, Server Domino R5 oder höher
            [contact_name] => Thomas Brausch
            [prices] => Array
                (
                    [1] => Array
                        (
                            [description] => bis 10 Gruppenlizenzen
                            [value] => 3045
                        )

                    [2] => Array
                        (
                            [description] => bis 25 Gruppenlizenzen
                            [value] => 3870
                        )
                )
        )
And this one causes problems, because the ' disturbs the second script (how obvious ;)).

Has someone of you an idea how to solve this? To be honest, I am a bit too tired at the moment to get a clear thought...

Thanks in advance

- Jérôme

Posted: Wed 31. Mar 2004, 11:48
by Oliver Georgi
When storing string in db you have to replace every apostrophe ' and every backslash.

Check my aporeplace function in phpwcms.

works like this

...myfiledname='".aporeplace($mystring)."'....

Oliver

Posted: Wed 31. Mar 2004, 12:44
by Jérôme
Thank you for your reply, Oliver.

Now, my array looks like this:

Code: Select all

[bit-Mail\'n\'Time] => Array
        (
            [category] => Notes
            [system_req] => Lotus Notes Clientversionen 4.5 oder höher, Server Domino R5 oder höher
            [contact_name] => Thomas Brausch
            [prices] => Array
                (
                    [1] => Array
                        (
                            [description] => bis 10 Gruppenlizenzen
                            [value] => 3045
                        )

...
The problem still persists...

The error message is:
Warning: Invalid argument supplied for foreach() in /web/hosting/de/bit-informatik/www/html/cms/include/inc_module/mod_company/inc_lib/products.functions.inc.php on line 92
The code in question is:

Code: Select all

function products_get_pricelist ($product_name, $array, $db)
{
	$product_name = aporeplace($product_name);
	$currency = get_currency_symbol($db);
	$pricelist_head = get_pricelist_head($db);
	
	$pr_arr = $array[$product_name]["prices"];
		
	$r = "<div class=\"pricetable\">\n";
	$r .=  "<table cellspacing=\"0\" cellpadding=\"0\">\n";
	$r .= "  <tr><th colspan=\"2\">".$pricelist_head." '".$product_name."'</th></tr>\n";
	
	$rowcount = 0;	
	
	echo "<pre>";
	print_r($array);
	echo "</pre>";

	
	foreach ($pr_arr as $row)
	{
		++$rowcount;
		if (bcmod($rowcount, 2) == 0)
			$rowclass = "tr2";
		else
			$rowclass = "tr1";
...
You can have a look at the page here (there are debugging output, so you'll have to scroll downwards :)).

http://www.bit-informatik.de/cms/index. ... ,0,0,1,0,0

Posted: Wed 31. Mar 2004, 13:08
by Oliver Georgi
Ah OK - now I have understand - my fault.

I think you should replace every quote and double qoute in key name with "_" or any other non regular char for procut name like #. And if you need the key name to get the as product name backe again replace # to quote or dpoble quote.

Oliver

Posted: Wed 31. Mar 2004, 14:51
by Jérôme
*sigh*

I should have looked in your core code more closely, Oliver. I found the functions html_specialchars and html_despecialchars.

Now I use the dirst to write into the database, the second to read out of the database.

In addition do that, I modified my array-function so that the output will be

Code: Select all

array["bit-Mail'n'Time"]