1

Well it's been my very first initiative to build a dynamic page in php. As i'm a newbie in php, i don't know much about php programming. i've made a database named "dynamic" and it's table name "answer" after that i've inserted four fields namely 'id', 'A1','A2', 'A3'.

I inserted the value in id=1 which are A1=1,A2 and A3-0,

In id=2, i have inserted A1=0, A2=1, A3=0

In id-3, i have inserted A1 and A2=0 A3=1

So now what i wanted is whenever i will click on the link of id=1 then it will display the content of id=1 and so on... What i've done so far are:-

     $conn= mysql_connect("localhost","root", "");
      $db= mysql_select_db("dynamic", $conn);
      $id=$_GET['id'];
      $sql= "select * from answer order by id";
      $query= mysql_query($sql);

       while($row=mysql_fetch_array($query, MYSQL_ASSOC))
      {
          echo "<a href='dynamic.php?lc_URL=".$row['id']."'>Click Here</a>"; 
          if($row['A1']==1)
          {
              echo "A1 is 1";
          }
          else if($row['A2']==1)
          {
              echo "A2 is 1";

          }
          else if($row['A3']==1)
          {
              echo "A3 is 1";
          }
          else {
              echo "Wrong query";

            }

        }

       ?>

When i've executed this codes then it is showing me the exact id and it is going to the exact id but the values has not been changing..

I want whenever i will click on the id then it will display the exact value like if i click on id=2 then it will echo out "A2 is 1" nothing else....

Can anyone please help me out?

I also have noticed about

         $id=$_GET['id'];

what is it and how to use it. Can anyone explain me out.. Thanks alot in advance:)

  • 1
    The first thing you need to distinguish - is that `'` and ` are different characters – zerkms Nov 05 '12 at 06:44
  • Ya, i know the difference between them but ryt now i`m expecting the answer of my question if u will be able to help me out! –  Nov 05 '12 at 06:45
  • 3
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Nov 05 '12 at 06:57
  • `else if($row['A3==1'])` should be `else if($row['A3']==1)` Also about Get method, See here [link](http://php.net/manual/en/reserved.variables.get.php) – Lao Nov 05 '12 at 09:09

1 Answers1

0

It may be best to start here to get a good understanding of php, before diving so deep. But to answer the specific questions you asked here...

The php $_GET variable is defined pretty well here:

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".

What this means is that any parameters passed via the query string (on a GET request) in the URL will be accessible through the $_GET variable in php. For example, a request for dynamic.php?id=1 would allow you to access the id by $_GET['id'].

From this we can derive a simple solution. In the following solution we use the same php page to show the list of items from the answer table in your database or single row if the id parameter is passed as part of the url.

<!DOCTYPE HTML>
<html>
  <head>
  </head>
  <body>
  <?php
    $mysqli = new mysqli("localhost", "user", "password", "dynamic");

    $query = 'SELECT * FROM answer';
    if ($_GET['id']) {
      $query .= ' WHERE id = '.$_GET['id'];
    } else {
      $query .= ' ORDER BY id';
    }
    $res = $mysqli->query($query);

    if ($res->num_rows == 0) {
      echo '<p>No Results</p>';
    } else if ($res->num_rows == 1) {
      // Display Answer
      $row = $res->fetch_assoc();
      echo '<h3>Answer for '.$row['id'].'</h3>';
      echo '<ul>';
      echo '<li>A1 = '.$row['A1'].'</li>';
      echo '<li>A2 = '.$row['A2'].'</li>';
      echo '<li>A3 = '.$row['A3'].'</li>';
      echo '</ul>';
    } else {
      // Display List
      echo '<ul>';
      while ($row = $res->fetch_assoc()) {
        echo '<li><a href="dynamic.php?id='.$row['id'].'">Answers for '.$row['id'].'</a></li>';
      }
      echo '</ul>';
    }
  ?>
  </body>
</html>

OK, this might not be exactly what you are looking for, but it should help you gain a little better understanding of how things work. If we add a little javascript to our page then we can show/hide the answers without using the GET parameters and the extra page request.

<!DOCTYPE HTML>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  </head>
  <body>
  <?php
    $mysqli = new mysqli("localhost", "user", "password", "dynamic");

    $query = 'SELECT * FROM answer ORDER BY id';
    $res = $mysqli->query($query);

    if ($res->num_rows == 0) {
      echo '<p>No Results</p>';
    } else {
      // Display List
      echo '<ul>';
      while ($row = $res->fetch_assoc()) {
        echo '<li><a href="javascript:toggleAnswers('.$row['id'].')">Answers for '.$row['id'].'</a>';
        echo '<ul id="answers_'.$row['id'].'" style="display:none;">';
        echo '<li>A1 = '.$row['A1'].'</li>';
        echo '<li>A2 = '.$row['A2'].'</li>';
        echo '<li>A3 = '.$row['A3'].'</li>';
        echo '</ul>';
        echo '</li>';
      }
      echo '</ul>';
    }
  ?>

    <script>
      function toggleAnswers(answer) {
        $('#answers_' + answer).toggle();
      }
    </script>
  </body>
</html>

There are many more solutions, each more complicated that what I've presented here. For example we could set up an ajax request to load the answers into the list page only when an item is clicked. My advice is to go through some beginner tutorials on php and look at some of the popular PHP frameworks: Zend, CodeIgniter, CakePHP, etc. Depending on what you overall goal is, one of these might really help you get there faster.

Be warned that the code provided here is only an example of how to accomplish what you were asking. It definitely does not follow all (if any) best practices.

Jonathan Dixon
  • 2,316
  • 23
  • 29