Jump to content

Recommended Posts

My web app already takes all the data and it's stored in a DB where I will process things.

 

What I want to do is load the data in to a <table> somehow and have checkbox form elements at the end of each row.

 

Then on the screen, there will be a button to click for processing. Processing will go through all the checked rows and do something based on the data behind it : like in this case, it'll build a file and mark all rows as "processed" in the DB.

 

I'm not quite understanding the best way to generate this table, and then going through it and knowing what is what as I process - and the whole relation  between what I loaded and how to know which records to process.

 

Can anyone assist?

 

Thanks for any help,

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/82344-table-from-database-for-processing/
Share on other sites

This is a basic example I took from my script:

 

<?php

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>

<td bgcolor="#FFFFFF"><img src="images/thumbs/<? echo $rows['Filename']; ?>"></td>

<td bgcolor="#FFFFFF"><? echo $rows['Make']; ?></td>

<td bgcolor="#FFFFFF"><a href="viewspecificcar.php?id=<? echo $rows['id']; ?>"><? echo $rows['Model']; ?></a></td>

<td bgcolor="#FFFFFF"><? echo $rows['Year']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['Color']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['VIN']; ?></td>

<td bgcolor="#FFFFFF"><a href="editcar?id=<? echo $rows['id']; ?>">Edit<a/>

<?php

}

?>

 

so it creates a table depending on the number of rows in the db table or the result of the query and goes from there, as you see in this table, it will add a checkbox and a link. Modify as you see necessary.

Well your first task is generating the table, for that you will need to:

  • Grab all the records
  • Echo the table header
  • for each record
     

    •    
  • echo a table row
     

 

[*]echo the table footer

 

Do you know how to do any of that?  If not, you may want to look at some general PHP / MySQL tutorials first.

  • 2 weeks later...

Thank you for your tips and tricks.. Here's what I've come up with ....

$statement is my query result and it's currently building the table just fine...

 

So - in order to be able to check on submit which rows were checked and which to process, I take it I need to make individual IDs for each checkbox and check the IDs on submit?

 

Anyone have any tips on this please?

 

Thanks,

Adam

 

<form action="processfile.php" method="POST">
  <?php
  if ( isset ( $statement ))
  {
    echo "<table>";

    echo "<tr>" .
         "<td>Process Order</td>" .	
         "<td>OrderID</td>" .
         "<td>ShipBusinessName</td>"
    . "</tr>";

    while($row = mysql_fetch_array($statement)) {
  echo "<tr>" .
  
         "<td> <input type=\"checkbox\" name=\"Process Record\" value=\"Process\"> </td>" .
		 	  
         "<td>" . $row['OrderID'] . "</td>" .
         "<td>" . $row['ShipFirstName'] . ' ' . $row['ShipLastName'] . "</td>" .
	       "</tr>";
    }
echo "</table>";
  }
?>
</form>

I've changed things a little ...

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">

 

Is how I am going to trigger things to re-process in the top part of the file itself.

 

So -

I don't know how to loop through all checkbox_x values in the $POST var dynamically. How do I do this in PHP?

 

I am going to a confirmation page after this, but I want to do all the processing in the top of the pHP page itself - that's why I changed the form tag.

 

Any advice please?

 

Thanks,

 

Adam

 

In working, I've tried this :

 

$sql = 'select * from origin where BillCountry = \'United States\' and Processed = 0';
$statement = mysql_query($sql);

if (isset($_POST['submit'])) 
  {
    $i = 0;
    while($row = mysql_fetch_array($statement)) {  //<<<< To me, this should be a for loop based on the HTML on the screen or something .. running the sql again to build the HTML seems silly... 
   $i = $i + 1;
   echo "value for each row " . $row['ShipFirstName'] . "<br />";
   if ( $_POST['checkbox_' . $i] = 'on' ) //****<<----this really doesn't look right - but I can't figure another way to access the checkboxes?
   {
     echo "checkbox " . $i . " is set";      
   }
     }  
      //header("location: contest.php"); 
  }
else 	
  {
    echo "<br />submit is not set";
  }

 

I don't like it. It assumes too much and seems sloppy to me. Nothing is working anyhow. I am trying to address the checkboxes I create in the HTML form as:

 

<td> <input type=\"checkbox\" name=\"checkbox_" . $i . "\" > </td>"

 

Which are being created nicely...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.