Forums

PHP

This topic is locked

update multiple Frankenstein code

Posted 31 Aug 2003 17:27:26
1
has voted
31 Aug 2003 17:27:26 A D posted:
I've read through all the threads on this forum talking about solutions for this. Some were a little clearer than others. I ended up finding an ASP solution and have tried to translate and use portions of that code with some PHP / Javascript code I've found here and elsewhere. Here's what I've got so far, but I'm stumped.

I have built a dynamic navbar which uses dynamic popup menus. The order of the navbar buttons relys on the position set by the user which in turn could mess up the position of the popup menus (may sound confusing, but it's not that bad). SO. I'm working on a form that will let users update the position of all the navbar buttons at once, allowing me to write an error script stating "HEY! That position number is being used already .. please select a unique value!". I'm able to use Javascript to show which records have been altered, but having problems stripping "*" out of the string and looping the update. The core of this code was modified from Tim's code used to delete multiple records using check boxes. Here's what I've got:

<?php
// If user submits the form update each record altered
if ($HTTP_GET_VARS["update"] == "yes" {
$varRecIDs = str_replace('*', '',$hidRecIDs); // I need to strip the * characters out of the string
$theUpdates = explode(",",$HTTP_POST_VARS["hidRecIDs"]); // if I use explode("," $varRecIDs) the ID number won't show
$theSQL = "UPDATE ecom_navbar WHERE ";
for ($k=0; $k < count($theUpdates); $k++) {
if ($k > 0) {
$theSQL.=" OR ";
}
$theSQL.="ID = '".$theUpdates[$k]."'";
}
echo $theSQL;
$theDelRS = $connTest->Execute($theSQL) or DIE($connTest->ErrorMsg());
}
?>

Replies

Replied 01 Sep 2003 05:02:01
01 Sep 2003 05:02:01 A D replied:
I'm still messing with this and I think I've almost got it. Just have a problem getting the new value from the users input. I know I need to loop something, I'm not sure where. Any records I change are updated with the last records value. So I'm stumped. Not sure if I need to use another Javascript to capture the new value in a hidden field or if I need to alter my PHP script. Any thoughts are GREATLY appreciated. Thx.

<?php
// If user submits the form update each record altered
if ($HTTP_GET_VARS["update"] == "yes" {
$varRecIDs = str_replace('*', '',$HTTP_POST_VARS["hidRecIDs"]); // strip the * characters out
$theUpdates = explode(",",$varRecIDs); // insert ,
$theSQL = "UPDATE ecom_navbar SET n_position = '90' WHERE "; // instead of '90' I need to grab a value submitted by the user
for ($k=0; $k < count($theUpdates); $k++) {
if ($k > 0) {
$theSQL.=" OR ";
}
$theSQL.="nid = '".$theUpdates[$k]."'";
}
echo $theSQL;
mysql_select_db($database_connDe, $connDe);
$Result1 = mysql_query($theSQL, $connDe) or die(mysql_error());
}
?>
Replied 02 Sep 2003 00:18:31
02 Sep 2003 00:18:31 A D replied:
I finally read enought to figure out that my PHP was wrong ... I've got that set so it loops through each updated record. What I can't seem to get (still) is the new value the user enters dumped into a hidden feild as a string so I can split it later.

PHP this part works for the record id # >>
<pre id=code><font face=courier size=2 id=code>
&lt;?php
if ($HTTP_GET_VARS["update"] == "yes" {
$varRecIDs = str_replace('*', '',$HTTP_POST_VARS["hidRecIDs"]); // strip the * characters out
$theUpdates = explode(",",$varRecIDs); // insert ,
for ($k=0; $k &lt; count($theUpdates); $k++) {
$np = $HTTP_POST_VARS["n_position"];
$theSQL = "UPDATE ecom_navbar SET n_position = '".$np."' WHERE nid = '".$theUpdates[$k]."'";
echo $theSQL;
echo $n_position1;
echo $n_position2;
mysql_select_db($database_connDem, $connDem);
$Result1 = mysql_query($theSQL, $connDem) or die(mysql_error());
}
}
?&gt;
</font id=code></pre id=code>
Javascript (I'm thinking I need to alter this to get the new value submitted) &gt;&gt;

<pre id=code><font face=courier size=2 id=code>
function RecUpdate(nid){
var ThisID = "*" + (nid) + "*"
if (document.form1.hidRecIDs.value == ""{ // If the hidden field is empty
document.form1.hidRecIDs.value = (ThisID) // Store the value in the hidden field (hidRecIDs) as it is.
}
if (document.form1.hidRecIDs.value != ""{ // If the hidden field isn't empty
var str = document.form1.hidRecIDs.value; // Store the contents of the hidden field in the variable str
var pos = str.indexOf(ThisID); // Search str to see if this nid is allready in it.
if (pos == -1) { // If the position returned is -1 it isn't allredy in there,
document.form1.hidRecIDs.value = document.form1.hidRecIDs.value + ", " + (ThisID)
} // so add ", " and this ID to what is already in hidRecIDs
} // to create a list like this *2*, *5*, *8* etc.
}
</font id=code></pre id=code>

Reply to this topic