Jump to content

Recommended Posts

Hey all,

 

This question is coming forth of another topic, but since that topic is not really about this issue, I posted this new topic.

 

Okay, so I made a table, and each row is generated with a WHILE loop.

In this while loop, there's also a form generated for each row/record, for updating them seperately.

 

I'm having trouble with naming those forms to process them seperately.

I could name them like name="form20110001", with the number being the record's id.

But since there can be gaps between id's, how can I retreive them efficiently?

And I want them to process all on a single process page, obviously.

 

How can I do this easily?

Can someone give me a concise example?

Would appreciate it a lot.

Thanks

 

This is my code so far:

 

<?php
$checkboxFields = array(
	'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved',
	'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved',
	'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online');

while ($row = mysql_fetch_array($result)) {
	$formHTML  = "<td class=\"customer\">{$row['project']}</td>\n";
	$formHTML .= "<td class=\"customer\">{$row['customer']}</td>\n";

	foreach($checkboxFields as $fieldName) {
		$checked = ($row[$fieldName]) ? ' checked="checked"' : '';
		$formHTML .= "<td class=\"data\"><input class=\"checkbox\" type=\"checkbox\" name=\"{$fieldName}\"{$checked} /></td>\n";
	}

	// OUTPUT
	echo "<form name=\"form{$row['project']}\" class=\"checkbox_form\" action=\"process_form.php\">\n";
	echo "<input type=\"hidden\" name=\"project\" value=\"{$row['project']}\" />\n";
	echo "<tr>\n";
	echo $formHTML;
	echo "<td><input type=\"submit\" name=\"submit\" value=\"Update\" /></td>\n";
	echo "</tr>\n";
	echo "</form>\n";
}
?>

 

The content isn't really of importance, it's just about processing the multiple generated forms to one process file.

I also can't really find a tutorial about this, which is very strange. This is a common thing used, right?

If you are making a separate form for each row of data (your current code), by definition, that means that you can only submit one at a time. The id you put into the hidden field would uniquely identify which row you would update in the database table.

Yesterday it seemed so obvious..

 

But while coding this, the problem comes back.

 

So I have one big form.

Inside it, I generate rows/records with a while loop.

In that while loop, I generate fields with a foreach loop.

 

Those fields have the same name in each row/record. And that's my problem now, when posting the data.

 

How do I solve this efficiently?

I'm breaking my head on this one.  :confused:

So I have one big form

 

If you changed the method from what you are doing in the code you posted (one form for each row of data, not one big form holding all the data), you would need to post your current code.

I was just about to add my code to my previous post, but my time expired...  :)

But if it could be done with multiple forms fairly easy, I would prefer that actually.

It's just the problem about naming those generated forms/inputs, to process them according to their ID (project)

 

Here's the code:

 

<?php
$get_all = "SELECT * FROM checklist ORDER BY project ASC;";

include_once('connection.inc.php');
$con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error());

mysql_select_db("cre8") or die ("Can't open database: " . mysql_error());

$result = mysql_query($get_all) or die ("Query failed: " . mysql_error());
$num = mysql_numrows($result);

if($num < 1) {
	echo '<p class="info">No records in database.</p>';
}
else {
	echo '<p class="info">Amount of records: '.$num.'</p>';
?>
<form name="check_form" class="checkbox_form" action="check_process.php">
	<input class="submit_btn" type="submit" name="submit" value="Update" />
	<table id="check_table">
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<colgroup></colgroup>
		<tr>
			<td class="vert"> </td>
			<td class="vert"> </td>
			<td class="vert"><p>fact-sent</p></td>
			<td class="vert"><p>fact-payed</p></td>
			<td class="vert"><p>domain</p></td>
			<td class="vert"><p>content</p></td>
			<td class="vert"><p>design-sent</p></td>
			<td class="vert"><p>design-approved</p></td>
			<td class="vert"><p>design-sliced</p></td>
			<td class="vert"><p>temp-website</p></td>
			<td class="vert"><p>temp-website-sent</p></td>
			<td class="vert"><p>temp-website-approved</p></td>
			<td class="vert"><p>cms</p></td>
			<td class="vert"><p>seo</p></td>
			<td class="vert"><p>analytics</p></td>
			<td class="vert"><p>webmaster-tools</p></td>
			<td class="vert"><p>website</p></td>
			<td class="vert"><p>website-online</p></td>
		</tr>				
<?php
$checkboxFields = array(
	'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved',
	'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved',
	'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online');

while ($row = mysql_fetch_array($result)) {
	$formHTML  = "<td class=\"customer\">{$row['project']}</td><input type=\"hidden\" name=\"project[]\" value=\"{$row['project']}\" />\n";
	$formHTML .= "<td class=\"customer\">{$row['customer']}</td>\n";

	foreach($checkboxFields as $fieldName) {
		$checked = ($row[$fieldName]) ? ' checked="checked"' : '';
		$formHTML .= "<td class=\"data\"><input class=\"checkbox\" type=\"checkbox\" name=\"{$fieldName}\"{$checked} /></td>\n";
	}

	// OUTPUT
	echo "<tr>\n";
	echo $formHTML;
	echo "</tr>\n";
}
?>
	</table>
</form>
<?php
}
?>

 

 

