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.

I am trying to make a dynamic division that changes its id with a variables. In addition to this, the dvision is a popup. The division has to be unique, since I am pulling info from a database. What I have so far is this:

<?php
$index_map = 4; //this variable is already defined in other part of the document.
//It is just here to specify its value to the readers

$index_data = 1;

while($index_data = $index_map)
{

echo '
<div data-role="popup" id="mapdata'.$index_data.'" class="ui-content" data-theme="d">
    <img class="popphoto" src="style/2nd_floor.png"
    style="max-height:512px;" alt="Parking_Lot_Map1">
    '.$index_data.'
</div>
';

$index_data++;
}
?>

For some reason the while loops does not do anything, and does not display any other html data after that. This is a PHP document that is included in another .php file. I dont why the while loop does no recognize the variables

share|improve this question
1  
I think you are looking for $index_data == $index_map double equals –  Justin Bicknell Jun 7 at 20:33
 
$index_data is always going to be set to a value of 4 at each iteration. Since 4 will evaluate as truthy, you will have an endless loop. –  Mike Brant Jun 7 at 20:37
add comment

3 Answers

up vote 0 down vote accepted

What exactly are you trying to do (would comment but can't)?

Even changing to the comparison == operator would do nothing if it is as on your fifth line $index_data = 1; because there's nothing to change that to 4. The loop would say false on the first execution and therefore nothing would happen, as you described. In your use case there is no need for $index_data, you could just use $index_map

<?php
    echo '
    <div data-role="popup" id="mapdata'.$index_map.'" class="ui-content" data-theme="d">
        <img class="popphoto" src="style/2nd_floor.png"
        style="max-height:512px;" alt="Parking_Lot_Map1">
        '.$index_map.'
    </div>
    ';
?>

Unless there's more to your code you haven't shown or explained.

share|improve this answer
 
As @Assimilater said, I was making the wrong comparison. I want it to compare them different to each other I used '!=' in other to compared them different and now my code works. Thank you guys, I was blinded by the fact that my code wasn't working that I was not able to see that small mistake –  JoseD Jun 10 at 16:27
 
@user2455993 That would make more sense. Glad I could help. When working through loops or other operations if you're not sure what's happening it's a good idea to write out a simplified step by step "Variable1 = value1; Variable2 = value2; Variable1 == Variable2? Value1 == value2? Yes? then loop" or similar replacing real names and values. I still do this, just in my head. But it got me started thinking through things without what I wanted getting in the way of seeing what was happening. Happy Programming! –  Assimilater Jun 11 at 0:01
add comment
while($index_data = $index_map)

should be

while($index_data == $index_map)

because = sets, == compares.

share|improve this answer
add comment

You're using the single equals operator, which always returns true. For comparison, you have to use == or ===

while($index_data = $index_map)

should be

while($index_data == $index_map)
share|improve this answer
add comment

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.