Alignment Issue in PHP Code

Hi,

I was wondering if anyone can point me in the right direction as i am not able to align the text boxes beside there title’s. I was to get 2 out of 3 boxes to line up but cant seem to be able to get the last textbox to align correctly and also i can’t center a title that i need to have centered. Below is the code and screenshot of my issue. Thanks in advance for any suggestions/ help with getting this fixed as i am still trying to learn PHP coding.

[php]

<?php session_start(); $firsttext="Your Body Mass Index (BMI) is %%BMI%%"; ?> .calculator_div { font-size:16px; font-family:verdana, arial, sans-serif; border:1px solid #51739B; padding:25px; margin-top:0px; margin-bottom:0px; margin-right:160px; margin-left:260px; width:350px; } .calculator_div label { width: 8em; float: left; text-align: center; margin-right: 0.5em; display: block } <?php if(!empty($_POST['calculator_ok'])) { // set vars in session foreach($_POST as $key=>$var) { $_SESSION['bmi_calc_'.$key]=$var; } if($_POST['system']=='english') { $height=$_POST['height_ft_en']*12+$_POST['height_in_en']; $bmi=($_POST['weight_en']*703) / ($height*$height); } else { $height=$_POST['height_met']/100; $bmi=$_POST['weight_met'] / round(($height*$height),2); } $bmi=number_format($bmi,1,".",""); // prepare message for the user if($bmi<=18.5) { $bmimsg="Underweight"; } if($bmi>18.5 and $bmi<=24.9) { $bmimsg="Normal"; } if($bmi>=25 and $bmi<=29.9) { $bmimsg="Overweight"; } if($bmi>=30) { $bmimsg="Obese"; } // what is the weight range if($bmimsg!='Normal') { if($_POST['system']=='english') { $lowerlimit=number_format(( 18.5 * ($height*$height) ) / 703); $lowerlimitkg=number_format($lowerlimit*0.453,1,".",""); $upperlimit=number_format(( 24.9 * ($height*$height) ) / 703); $upperlimitkg=number_format($upperlimit*0.453,1,".",""); } else { $lowerlimit=number_format( 18.5 * ($height*$height) * 2.204 ); $lowerlimitkg=number_format(18.5 * ($height*$height),1,".",""); $upperlimit=number_format( 24.9 * ($height*$height) * 2.204 ); $upperlimitkg=number_format(24.9 * ($height*$height),1,".",""); } } //prepare texts $firsttext=str_replace("%%BMI%%",$bmi,$firsttext); $firsttext=str_replace("%%BMIMSG%%",$bmimsg,$firsttext); $lowertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$lowertext); $lowertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$lowertext); $lowertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$lowertext); $lowertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$lowertext); $uppertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$uppertext); $uppertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$uppertext); $uppertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$uppertext); $uppertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$uppertext); } ?>
   <form method="post">
   <div class="calculator_div">
        <div><input type="radio" value="english" name="system" <?php if($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english') echo "checked='true'";?> onclick="changeSystem('english');"> Imperial
        &nbsp;
        <input type="radio" value="metric" name="system" <?php if($_SESSION['bmi_calc_system']!='' and $_SESSION['bmi_calc_system']=='metric') echo "checked='true'";?> onclick="changeSystem('metric');"> Metric
        </div>
   <div><label>Your Weight:</label>
                <span id="englishWeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'block':'none'?>;"><input type="text" name="weight_en" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_en'])?$_SESSION['bmi_calc_weight_en']:""?>"> lbs</span>
		<span id="metricWeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english'))?'none':'block'?>;"><input type="text" name="weight_met" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_met'])?$_SESSION['bmi_calc_weight_met']:""?>"> kg</span>
   </div>	
   <div><label>Your Height:</label>
        <span id="englishHeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english'))?'block':'none'?>;"><input type="text" size="10" name="height_ft_en" value="<?php echo !empty($_SESSION['bmi_calc_height_ft_en'])?$_SESSION['bmi_calc_height_ft_en']:""?>"> ft
		&nbsp; <input type="text" size="10" name="height_in_en" value="<?php echo ($_SESSION['bmi_calc_height_in_en']!='')?$_SESSION['bmi_calc_height_in_en']:""?>"> in</span>
		<span id="metricHeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'none':'block'?>;">
		<input type="text" name="height_met" size="10" value="<?php echo ($_SESSION['bmi_calc_height_met']!='')?$_SESSION['bmi_calc_height_met']:""?>"> cm
		</span>
   </div>
   <div align="center">
   <input type="hidden" name="calculator_ok" value="ok">
   <input type="submit" value="Calculate Your BMI">
   </form>

   <?php if(!empty($_POST['calculator_ok'])):?>
   <div class="calculator_table">
   <p><?=$firsttext?></p>

   <?php
      switch($bmimsg)
   {		
      case 'Normal':
      // you can echo here if you want for normal weight people
      break;
      case 'Underweight':
          echo $lowertext;
      break;
      default:
          echo $uppertext;
      break;
    } 	
   ?> 

Calculate again

<?php endif;?> [/php]

Sorry forgot to post the screenshot.

I just copied the code to see the design but m getting error as I have to pass the session variables. However, I have did little changes on the code. Please replace your code with the below code. I’m not whether this works as I’m not able to see your design: :stuck_out_tongue:
[php]

Your Height: "> ft
<label>&nbsp;</label>
<span><input type="text" size="10" name="height_in_en" value="<?php echo ($_SESSION['bmi_calc_height_in_en']!='')?$_SESSION['bmi_calc_height_in_en']:""?>"> in</span>
<span id="metricHeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'none':'block'?>;">
<input type="text" name="height_met" size="10" value="<?php echo ($_SESSION['bmi_calc_height_met']!='')?$_SESSION['bmi_calc_height_met']:""?>"> cm
</span>
[/php]

Ok, I think I can help you out with this…

Before I give you other options, I have to say it looks like both inputs are to be on the same line. The ‘& n b s p;’ is a nonbreaking space to separate the two inputs. Expanding the bounding box (table?) should prevent the second input from wrapping to another line.

Now if you want the second input on another line, you can replace the ‘$n b s p;’ with a spacer.gif graphic with an appropriate width or add a style to the input and set ‘position:absolute; top:; left:;’

		[code]&nbsp; <input type="text" size="10" name="height_in_en" value=[/code]

put all the html code within one div like

Tittle all your codes

Try this

Sajan,

Sorry to be a pain in but i just checked the coding and the changes. It looks like everything is working except for when i click on “metric” the alignment does not stay and below is a screenshot of the issue.

I will see if i can figure out where to make the changes in the meantime. Once again thanks for the help.

ok this is the same problem I described before. In your style css you have the width set to 350, I’m guessing you modified this and it was wide enough to accommodate the form objects before. The objects are wrapping.

If you set an inline style to these form objects and set the top and left parameters for each, they will land exactly where you want all the time. If you can, expand the width, if not add a style to each to place manually.

Aztech4mac,

I have tried expanding the width but once i do that then it throws off the page alignment. If you can point me the right way as i am a noob to PHP and i am learning on the fly. Thanks

I have tried expanding the width but once i do that then it throws off the page alignment.

Well, thats the problem you came here for. Altering the width changes the layout. There is a sweet spot where the width will display correctly and is probably what the original author used in the first place.

if you alter someone else’s code, you are going to be forced to redesign the layout. HTML has a natural flow to the elements on the page. Elements flow from left to right and from top to bottom.

Web designers use many tricks to alter this natural flow. Sometimes an invisible spacer.gif graphic is used to take advantage of HTML’s natural flow, forcing other elements to be repositioned. When proper element positioning is impossible or impractical inline style code can be added to each element you need to position (or style other ways) or you can add code in your css segment for each element.

I find myself using inline style because it is easier to debug or alter. Looking through a css block in the header or an external file forces one to jump back and forth between the actual element and style information.

that being said add this to your css style block;

form #englishHeight {
position: absolute;
top: 128px; // change to set proper height positioning
left: 80px; // change to set proper width positioning
}

each form element will need to be included in the style block with it’s own segment like above. This uses the element’s id (not name!). Some of your elements have a name but no id. Either add an id to each element and use the style as above or change the ‘#’ to a ‘.’ to use the elements name. You will have to play with the top and left values to get what you want. Each name or id must be unique, but an element can have the same name and id.

Lastly, the code already uses an inline style to display or hide elements depending on the user selecting english or metric. the elements will most likely will overlap each other but only one will actually be displayed.

HTH

Ok thanks for the detail answer.

Aztech,

Sorry to be a pain but it looks like i am doing something wrong as it does not do anything even changing the top and bottom like you mentioned. Here is the current code that i am using and not sure what i am doing wrong. Thanks again for the help and the explanations.

[php]

<?php session_start(); $firsttext="Your Body Mass Index (BMI) is %%BMI%%"; ?> .calculator_div { font-size:16px; font-family:verdana, arial, sans-serif; border:1px solid #51739B; padding:25px; margin-top:0px; margin-bottom:0px; margin-right:160px; margin-left:260px; width:350px; } .calculator_div label { width: 8em; float: left; text-align: center; margin-right: 0.5em; display: block; } form .englishHeight { position: absolute; top: 200px; left: 80px; } form .englishWeight { position: absolute; top: 128px; left: 80px; } form .metricHeight { position: absolute; top: 128px; left: 80px; } form .metricWeight { position: absolute; top: 128px; left: 80px; } <?php if(!empty($_POST['calculator_ok'])) { // set vars in session foreach($_POST as $key=>$var) { $_SESSION['bmi_calc_'.$key]=$var; } if($_POST['system']=='english') { $height=$_POST['height_ft_en']*12+$_POST['height_in_en']; $bmi=($_POST['weight_en']*703) / ($height*$height); } else { $height=$_POST['height_met']/100; $bmi=$_POST['weight_met'] / round(($height*$height),2); } $bmi=number_format($bmi,1,".",""); // prepare message for the user if($bmi<=18.5) { $bmimsg="Underweight"; } if($bmi>18.5 and $bmi<=24.9) { $bmimsg="Normal"; } if($bmi>=25 and $bmi<=29.9) { $bmimsg="Overweight"; } if($bmi>=30) { $bmimsg="Obese"; } // what is the weight range if($bmimsg!='Normal') { if($_POST['system']=='english') { $lowerlimit=number_format(( 18.5 * ($height*$height) ) / 703); $lowerlimitkg=number_format($lowerlimit*0.453,1,".",""); $upperlimit=number_format(( 24.9 * ($height*$height) ) / 703); $upperlimitkg=number_format($upperlimit*0.453,1,".",""); } else { $lowerlimit=number_format( 18.5 * ($height*$height) * 2.204 ); $lowerlimitkg=number_format(18.5 * ($height*$height),1,".",""); $upperlimit=number_format( 24.9 * ($height*$height) * 2.204 ); $upperlimitkg=number_format(24.9 * ($height*$height),1,".",""); } } //prepare texts $firsttext=str_replace("%%BMI%%",$bmi,$firsttext); $firsttext=str_replace("%%BMIMSG%%",$bmimsg,$firsttext); $lowertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$lowertext); $lowertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$lowertext); $lowertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$lowertext); $lowertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$lowertext); $uppertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$uppertext); $uppertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$uppertext); $uppertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$uppertext); $uppertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$uppertext); } ?>
   <form method="post">
   <div class="calculator_div">
        <div><input type="radio" value="english" name="system" <?php if($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english') echo "checked='true'";?> onclick="changeSystem('english');"> Imperial
        &nbsp;
        <input type="radio" value="metric" name="system" <?php if($_SESSION['bmi_calc_system']!='' and $_SESSION['bmi_calc_system']=='metric') echo "checked='true'";?> onclick="changeSystem('metric');"> Metric
        </div>
   <div><label>Your Weight:</label>
                <span id="englishWeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'block':'none'?>;"><input type="text" name="weight_en" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_en'])?$_SESSION['bmi_calc_weight_en']:""?>"> lbs</span>
		<span id="metricWeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english'))?'none':'block'?>;"><input type="text" name="weight_met" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_met'])?$_SESSION['bmi_calc_weight_met']:""?>"> kg</span>
   </div>	
   <div><label>Your Height:</label>
        <span id="englishHeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english'))?'block':'none'?>;"><input type="text" size="10" name="height_ft_en" value="<?php echo !empty($_SESSION['bmi_calc_height_ft_en'])?$_SESSION['bmi_calc_height_ft_en']:""?>"> ft</span>		
   </div>
   <div>
       <label>&nbsp;</label>
       <span ><input type="text" size="10" name="height_in_en" value="<?php echo ($_SESSION['bmi_calc_height_in_en']!='')?$_SESSION['bmi_calc_height_in_en']:""?>"> in</span>
       <span id="metricHeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'none':'block'?>;"><input type="text" name="height_met" size="10" value="<?php echo ($_SESSION['bmi_calc_height_met']!='')?$_SESSION['bmi_calc_height_met']:""?>"> cm
       </span>
   </div>
   <div align="center">
   <input type="hidden" name="calculator_ok" value="ok">
   <input type="submit" value="Calculate Your BMI">
   </form>

   <?php if(!empty($_POST['calculator_ok'])):?>
   <div class="calculator_table">
   <p><?=$firsttext?></p>

   <?php
      switch($bmimsg)
   {		
      case 'Normal':
      // you can echo here if you want for normal weight people
      break;
      case 'Underweight':
          echo $lowertext;
      break;
      default:
          echo $uppertext;
      break;
    } 	
   ?> 
<p align="center"><a href="http://<?=$_SERVER['HTTP_HOST'];?><?=$_SERVER['REQUEST_URI']?>">Calculate again</a></p>
<?php endif;?>

[/php]

You altered my css example. if an element has an id you use # in your css if it has a name use a .

So, 'id=“englishHeight” uses #englishHeight in the css and name=“englishHeight” uses .englishHeight

You MUST match the structures correctly, they are not interchangeable. My suggestion to simplify is to add an id to each element that you need to style that does not already have an id tag. Then use # in all css. It is not necessary to do this but it might just be an easy way to deal with it. An element can have both a name and id and an element can use the same string for both id and name, but each element must have a unique name from other elements. You can not start a name or id with a number but you can incorporate numbers in the string. (id=“input1”)

You will need to tinker with the top and left parameters to find the correct location for each element. Obviously 0 for both are the top left corner, increasing the top value, places the element x pixels down from the top, same for left. Be sure you add px and a semicolon after each parameter. A colon separates the label and value. (top:100px;) You need to style six elements.

Ok thanks for explaining it and i will try making the changes to the coding. Any issues i will let you know and thanks again for your help.

Hi,

I just made the changes and there seems to be an issue with the alignment. i can’t get everything to align correctly so am not sure what i am doing wrong. Here is a screen shot and the latest coding that i am using. So any suggestions would be appreciated it. Thanks

[php]

<?php session_start(); $firsttext="Your Body Mass Index (BMI) is %%BMI%%"; ?> .calculator_div { font-size:16px; font-family:verdana, arial, sans-serif; border:1px solid #51739B; padding:25px; margin-top:0px; margin-bottom:0px; margin-right:200px; margin-left:300px; width:350px; } .calculator_div label { width: 8em; float: left; text-align: center; margin-right: 0.5em; display: block; } form #englishWeight { position: absolute; top: 280px; left: 615px; } form #metricWeight { position: absolute; top: 0px; left: 0px; } form #englishHeight { position: absolute; top: 280px; left: 855px; } form #metricHeight2 { position: absolute; top: 300px; left: 855px; } form #metricHeight { position: absolute; top: 128px; left: 80px; } <?php if(!empty($_POST['calculator_ok'])) { // set vars in session foreach($_POST as $key=>$var) { $_SESSION['bmi_calc_'.$key]=$var; } if($_POST['system']=='english') { $height=$_POST['height_ft_en']*12+$_POST['height_in_en']; $bmi=($_POST['weight_en']*703) / ($height*$height); } else { $height=$_POST['height_met']/100; $bmi=$_POST['weight_met'] / round(($height*$height),2); } $bmi=number_format($bmi,1,".",""); // prepare message for the user if($bmi<=18.5) { $bmimsg="Underweight"; } if($bmi>18.5 and $bmi<=24.9) { $bmimsg="Normal"; } if($bmi>=25 and $bmi<=29.9) { $bmimsg="Overweight"; } if($bmi>=30) { $bmimsg="Obese"; } // what is the weight range if($bmimsg!='Normal') { if($_POST['system']=='english') { $lowerlimit=number_format(( 18.5 * ($height*$height) ) / 703); $lowerlimitkg=number_format($lowerlimit*0.453,1,".",""); $upperlimit=number_format(( 24.9 * ($height*$height) ) / 703); $upperlimitkg=number_format($upperlimit*0.453,1,".",""); } else { $lowerlimit=number_format( 18.5 * ($height*$height) * 2.204 ); $lowerlimitkg=number_format(18.5 * ($height*$height),1,".",""); $upperlimit=number_format( 24.9 * ($height*$height) * 2.204 ); $upperlimitkg=number_format(24.9 * ($height*$height),1,".",""); } } //prepare texts $firsttext=str_replace("%%BMI%%",$bmi,$firsttext); $firsttext=str_replace("%%BMIMSG%%",$bmimsg,$firsttext); $lowertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$lowertext); $lowertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$lowertext); $lowertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$lowertext); $lowertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$lowertext); $uppertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$uppertext); $uppertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$uppertext); $uppertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$uppertext); $uppertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$uppertext); } ?>
   <form method="post">
   <div class="calculator_div">
        <div><input type="radio" value="english" name="system" <?php if($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english') echo "checked='true'";?> onclick="changeSystem('english');"> Imperial
        &nbsp;
        <input type="radio" value="metric" name="system" <?php if($_SESSION['bmi_calc_system']!='' and $_SESSION['bmi_calc_system']=='metric') echo "checked='true'";?> onclick="changeSystem('metric');"> Metric
        </div>
   <div><label>Your Weight:</label>
        <span id="englishWeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'block':'none'?>;"><input type="text" name="weight_en" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_en'])?$_SESSION['bmi_calc_weight_en']:""?>"> lbs</span>
		<span id="metricWeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english'))?'none':'block'?>;"><input type="text" name="weight_met" size="10" value="<?php echo !empty($_SESSION['bmi_calc_weight_met'])?$_SESSION['bmi_calc_weight_met']:""?>"> kg</span>
   </div>	
   <div><label>Your Height:</label>
        <span id="englishHeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english'))?'block':'none'?>;"><input type="text" size="10" name="height_ft_en" value="<?php echo !empty($_SESSION['bmi_calc_height_ft_en'])?$_SESSION['bmi_calc_height_ft_en']:""?>"> ft
        </span>		
   </div>
   <div>
       <label>&nbsp;</label>
       <span id="metricHeight2"><input type="text" size="10" name="height_in_en" value="<?php echo ($_SESSION['bmi_calc_height_in_en']!='')?$_SESSION['bmi_calc_height_in_en']:""?>"> in</span>
       <span id="metricHeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'none':'block'?>;"><input type="text" name="height_met" size="10" value="<?php echo ($_SESSION['bmi_calc_height_met']!='')?$_SESSION['bmi_calc_height_met']:""?>"> cm</span>
   </div>
       <div align="center">
       <input type="hidden" name="calculator_ok" value="ok">
       <input type="submit" value="Calculate Your BMI">
   </form>

   <?php if(!empty($_POST['calculator_ok'])):?>
   <div class="calculator_table">
   <p><?=$firsttext?></p>

   <?php
      switch($bmimsg)
   {		
      case 'Normal':
      // you can echo here if you want for normal weight people
      break;
      case 'Underweight':
          echo $lowertext;
      break;
      default:
          echo $uppertext;
      break;
    } 	
   ?> 
<p align="center"><a href="http://<?=$_SERVER['HTTP_HOST'];?><?=$_SERVER['REQUEST_URI']?>">Calculate again</a></p>
<?php endif;?>

[/php]

Hi,

Would anyone have any idea or direction on how i can fix the issue or is not possible.

Thanks

Well, it would be very easy to do in a table. But, it seems the world says that is not the best way to do it.
(Don’t know why, because I do this in tables all the time??? Hmmmm…)

But, in your case it is because of the layout of your DIV’s. You have absolute positioning used.
This is a little tricky sometimes. Because you have each section placed in a different DIV!
In one DIV, if you have to items such as text and an input box, it is moved around depending on the
sizes of each item. So, most likely it is just a matter of making your DIV’s wider…

Here is a link to a tutorial that is 10 short steps to understand how this works…
http://www.barelyfitz.com/screencast/html-training/css/positioning/
And, here is another one that is good explaining it from the beginning…
http://www.w3schools.com/Css/css_positioning.asp
(ON this one, press the PREVIOUS and NEXT-CHAPTER to see lots of great ways to use CSS for the type of display you want to do.)

Hopefully, these will help you get them aligned… If not ask further questions… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service