Page 1 of 1

Having trouble displaying check boxes

Posted: Wed Sep 30, 2009 6:15 pm
by merylvingien
Hi again, i have tried to refrain from posting ridiculous questions and have muddled through till now, but i am starting to get bogged down.
Please be gentle as this is my first time with a php website.

What i am trying to acheive is this:

The site has many pages where someone can advertise thier skills, in essence they can subscribe to a page. Simple right?

Now on the page where they can select which pages they want to subscribe too, they choose a county from a drop down menu ( this is working ) then it displays all the pages connected to that county (mostly working apart from the first one doesnt display)

If the page is still available a check box should appear next to the town name and postcode, only at the moment a check box appears on all the towns. I have set the pagestate in the database to either A (available) or B (taken).

Eventually when i get it working, the user should be able to check all the towns he/she wants to take and then move onto the next page with their selected places.

The code i have hashed together so far have so far is this: (dont laugh to much) 8O
P.s the prices are just an example at the moment as i havent worked that out yet!

Code: Select all

<form action"code2.php" method="post">
        <?php
      
      if ($row['pagestate'] == "A") {$checkbox = "<input type='checkbox' value='0' name='add'></div>";} elseif ($row['pagestate'] == "B") {$checkbox = "Sorry this page is already taken</div>";}
      if ($row['priceb'] = "A") {$price = "£10.00";} elseif ($row['priceb'] = "B") {$price="£20.00";} elseif ($row['priceb'] ="C") {$price="£30.00";} else {$price="£40.00";}
      while($row = @mysql_fetch_array($result))
      {
    echo "<div class='redisplay'>" . $row['postcodename'] . ", ". $row['town'] . ", ". $row['county'] . ", ". $row['country'] . " $price, $checkbox";
    echo "<br />";
}
 
 
mysql_close($con);
 
?>
        <input type="submit" value="Continue">
Does this code look correct to those that know?

Re: Having trouble displaying check boxes

Posted: Wed Sep 30, 2009 7:03 pm
by califdon
Well, you are setting the price and checkbox variables once at the top, before entering the while loop, so I wouldn't expect them to change as you go through the loop. And where does the $row come from that you use at the top? Is there an earlier query? In short, I can't make any logical sense out of what your code is trying to do.

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 4:23 am
by merylvingien
Sorry, the full code is this:

Code: Select all

 
<?php
$counties=$_POST["counties"];
    
