ethought Posted March 23, 2008 Share Posted March 23, 2008 Hi All - Got a problem that I cannot seem to find a fix for. function list_edit($dbname,$dbfield,$submitbutton,$newvalue,$deletebutton,$deletevalues) { include("db.php"); if(isset($submitbutton)){ $newtype = $newvalue; if($newtype == ""){ echo "You did not enter anything...? Please go back and try again.<br>"; } else { $updatedb = mysql_query("INSERT INTO $dbname ( `id` , $dbfield ) VALUES ('','$newtype')", $db); if($updatedb){ echo "PLant class Added Successfully. <br>"; } else { echo "There was an error adding the plant class please contact admin to report error"; } } } if(isset($deletebutton)){ foreach($deletevalues as $id){ $dbname = mysql_query("DELETE FROM $dbname WHERE id='$id'", $db); if($deletetype){ echo "Plant db name with id of $id has been deleted... <br>"; } else { echo "There was a problem deleting plant $dbname id $id <br>"; } } } } ### function for output of all known list vars and delete and add parts function list_display($title,$dbname,$deletevar,$dbfield,$deletebutton,$newvalue,$addbutton){ include("db.php"); ?> <h3><? echo $title ?></h3> <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data"> <table class=neat> <? $i = 0; $gettypes = mysql_query("SELECT * FROM $dbname", $db); while($types = mysql_fetch_array($gettypes)){ $id = $types[id]; echo "<tr><td><input type='checkbox' name=$deletevar[$i] value='$id'></td><td>$types[$dbfield] - Delete Var is $deletevar[$i]</td></tr>"; $i++; } echo "</table>"; echo "<input type=submit name='$deletebutton' value='Delete'>"; ## ADD FORM echo "<table class=neat>"; echo "<tr><td>Add $title</td><td><input type=text name='$newvalue' size='40'> <input type=submit name='$addbutton' value='Add'></td></tr>"; echo "</table>"; echo "</form>"; } The thing is in the list_display function when the function names the $deletevar[$i] variables which assign a delete id to each selected plant in the form, instead of acting like normal so that $deletevar = "deleteid"; $i = 0; while(.......){ echo "<tr><td><input type='checkbox' name=$deletevar[$i] value='$id'></td><td>$types[$dbfield] - Delete Var is $deletevar[$i]</td></tr>"; } would give me: deleteid[0]; deleteid[1]; deleteid[2]; I am getting d e l e t e as my posted delete id names.... I hope someone can understand what I mean and point out where I am going wrong. Basically I just want to assign the value to the form of: deleteid[1] deleteid[2] etc... at the moment it seems to be pulling character x from deleteid[x] so my posted variables are like $e = 2 $l = 3 ??? Link to comment https://forums.phpfreaks.com/topic/97505-problem-with-string-in-a-function-acting-as-array/ Share on other sites More sharing options...
Barand Posted March 23, 2008 Share Posted March 23, 2008 why don't you just name like this <input type='checkbox' name='delete[]' value='$id'> Link to comment https://forums.phpfreaks.com/topic/97505-problem-with-string-in-a-function-acting-as-array/#findComment-498910 Share on other sites More sharing options...
ethought Posted March 24, 2008 Author Share Posted March 24, 2008 The name of the checkbox needs to be dynamic, otherwise all my checkboxes for all 20 or so sections will have the same name, which will not work for what I am doing. I want to run through and basically use: foreach($_POST[deleteids] as $did){ Do STUFF; } foreach($_POST[deletetypeids] as $dtid){ Do STUFF; } Works perfectly when not using in a function. If I just use: <input type='checkbox' name='$deleteid[]' value='$id'> in the function I get Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in ... Link to comment https://forums.phpfreaks.com/topic/97505-problem-with-string-in-a-function-acting-as-array/#findComment-499188 Share on other sites More sharing options...
ethought Posted March 24, 2008 Author Share Posted March 24, 2008 OK figured it out - Just needed to add deleteid[] to the function call NOT within the function! Link to comment https://forums.phpfreaks.com/topic/97505-problem-with-string-in-a-function-acting-as-array/#findComment-499221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.