And with this script, I can't even echo out the posted ID's (projects). I have a blank page, containing the data in the url.

 

<?php
$checkboxFields = array(
	'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved',
	'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved',
	'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online');

if(isset($_POST['submit'])) {

	$projects = $_POST['project'];
	$projects = implode(', ', $projects);

	echo "<p>{$projects}</p>";
}
?>

In your other thread for this problem, mjdamato both told and showed how to use one big form and identify which record the checkbox belong with -

 

If you want one form to update many records, the solution is simply. Name the fields so that they are arrays with the index of each field the record id.

<input type="checkbox" name="fieldname[recordID]" />

 

The fieldname portion of that would be your $fieldName variable. The recordID would apparently be from $row['project']

Damn, I totally forgot about that post, as the thread went on.

 

Okay, so I modified my code to that.

 

But for the processing part, I need to link the fieldNames with the corresponding ID's.

I have no idea how I would build a query upon that.

 

The process script so far:

<?php
$checkboxFields = array(
	'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved',
	'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved',
	'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online');

if(isset($_POST['submit'])) {

	include_once('connection.inc.php');
	$con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error());
	mysql_select_db("cre8") or die ("Can't open database: " . mysql_error());

	foreach($checkboxFields as $fieldName) {
		$field = $_POST[$fieldName];
	}
	// $field = array(20110001, 20110001, ..., 20110002, ...);  CORRECT ?
	// How do I link this array with the $checkboxFields array, to build an update-query for each input?

	echo "<p>Hello</p>";    // The page remains blank ?!
}
?>

 

Also, after submitting the form, I can't echo anything out. What's up with that?

 

I really appreciate your help man.

Basically, you could do this -

<?php
$checkboxFields = array(
	'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved',
	'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved',
	'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online');

echo "<pre>",print_r($_POST,true),"</pre>";

if(isset($_POST['submit'])) {
	$pivot = array(); // swap the columns and rows
	foreach($checkboxFields as $fieldName){
		if(isset($_POST[$fieldName])){ // at least one checkbox is set
			foreach($_POST[$fieldName] as $project=>$dummy){
				$pivot[$project][] = $fieldName;
			}
		}
	}

	echo "<pre>",print_r($pivot,true),"</pre>";

	foreach($pivot as $project=>$array){
		$fields = array();
		foreach($checkboxFields as $fieldName){
			if(in_array($fieldName,$array)){
				$fields[] = "`$fieldName` = TRUE"; // whatever value you are using to indicate checked
			} else {
				$fields[] = "`$fieldName` = NULL"; // whatever value you are using to indicate not-checked
			}
		}
		$query = "UPDATE your_table SET " . implode(',',$fields) . " WHERE project = $project";
		echo $query . "<br />";
		// execute your query here
	}
}

Hmm, seems like a big connection of arrays!

Could you please explain me in detail what the code is doing exactly?

For instance, what's the $dummy used for?

 

As of now, it echoes an empty array:

Array

(

)

 

Thank you for the great effort! Really appreciate it.

Omg, how in the world could I forget that?!  :o

I feel ashamed you had to find out something as silly as that was missing.

 

But wow.. that code is fantastic! It works like a charm now!  :D

Thank you soo much!

Also for echoing out the print_r's, really helpful!

 

I've already asked a lot, but, just out of curiosity.. Is it possible to NOT update a record when it isn't modified, since its last update?

 

Again, thanks a lot man, really appreciate it!  :)

 

Guess I got to learn a lot about nested loops and arrays.  :P

NOT update a record when it isn't modified

 

You would need to retrieve the current data (or pass it through the form somehow), then compare the original values with the submitted values. Either method would all take extra time and more code.

 

However, there's little point in doing that because if you try to UPDATE a value to the same value it already is, mysql doesn't actually go to the trouble of writing the value to the database. So, if none of the values in a row are changed, yes you would still be executing an UPDATE query, but mysql would short-circuit the process and not actually take the time to write the row back into the database.

 

If you set a column to the value it currently has, MySQL notices this and does not update it.
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.