Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    variable not looping to push to Javascript

    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.


    PHP Code:

    <?php 
    include("dbc.php");

    $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">
    OutHTML;


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

    $ExpandImageTafter = <<<OutHTML
    </div>
    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 ($ExpandTemplatehtmlspecialchars ($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 ($ExpandImageTemplatehtmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars (rawurlencode ($row['image_filepath'])),htmlspecialchars ($row['component_name']));

    }


    ?>



    <!-- html -->



    <?php 
    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]))
        {
        document.getElementById(divs[i]).style.display='none';
        }
      }
    onediv.style.display = 'block';
    }

    </script>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    $JSVariables "'" htmlspecialchars ($row['folder_path']). "-expander', "
    It should be '.=' (with the dot). You'll end up with

    PHP Code:
    $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

    PHP Code:
    $JSVariables ''
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    hmmmm...

    I'm assuming you mean
    PHP Code:
    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?

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    scratch that... there must've been an extra space at the end, changing it to -2 got rid of the comma.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    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().
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •