By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

displaying check box inside row query

ddtpmyra
100+
P: 333
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Connect to the database
  3. $dbLink = mysql_connect("xxx.xxx.xx.x", "xxx", "xxx")
  4. or die("Error! Failed to connect to the MySQL server!");
  5. mysql_select_db("databasename", $dbLink)
  6. or die("Error! Failed to select a database!");
  7.  
  8. # Query for a list of all existing files
  9. $result = mysql_query("SELECT name, age, approved FROM table1");
  10.  
  11. # Check if it was successfull
  12. if($result)
  13. {
  14. # Make sure there are some files in there
  15. if(mysql_num_rows($result) == 0) {
  16.     echo "<p>There are no files in the database</p>";
  17. }
  18. else
  19. {
  20.     # Print the top of a table or header
  21.     echo "<table width='100%'><tr>";
  22.     echo "<td><b>Name</b></td>";
  23.     echo "<td><b>Age</b></td>";
  24.     echo "<td><b>Click the box to approved</b></td>";        
  25.  
  26.     # Print each data
  27.     while($row = mysql_fetch_assoc($result))
  28.     {
  29.         # Print file info
  30.         echo "<tr border=10><td>". $row['name']. "</td>";
  31.         echo "<td>". $row['age']. "</td>";
  32.         #how could I add a check box here?
  33.         echo "</tr>";
  34.     }
  35.  
  36.     # Close table
  37.     echo "</table>";
  38. }
  39. }
  40. else
  41. {
  42. echo "Error! SQL query failed:";
  43. echo "<pre>". mysql_error($dbLink) ."</pre>";
  44. }
  45.  
  46. # Close the mysql connection
  47. mysql_close($dbLink) ;
  48. ?>
Sep 2 '08 #1
Share this Question
Share on Google+
14 Replies


pbmods
Expert 5K+
P: 5,821
Heya, ddtpmyra.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]

Thanks for your time.

pbmods
Sep 2 '08 #2

ddtpmyra
100+
P: 333
anybody can help?

All I need is to insert a text box inside the row
Sep 4 '08 #3

100+
P: 190
could you explain little bit brief.... Why u can't add the checkbox in to loop.....
Sep 5 '08 #4

ddtpmyra
100+
P: 333
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%'><tr>";
echo "<td><b>Created</b></td>";
echo "<td><b>File Name</b></td>";
echo "<td><b>Requestor</b></td>";
echo "<td><b>Author</b></td>";
echo "<td><b>Deadline Feedback</b></td>";
echo "<td><b>Approved</b></td>";
echo "<td><b>Category</b></td>";
echo "<td><b>Click the box to approved</b></td>"; echo "<td><b>&nbsp;</b></td>";
echo "</tr>";

# #Print each file
while($row = mysql_fetch_assoc($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['DeadLineFeedback']. "</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]
Sep 10 '08 #5

100+
P: 190
Hey could said where u can face the problems..?

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

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. #
  4. # Print the top of a table LABELINGS
  5. #
  6.                             echo "<table width='100%'><tr>";
  7. #
  8.                             echo "<td><b>Created</b></td>";
  9. #
  10.                             echo "<td><b>File Name</b></td>";
  11. #
  12.                             echo "<td><b>Requestor</b></td>";
  13. #
  14.                             echo "<td><b>Author</b></td>";
  15. #
  16.                             echo "<td><b>Deadline Feedback</b></td>";
  17. #
  18.                             echo "<td><b>Approved</b></td>";
  19. #
  20.                             echo "<td><b>Category</b></td>";                           
  21. #
  22.     echo "<td><b>Click the box to approved</b></td>";                           echo "<td><b>&nbsp;</b></td>";
  23. #
  24.                             echo "</tr>";
  25. #
  26.          $res="select * from app";
  27.          $result=mysql_query($res);
  28. #
  29.                             # #Print each file
  30. #                            
  31.                             $i=0;
  32.                             while($row = mysql_fetch_assoc($result))
  33. #
  34.                             {
  35. #
  36.                            $i++;
  37. #
  38. # Print file info FROM THE MYSQL TABLE
  39. #
  40.                                 echo "<tr border=10><td>". $row['Created']. "</td>";
  41. #
  42.                                 echo "<td>". $row['File Name']. "</td>";
  43. #
  44.  
  45. #
  46.                                 echo "<td>". $row['Requestor']. "</td>";
  47. #
  48.                                 echo "<td>". $row['Author']. "</td>";
  49. #
  50.                                 echo "<td>". $row['Deadline Feedback']. "</td>";
  51. #
  52.                                 echo "<td>". $row['Approved']. "</td>";
  53. #
  54.                                 echo "<td>". $row['Category']. "</td>";
  55.                                 echo "<td><input type=\"checkbox\" name=\"chkid_$i\"></td>";
  56. #
  57.  
  58. #
  59.  #THIS IS WHERE WHERE I WANTED TO PUT THE CHECK BOX        echo "<td><input type="text"></td>";
  60. #
  61.                                 #Print download link
  62. #
  63.                                 echo "<td><a href='get_file.php?id=". $row['FileID'] ."'>Download";
  64. #
  65.                                 echo "</tr>";
  66. #
  67.                             }
  68.                             ?>
  69.  
Sep 11 '08 #6

ddtpmyra
100+
P: 333
Hey Maheswaran it works thanks a ton... its the syntax all a long :)
Sep 11 '08 #7

ddtpmyra
100+
P: 333
maheswaran,

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

thanks!
Sep 15 '08 #8

100+
P: 190
for that am incresed the checkbox name with $i

Expand|Select|Wrap|Line Numbers
  1. <input type=\"checkbox\" name=\"chkid_$i\">
  2.  
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)
Expand|Select|Wrap|Line Numbers
  1. for($j=0;$j<$i;$j++)
  2. {
  3. $chkid=$_POST['$chkid'_$j'];
  4. $qry="update qry";
  5. $res=mysql($qry,$db);
  6. }
Sep 17 '08 #9

ddtpmyra
100+
P: 333
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!
Sep 17 '08 #10

100+
P: 190
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...

Expand|Select|Wrap|Line Numbers
  1.                      $i=$count_rows;
  2.                         for($j=0;$j<$i;$j++)
  3.                         {
  4.                         $chkid=$_POST['chkid'_$j'];
  5.                         $qry="update filestorage set Approved='Y'";
  6.                         $res=mysql_query($qry);
  7.                         }  
  8.  
Sep 18 '08 #11

ddtpmyra
100+
P: 333
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='approvals_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("SELECT filebaname, approved from filestorage");


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


# Print each file
while($row = mysql_fetch_assoc($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($dbLink);

?>
<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_query($qry);
}

# Close the mysql connection
mysql_close($dbLink);
?>
[/PHP]
Sep 18 '08 #12

ddtpmyra
100+
P: 333
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_query($qry);
}


[/PHP]
Sep 18 '08 #13

100+
P: 190
just add where conditions in that
Sep 19 '08 #14

P: 4
Try this
Expand|Select|Wrap|Line Numbers
  1.  while($row = mysql_fetch_assoc($result))
  2.     {
  3.         # Print file info
  4.         echo "<tr border=10><td>". $row['name']. "</td>";
  5.         echo "<td>". $row['age']. "</td>";
  6.         echo"<input type='checkbox' name='check' name='value' id='check'>";
  7.         echo "</tr>";
  8.     }
Jun 28 '14 #15

Post your reply

Sign in to post your reply or Sign up for a free account.