Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My code

<script type="text/javascript">
    if(<?php echo json_encode($re=='reject'); ?>){
        //if response is reject
        document.getElementById('hide').style.display = "none"; 
        document.getElementById('onreject').style.display="block";
    }

    else if(<?php echo json_encode($re=='approve'); ?>){
        //if response is approve
        document.getElementById('hide').style.display = "none"; 
        document.getElementById('onapprove').style.display = "block";
    }
    else
    {
        document.getElementById('hide').style.display="block";
    }
</script>

I want this to be inside a while loop til <?php $responseanswer = mysqli_fetch_array($result); ?>how do i that?

share|improve this question
4  
json_encode() is not JavaScript encoding. –  BoltClock Jan 28 '11 at 20:37
add comment

3 Answers

To expand on the first answer, here's one possible solution that doesn't involve switching back and forth between JavaScript and PHP too much. I'm personally a fan of templating engines (such as Smarty), but this is my next preference (all the logic goes in the PHP code, the JavaScript at the end of the loop just does the display work for you).

<script>
<?php
while($responseanswer = mysqli_fetch_array($result)) {
  // This is probably not how your code does it, but I don't know your table structure
  $re = $responseanswer[0];
  $div_to_show = "";

  // whether to show the element with ID "hide"
  $hide_display_style = 'none';

  // Decide now what divs we want visible and hidden
  if($re == 'reject') $div_to_show = 'onreject';
  else if($re == 'approve') $div_to_show = 'onapprove';
  else $hide_display_style='block';

  echo "document.getElementById('hide').style.display = '$hide_display_style';\n";
  if($div_to_show) {
    echo "document.getElementById('$div_to_show').style.display = 'block';\n";
  }
}
?>
</script>
share|improve this answer
    
hey..that's amazing..but the approve n reject are not working –  sarthak Jan 28 '11 at 21:23
    
plz..plz..help. –  sarthak Jan 28 '11 at 21:24
3  
People can't always respond within one minute. –  pimvdb Jan 28 '11 at 21:31
    
I made a couple typos but it should be correct now. However, since I don't know your whole application (but only what you've shown us), I can't possibly write something completely correct. In particular, each iteration of the loop will try to change the value of the same divs, which can't possibly be what you really want. –  Steve Howard Jan 28 '11 at 22:32
add comment

Do you mean this?

Anyway, combining JavaScript and PHP like this is not preferable in my opinion.

<?
 while($responseanswer = mysqli_fetch_array($result)) {
?>
 if(<?php echo json_encode($re=='reject'); ?>){
     //if response is reject
     document.getElementById('hide').style.display = "none"; 
     document.getElementById('onreject').style.display="block";
 }

 else if(<?php echo json_encode($re=='approve'); ?>){
     //if response is approve
     document.getElementById('hide').style.display = "none"; 
     document.getElementById('onapprove').style.display = "block";
 }
 else
 {
     document.getElementById('hide').style.display="block";
 }
<?
}
?>
share|improve this answer
    
thanks...if this is not preferable...can u give a solution –  sarthak Jan 28 '11 at 20:40
1  
Well, at the PHP side you already know what is going to be executed on the JavaScript side. Therefore, you are wasting bandwidth and loading time this way. At the client side, it will always say if(true) or if(false). You could, using PHP, decide what to send to the client and what not to send. –  pimvdb Jan 28 '11 at 20:43
    
Another reason why you shouldn't write it this way is that mixing inline PHP into JavaScript becomes more and more unreadable as your codebase grows. –  Steve Howard Jan 28 '11 at 20:50
    
Wrapping the JS in the PHP loop like this will repeat the entire if/else-if/else javascript block N times, one for each element of your fetch_array. Since each repeated block refers to the same 'hide', 'onreject', and 'onapprove' elements this wouldn't even do what you want. If you have 3 elements in the array [ 'reject', 'approve', 'neither' ] your 1st if-block through would set onreject to block, the 2nd would set onapprove to block, and 3rd would set hide to block, so all 3 elements would be visible. Remember: by the time the Javascript is executing the PHP just does not exist any more. –  Stephen P Jan 28 '11 at 21:14
1  
@sarthak I think you've been given enough suggestions to be able to solve this now. –  Hamish Jan 28 '11 at 21:47
show 3 more comments

Ewww. This makes it hard to read, I think a solution like this would be better. (But not best). Best would probably to have a js functions that knows how to handle what to hide and show as well, or adding styles within the tag elements themself or not outputting the id's explicitly.

Either way, this should be more readable, and will be easier to add additional elements;

<?php
    switch ($re) {
        case 'reject':
            $styles = array(
                'hide'      => 'none',
                'onreject'  => 'block',
                'onapprove' => 'none'
            );
            break;
        case 'approve':
            $styles = array(
                'hide'      => 'none',
                'onreject'  => 'none',
                'onapprove' => 'block'
            );
        default:
            $styles = array(
                'hide'      => 'block',
                'onreject'  => 'none',
                'onapprove' => 'none'
            );
    }

    foreach ($styles as $id => $value) {
        echo 'document.getElementById("' , $id, '").style.display="', $value, '";', PHP_EOL;
    }
?>
share|improve this answer
    
error in for loop.... –  sarthak Jan 28 '11 at 21:31
    
echo statement error...can u plz rectify it? –  sarthak Jan 28 '11 at 21:33
    
@sarthak Why are you using JavaScript to hide the elements anyway? You could much easier decide the class name through PHP, and define the visibility using CSS. –  pimvdb Jan 28 '11 at 21:35
    
the approve, reject is not working –  sarthak Jan 28 '11 at 21:35
    
approvedocument.getElementById("offbox").style.display="block"; approvedocument.getElementById("onreject").style.display="none"; approvedocument.getElementById("onapprove").style.display="none"; –  sarthak Jan 28 '11 at 21:37
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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