View Full Version : variable not looping to push to Javascript

07-31-2012, 08:18 AM
I'm working on a script that's incorporating a few new things I've never worked with before, such as templates and sprintf.

I've got two glitches I'm trying to figure out now.

The first is that the variable JSvariables is only pushing the very last looped variable into my javascript.

The second problem is that, for some reason, my div's that are labeled "variable"-expander are showing, even though the style code is written to hide them. It was fine before, but I'm getting crossed and confused with this sprintf stuff.

Here's what I've got right now.


$query = "SELECT tbl_component_categories.ID, tbl_component_categories.folder_path, tbl_component_categories.comp_cat_name, tbl_components.component_name, tbl_components.image_filepath, tbl_components.component_category

FROM tbl_components

JOIN tbl_component_categories ON tbl_components.component_category = tbl_component_categories.ID
ORDER BY tbl_components.component_category";
$result = mysql_query($query);

$category = false;

// set the templates
$ExpandTemplate = <<<OutHTML
<div id="%1\$s" style="width:350px;padding-top:20px; class="select-toggler" onclick="return showHide('%2\$s-expander');\">
<a href="" onclick="return showHide('%2\$s-expander');\"">
<img style="position:relative;top:-2px;" src="images/structural/red-plus.gif" /> %1\$s </a><br>
<div id="%2\$s-expander" style="float:left;padding-right:25px;display:none;" width="90">

$ExpandImageTemplate = <<<OutHTML
%4\$s <br><img src='%3\$s'> </div>

$ExpandImageTafter = <<<OutHTML

// and here's the loop
$Output = '';
while ($row = mysql_fetch_assoc ($result)) {

if ($category != $row['component_category']) {
$category = $row['component_category'];
if (!$firstime) {

$Output .= '</div><br><br>';

} else {
$firstime = true;

// Change output to be stored in a temp variable
$Output .= sprintf ($ExpandTemplate, htmlspecialchars ($row['comp_cat_name']), htmlspecialchars ($row['folder_path']));


// Add the name of the JS variable to a comma-seperated list.
// this is the variable that doesn't seem to work. It's not adding any but the last looped variable
$JSVariables = "'" . htmlspecialchars ($row['folder_path']). "-expander', ";

//Changed output to be stored in a temp variable
$Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars (rawurlencode ($row['image_filepath'])),htmlspecialchars ($row['component_name']));



<!-- html -->

echo $Output;

<!-- here's the script I'm tryingget the variables to. I've tried putting the script at the top and at the bottom. No difference -->

<SCRIPT LANGUAGE="JavaScript1.2">

function showHide(d)
var onediv = document.getElementById(d);
var divs=[<?php echo $JSVariables; ?>];
for (var i=0;i<divs.length;i++)
if (onediv != document.getElementById(divs[i]))
onediv.style.display = 'block';


07-31-2012, 01:11 PM
$JSVariables = "'" . htmlspecialchars ($row['folder_path']). "-expander', ";

It should be '.=' (with the dot). You'll end up with

$JSVariables = "'something-expander', 'other-expander', ";

That is, one single string value. You'll need to strip the last ", ".

Then, in JS, you'll need to split this single string into separate words using the split() JS function, to then store them in a divs array.

Added: Start with

$JSVariables = '';

07-31-2012, 07:17 PM

I'm assuming you mean
substr_replace($JSVariables ,"",-1);

I can't seem to figure out where that would go though? I've tried it several places, and it's not making any difference?

07-31-2012, 07:44 PM
scratch that... there must've been an extra space at the end, changing it to -2 got rid of the comma.

08-01-2012, 08:19 AM
if you’re going to put it in an JS array anyways, why not using json_encode() on a PHP array? no need to strip off commas or spaces or using String.split().