By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

function php_mixed_to_js_value($jsname, $mixed)

P: n/a
No questions, but just consider if this is useful to you:-) but of course
feedback & corrections are welcome.

function php_mixed_to_js_value($jsname, $mixed)
{
if(is_null($mixed))
{ return "\n $jsname = null;\n"; }
elseif(is_string($mixed))
{ return "\n $jsname = '$mixed';\n"; }
elseif(is_numeric($mixed) || is_boolean($mixed))
{ return "\n $jsname = $mixed;\n"; }
elseif(is_array($mixed))
{
$retvalue = "\n $jsname = new Array();\n";
foreach($mixed as $key=>$value)
{
if(is_numeric($key))
$new_jsname = $jsname."[".$key."]";
else
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_object($mixed))
{
$retvalue = "\n $jsname = new Array();\n";
$object_vars = get_object_vars($mixed);
foreach($object_vars as $key=>$value)
{
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_resource($mixed) && get_resource_type($mixed) == "mysql result"
&& mysql_numrows($mixed)!==false)
{
$retvalue = "\n $jsname = new Array();\n";
$index=0;
while($row = @mysql_fetch_assoc($mixed))
{
$retvalue.= $jsname."['row_nr_".$index."'] = new Array();\n";
foreach($row as $key=>$value)
{
$new_jsname = $jsname."['row_nr_".$index."']['$key']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
$index++;
}
return $retvalue;
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
// well i took away some useless stuff and corrected something
// remember to use inside javascript tags!
// <?=php_mixed_to_js_value('names', $names)?>

function php_mixed_to_js_value($jsname, $mixed, $db_assoc=true)
{
if(is_null($mixed))
{ return "\n$jsname = null;\n"; }
elseif(is_string($mixed))
{ return "\n$jsname = '$mixed';\n"; }
elseif(is_numeric($mixed) || is_boolean($mixed))
{ return "\n$jsname = $mixed;\n"; }
elseif(is_array($mixed))
{
$retvalue = "\n$jsname = new Array();\n";
foreach($mixed as $key=>$value)
{
if(is_numeric($key))
$new_jsname = $jsname."[".$key."]";
else
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_object($mixed))
{
$retvalue = "\n$jsname = new Array();\n";
$object_vars = get_object_vars($mixed);
foreach($object_vars as $key=>$value)
{
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_resource($mixed) && get_resource_type($mixed) == "mysql result"
&& mysql_numrows($mixed)!==false)
{
$retvalue = "\n$jsname = new Array();\n";
$index=0;
if($db_assoc)
{
while($row = @mysql_fetch_assoc($mixed))
{
$retvalue.= "\n".$jsname."[$index] = new Array();\n";
foreach($row as $key=>$value)
{
$new_jsname = $jsname."[$index]['$key']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
$index++;
}
}
else
{
while($row = @mysql_fetch_row($mixed))
{
$retvalue.= "\n".$jsname."[$index] = new Array();\n";
foreach($row as $key=>$value)
{
$new_jsname = $jsname."[$index][$key]";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
$index++;
}
}
return $retvalue;
}
}
Jul 17 '05 #2

P: n/a
// HOPEFULLY LAST CORRECTED VERSION :-)

function php_mixed_to_js_value($jsname, $mixed, $db_assoc=true)
{
if(is_null($mixed))
{ return "\n$jsname = null;\n"; }
elseif(is_string($mixed))
{ return "\n$jsname = '$mixed';\n"; }
elseif(is_numeric($mixed))
{ return "\n$jsname = $mixed;\n"; }
elseif($mixed===true)
{ return "\n$jsname = true\n"; }
elseif($mixed===false)
{ return "\n$jsname = false;\n"; }
elseif(is_array($mixed))
{
$retvalue = "\n$jsname = new Array();\n";
foreach($mixed as $key=>$value)
{
if(is_numeric($key))
$new_jsname = $jsname."[".$key."]";
else
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_object($mixed))
{
$retvalue = "\n$jsname = new Array();\n";
$object_vars = get_object_vars($mixed);
foreach($object_vars as $key=>$value)
{
$new_jsname = $jsname."['".$key."']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
return $retvalue;
}
elseif(is_resource($mixed) && get_resource_type($mixed) == "mysql result"
&& mysql_numrows($mixed)!==false)
{
$retvalue = "\n$jsname = new Array();\n";
$index=0;
if($db_assoc)
{
while($row = @mysql_fetch_assoc($mixed))
{
$retvalue.= "\n".$jsname."[$index] = new Array();\n";
foreach($row as $key=>$value)
{
$new_jsname = $jsname."[$index]['$key']";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
$index++;
}
}
else
{
while($row = @mysql_fetch_row($mixed))
{
$retvalue.= "\n".$jsname."[$index] = new Array();\n";
foreach($row as $key=>$value)
{
$new_jsname = $jsname."[$index][$key]";
$retvalue.= php_mixed_to_js_value($new_jsname, $value);
}
$index++;
}
}
return $retvalue;
}
}
Jul 17 '05 #3

P: n/a
*** Perttu Pulkkinen wrote/escribió (Fri, 01 Oct 2004 14:44:53 GMT):
// HOPEFULLY LAST CORRECTED VERSION :-)


What does this function do?

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.