$con = mysql_connect("localhost", "user", "password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
 
$db_selected = mysql_select_db("database",$con);
$sql = ("SELECT postcodename, town, stowns, county, country, pagestate, priceb FROM Postcode WHERE county='$counties'")or die ('Error: '.mysql_error ());
$result = mysql_query($sql,$con);
$row = @mysql_fetch_array($result);
 
 
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Available postcodes in <?php echo "$counties"; ?></title>
<meta name="ROBOTS" content ="index, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
 
<body>
<div id="band">
  <div id="shadow"> 
    <div id="pagewrapper"> 
      <div id="header"> <img src="images/image1.png" alt="to be set" width="350" height="150" align="left" /> 
      </div>
    </div>
    <div id="leftsidebar"> 
      <div id="menu_vert"> 
        <div class="roundbox"> 
          <div class="t"> 
            <div class="b"> 
              <div class="l"> 
                <div class="r"> 
                  <div class="bl"> 
                    <div class="br"> 
                      <div class="tl"> 
                        <div class="tr"> 
                          <div id="code"> 
                            <h2>Postcode Finder</h2>
                            <p>Please enter the first half of your postcode to 
                              see who covers your area.</p>
                            <form action="postcode.php?id=postcode"  method="get">
                              <input name="postcode" type="text" size="5" maxlength="4" />
                              <input type="submit" name="postcodesearch" style="border:1px solid #000000; width:50px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;" value="Submit" />
                            </form>
                          </div>
                          <h2>Links</h2>
                          <ul>
                            <li> <a title="to be set" href="index.html">to be set</a> 
                            </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="to be set.html">to be set</a> </li>
                            <li> <a title="to be set" href="links.html">to be set</a> </li>
                          </ul>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <br />
      </div>
    </div>
    <div id="content"> 
      <p>The available Postcodes in <?php echo $counties ?> are:</p>
      <form action="code2.php" method="post">
        <?php
      
      if ($row['pagestate'] == "A") {$checkbox = "<input type='checkbox' value='0' name='add'></div>";} 
      elseif ($row['pagestate'] == "B") {$checkbox = "Sorry this page is already taken</div>";} 
      if ($row['priceb'] = "A") {$price = "£10.00";} elseif ($row['priceb'] = "B") {$price="£20.00";} 
      elseif ($row['priceb'] ="C") {$price="£30.00";} else {$price="£40.00";}
      
      while($row = @mysql_fetch_array($result)) 
      
      {
    echo "<div class='redisplay'>" . $row['postcodename'] . ", ". $row['town'] . ", ". $row['county'] . ", ". $row['country'] . " $price, $checkbox";
    echo "<br />";
}
 
 
mysql_close($con);
 
?>
        <input type="submit" value="Continue">
      </form>
    </div>
    <div id="footer"> 
      <p>Copyright &copy; http://www.to be set</p>
    </div>
    <br/>
  </div>
  </div>
</div>
 
 
</body>
</html>
 
Basicly what i am trying to achieve is when a user chooses a specific county from a list, the database should return a full list of the towns within the county chosen, and if the town is still available then a checkbox should be presented next to it. If the town has already been taken, then a message saying unavailble or something along those lines will appear instead of the checkbox.

The idea is that if the user still has his or her chosen towns still available then he can tick the check boxes to select those towns ready for the next step.

Hope i am making sense here. If not i can mock something up visualy if it will help make things clearer.
Any help here will be much appreciated...

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 1:00 pm
by califdon
So an earlier page allows the user to choose a county, which is passed to this script as a POST variable, which is used in the WHERE clause of your query on lines 13-14 (as the script appears here), which presumably returns records of postcodes in that county. Correct, so far? In line 15 you fetch one row of that recordset (which explains why you are not getting the first item in your display), then in line 86 you use the pagestate data from that row to determine the content of 2 variables, $checkbox and $price. Then you begin a while loop to fetch the remaining rows of the recordset and display their contents, along with the $checkbox and $price that you set in line 15. Do you see why you are getting the results you are getting? :)

Get rid of lines 13-15. Get rid of line 86. Do all of that inside your while loop. It has to be done for each row, not just the first row.

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 1:35 pm
by merylvingien
Many thanks for the reply califdon, i understand exactly what you are saying and moved the various peices of code as you instructed, but there must still be a mistake because now it displays one line ", , , £10.00,"

This is hopeless! I still have way to go!

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 2:09 pm
by califdon
Be sure that you understand what the mysql_... commands do in PHP. After mysql_connect() and mysql_select_db() set up the connection to the database, you can send mysql_query() requests to that database and then obtain "results" (arrays in memory, actually) which you can then navigate through, row by row, using mysql_fetch_... commands, which always fetch the next row in the results. If you're getting just one row, it's likely because you've only fetched one row. Review the logic of your code and see if you are really calling the fetch command every time you want a new row.

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 2:42 pm
by merylvingien
I thought that was the whole point of the "WHILE" loop? To go and get each row whilst the condition remained true.

So for instance whilst each row contains a county identical to the one that the user selected then it will loop through and display each row until the county name is not identical, then the loop stops?

Too be honest now i am not even sure if it is the while loop that i need here!

I think i am just scratching about in the dark getting nowhere.

Re: Having trouble displaying check boxes

Posted: Thu Oct 01, 2009 9:24 pm
by califdon
Programming is a skill that requires much patience and attention to detail and understanding in extremely clear terms what must be done. The while loop will repeat all the steps contained within the while block until the condition is no longer true, but it has no effect on anything not in the block. It is doing precisely what you told it to do: set the values of $checkbox and $price once, before the while block begins. Then it is repeating the steps within the block each time it fetches a new row from the query result. What you apparently want is to reset $checkbox and $price depending on each row, is it not? Then that has to occur inside the block.

Re: Having trouble displaying check boxes

Posted: Fri Oct 02, 2009 2:06 pm
by merylvingien
Starting to get my head around what you are talking about now. Although i have managed to get round the whole problem by taking a different approach i.e (database)

No matter what i tried, whilst adding anything within the "while" loop just didnt output any results, so i gave up on that.

Anyway thanks for your input.