displaying check box inside row query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    displaying check box inside row query

    Hi below is my script displaying all the information inside the table.

    Pupose:
    pupose of this php page is to display all the information on my table and have a check box at the last column that will allow the user to approved the member.

    Problem:
    I don't how to insert a checkbox on a loop condition.

    [code=php]
    <?php
    # Connect to the database
    $dbLink = mysql_connect(" xxx.xxx.xx.x", "xxx", "xxx")
    or die("Error! Failed to connect to the MySQL server!");
    mysql_select_db ("databasename" , $dbLink)
    or die("Error! Failed to select a database!");

    # Query for a list of all existing files
    $result = mysql_query("SE LECT name, age, approved FROM table1");

    # Check if it was successfull
    if($result)
    {
    # Make sure there are some files in there
    if(mysql_num_ro ws($result) == 0) {
    echo "<p>There are no files in the database</p>";
    }
    else
    {
    # Print the top of a table or header
    echo "<table width='100%'><t r>";
    echo "<td><b>Nam e</b></td>";
    echo "<td><b>Age </b></td>";
    echo "<td><b>Cli ck the box to approved</b></td>";

    # Print each data
    while($row = mysql_fetch_ass oc($result))
    {
    # Print file info
    echo "<tr border=10><td>" . $row['name']. "</td>";
    echo "<td>". $row['age']. "</td>";
    #how could I add a check box here?
    echo "</tr>";
    }

    # Close table
    echo "</table>";
    }
    }
    else
    {
    echo "Error! SQL query failed:";
    echo "<pre>". mysql_error($db Link) ."</pre>";
    }

    # Close the mysql connection
    mysql_close($db Link) ;
    ?>[/code]
    Last edited by pbmods; Sep 2 '08, 11:18 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, ddtpmyra.

    Please use CODE tags when posting source code:

    &#91;CODE=ph p]
    PHP code goes here.
    &#91;/CODE]

    Thanks for your time.

    pbmods

    Comment

    • ddtpmyra
      Contributor
      • Jun 2008
      • 333

      #3
      anybody can help?

      All I need is to insert a text box inside the row

      Comment

      • maheswaran
        New Member
        • Mar 2007
        • 190

        #4
        could you explain little bit brief.... Why u can't add the checkbox in to loop.....

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          Hi maheswaran,

          Below again is the script I placed 'comment #' where i wanted to insert a check box. The code will be used for displaying the list of documents coming from mysql database.

          Placing the checkbox inside the loop will allow the admin to approved it by checking the texbox and I'm having trouble of placing the check box inside the loop while displaying the records.

          [PHP]{

          # Print the top of a table LABELINGS
          echo "<table width='100%'><t r>";
          echo "<td><b>Created </b></td>";
          echo "<td><b>Fil e Name</b></td>";
          echo "<td><b>Request or</b></td>";
          echo "<td><b>Aut hor</b></td>";
          echo "<td><b>Deadlin e Feedback</b></td>";
          echo "<td><b>Approve d</b></td>";
          echo "<td><b>Categor y</b></td>";
          echo "<td><b>Cli ck the box to approved</b></td>"; echo "<td><b>&nb sp;</b></td>";
          echo "</tr>";

          # #Print each file
          while($row = mysql_fetch_ass oc($result))
          {

          # Print file info FROM THE MYSQL TABLE
          echo "<td>". $row['Created']. "</td>";
          echo "<tr border=10><td>" . $row['FileName']. "</td>";
          echo "<td>". $row['Created']. "</td>";
          echo "<td>". $row['Requestor']. "</td>";
          echo "<td>". $row['Author']. "</td>";
          echo "<td>". $row['DeadLineFeedba ck']. "</td>";
          echo "<td>". $row['approved']. "</td>";
          echo "<td>". $row['category']. "</td>";

          #THIS IS WHERE WHERE I WANTED TO PUT THE CHECK BOX echo "<td><input type="text"></td>";
          #Print download link
          echo "<td><a href='get_file. php?id=". $row['FileID'] ."'>Download ";
          echo "</tr>";
          }[/PHP]
          Last edited by pbmods; Sep 11 '08, 12:42 AM. Reason: Fixed CODE tags.

          Comment

          • maheswaran
            New Member
            • Mar 2007
            • 190

            #6
            Hey could said where u can face the problems..?

            Am not facing any problem in ur code (i changed some thing see)

            [code=php]
            <?

            #
            # Print the top of a table LABELINGS
            #
            echo "<table width='100%'><t r>";
            #
            echo "<td><b>Created </b></td>";
            #
            echo "<td><b>Fil e Name</b></td>";
            #
            echo "<td><b>Request or</b></td>";
            #
            echo "<td><b>Aut hor</b></td>";
            #
            echo "<td><b>Deadlin e Feedback</b></td>";
            #
            echo "<td><b>Approve d</b></td>";
            #
            echo "<td><b>Categor y</b></td>";
            #
            echo "<td><b>Cli ck the box to approved</b></td>"; echo "<td><b>&nb sp;</b></td>";
            #
            echo "</tr>";
            #
            $res="select * from app";
            $result=mysql_q uery($res);
            #
            # #Print each file
            #
            $i=0;
            while($row = mysql_fetch_ass oc($result))
            #
            {
            #
            $i++;
            #
            # Print file info FROM THE MYSQL TABLE
            #
            echo "<tr border=10><td>" . $row['Created']. "</td>";
            #
            echo "<td>". $row['File Name']. "</td>";
            #

            #
            echo "<td>". $row['Requestor']. "</td>";
            #
            echo "<td>". $row['Author']. "</td>";
            #
            echo "<td>". $row['Deadline Feedback']. "</td>";
            #
            echo "<td>". $row['Approved']. "</td>";
            #
            echo "<td>". $row['Category']. "</td>";
            echo "<td><input type=\"checkbox \" name=\"chkid_$i \"></td>";
            #

            #
            #THIS IS WHERE WHERE I WANTED TO PUT THE CHECK BOX echo "<td><input type="text"></td>";
            #
            #Print download link
            #
            echo "<td><a href='get_file. php?id=". $row['FileID'] ."'>Download ";
            #
            echo "</tr>";
            #
            }
            ?>
            [/code]

            Comment

            • ddtpmyra
              Contributor
              • Jun 2008
              • 333

              #7
              Hey Maheswaran it works thanks a ton... its the syntax all a long :)

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                maheswaran,

                How can I update mysql table when the box are checked?

                thanks!

                Comment

                • maheswaran
                  New Member
                  • Mar 2007
                  • 190

                  #9
                  for that am incresed the checkbox name with $i

                  [code=php]
                  <input type=\"checkbox \" name=\"chkid_$i \">
                  [/code]

                  Just you make the loop for number of rows or count. We knew the total number of records, so pass the this count and do it like

                  (for xample i=5)
                  [code=php]
                  for($j=0;$j<$i; $j++)
                  {
                  $chkid=$_POST['$chkid'_$j'];
                  $qry="update qry";
                  $res=mysql($qry ,$db);
                  }[/code]
                  Last edited by pbmods; Sep 17 '08, 04:15 AM. Reason: Fixed CODE tags.

                  Comment

                  • ddtpmyra
                    Contributor
                    • Jun 2008
                    • 333

                    #10
                    maheswaran
                    please tell me what's wrong with my update it's not working

                    [PHP]
                    <?php

                    # Connect to the database
                    $dbLink = mysql_connect(" localhost", "xxx", "xxx")
                    or die("Error! Failed to connect to the MySQL server!");
                    mysql_select_db ("cmr", $dbLink)
                    or die("Error! Failed to select a database!");



                    # Gather all required data
                    $i=5;
                    for($j=0;$j<$i; $j++)
                    {
                    $chkid=$_POST['$chkid'_$j'];
                    $qry="update filestorage set Approved='Y'";
                    $res=mysql($qry ,$db);
                    }

                    ?>
                    [/PHP]


                    and what if I don't know exactly how many the $i is since the user will add data and delete data daily?

                    thanks!

                    Comment

                    • maheswaran
                      New Member
                      • Mar 2007
                      • 190

                      #11
                      Hey myra,

                      Just for an example am giving $i=5; just you count the number of rows in ur table and place that one (.i.e $i=$count_rows) ..and sorry i mistakenly added $_POST['$chkid'_$j'] instead of $_POST['chkid'_$j'] and change the mysql connection string.. see below... may its help...

                      [code=php]
                      $i=$count_rows;
                      for($j=0;$j<$i; $j++)
                      {
                      $chkid=$_POST['chkid'_$j'];
                      $qry="update filestorage set Approved='Y'";
                      $res=mysql_quer y($qry);
                      }
                      [/code]
                      Last edited by ak1dnar; Sep 18 '08, 11:31 AM. Reason: code tags are like this [code=php] and not like this [php=code]

                      Comment

                      • ddtpmyra
                        Contributor
                        • Jun 2008
                        • 333

                        #12
                        Hi maheswaran... i'ts still not updating my table please help if I missing a link here.

                        Step 1 Displaying the row data
                        [PHP]
                        <form action='approva ls_file3.php' method='post'>
                        <?php


                        # Connect to the database
                        $dbLink = mysql_connect(" localhost", "xxx", "xxx")
                        or die("Error! Failed to connect to the MySQL server!");
                        mysql_select_db ("cmr", $dbLink)
                        or die("Error! Failed to select a database!");

                        # Query
                        $result = mysql_query("SE LECT filebaname, approved from filestorage");


                        # Print the top of a table
                        echo "<td><b>filenam e</b></td>";
                        echo "<td><b>aproved </b></td>";


                        # Print each file
                        while($row = mysql_fetch_ass oc($result))
                        {
                        # Print file info
                        echo "<tr border=10><td>" . $row['filename']. "</td>";
                        echo "<td><input type=\"checkbox \" name=\"chkid_$i \"></td>";
                        }


                        # Close the mysql connection
                        mysql_close($db Link);

                        ?>
                        <input type='submit' value='Submit'>
                        </form>
                        [/PHP]

                        Step 2 Updating the row data

                        [PHP]
                        <?php

                        # Connect to the database
                        $dbLink = mysql_connect(" localhost", "xxx", "xxx")
                        or die("Error! Failed to connect to the MySQL server!");
                        mysql_select_db ("cmr", $dbLink)
                        or die("Error! Failed to select a database!");

                        $i=$count_rows;
                        for($j=0;$j<$i; $j++)
                        {
                        $chkid=$_POST['chkid'_$j'];
                        $qry="update filestorage set Approved='Y'";
                        $res=mysql_quer y($qry);
                        }

                        # Close the mysql connection
                        mysql_close($db Link);
                        ?>
                        [/PHP]

                        Comment

                        • ddtpmyra
                          Contributor
                          • Jun 2008
                          • 333

                          #13
                          This update my table but it updates all the records.
                          Little more help on this.. how can I update the only checked ones?

                          [PHP] $i=10;
                          for($j=0;$j<$i; $j++)
                          {
                          $chkid=$_POST['chkid_$j'];
                          $qry="update filestorage set Approved='Y'";
                          $res=mysql_quer y($qry);
                          }


                          [/PHP]

                          Comment

                          • maheswaran
                            New Member
                            • Mar 2007
                            • 190

                            #14
                            just add where conditions in that

                            Comment

                            • johndavid19
                              New Member
                              • Jun 2014
                              • 4

                              #15
                              Try this
                              Code:
                               while($row = mysql_fetch_assoc($result))
                                  {
                                      # Print file info
                                      echo "<tr border=10><td>". $row['name']. "</td>";
                                      echo "<td>". $row['age']. "</td>";
                                      echo"<input type='checkbox' name='check' name='value' id='check'>";
                                      echo "</tr>";
                                  }

                              Comment

                              Working...