1

I have this code for create dynamic number of sliders with dynamic number of answers per slider and I can't understand two things of my code:

  1. Why only show one slider? (The first id slider number on my sql table)

  2. Why in the amount box only appears correct the ini value? When you move the slider appears A r r a y (one character each time that you move the slider) ?

    <script>            
        var arrayslidersvalueanswers="<?= $tableslidersanswervalues ?>";                
        <?php foreach ($tableslidersqid as $qid) { ?>
          <?php foreach ($qid as $index) { ?>               
            <?php echo $index;?>                    
            <?php foreach ($tableslidersanswersid as $qanswerid) { ?>
              <?php foreach ($qanswerid as $indexanswer) { ?>
    
        var answers="<?= $qanswerid ?>";                    
        var qid= "<?= $index ?>";
    
    
        jQuery(document).ready(function(){          
    
            /////////////////////SLIDER !!!!!!!!!!!!!!!!!!!!!!!!!!!
            var valor="<?= $tableslidersanswervalues[1][0] ?>"; // Ini slider
    
            $(document.getElementById("amount"+qid+"[]")).val(valor);
    
            $(document.getElementById("slider"+qid+"[]")).slider({ 
                                   min: 1,
                                   max:arrayslidersvalueanswers.length,
    
                                   slide: function( event, ui) {
                                       var ans=answers[0][1] ;
                                       $(document.getElementById("amount"+qid+"[]")).val(answers[ui.value-1][0]);
    
                                       } //end slider ini method builder
    
                 });//END OF FUNCTION
               });
            });// end JavaScript document function
    
                <?php } ?>
              <?php } ?>
             <?php } ?>
          <?php } ?>
    
    </script>
    
    <html>
        <p>
        <label for="amount< ?= $row_Answer['QuestionIDFK'];?>">< ?php echo $row_questionset['QuestionValue']; ?>< /label>
        <input type="text" id="amount< ?= $row_Answer['QuestionIDFK'];?>[]" name="amount< ?= $row_Answer['QuestionIDFK'];?>[]"  />
        </p>                   
        <div id="slider< ?= $row_Answer['QuestionIDFK'];?>[]" name="slider< ?php echo $row_Answer['AnswerValue']; ?>[]" >< /div>
    </html>
    
2
  • var arrayslidersvalueanswers="<?= $tableslidersanswervalues ?>"; replace by var arrayslidersvalueanswers="<?= echo $tableslidersanswervalues ?>"; so on.. Dec 18, 2012 at 9:58
  • thats don't works. On the other hand echo and <?= are the same when you use for inicialicate a var. Dec 18, 2012 at 10:09

1 Answer 1

0

You only have one slider in your HTML.

I think your foreach loops should go over your HTML to generate more markup and sliders. Then you can use a general selector in jquery to get all the sliders to work.

$("div[id^='slider']").slider(...

will select all divs with an id starting by "slider".

The way your code should be structured should be:

<script>
  $(("div[id^='slider']").slider(//slider configuration here);
</script>

<html>
  <?php foreach($allTableValues as $table): ?>
    <label>...
    <input>....
    <slider id="slider_<?php echo $table->id ?>">...
  <?php endforeach; ?>
</html>
2
  • I didn't add that loop to this code for simply my code but im doinmg that loop of course.For that reason its so strange only one slider. Dec 18, 2012 at 12:14
  • @user1901142 Do you have more than one sliderXXX in your generated html?
    – jaudette
    Dec 18, 2012 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.