swatisonee Posted December 19, 2006 Share Posted December 19, 2006 Hi,I use the following 3 scripts but i think there's got to be a more efficent and simpler way to code. Could someone let me know how i can modify these 3 to have them all work on 2 pages if not 1 ?a)1.php selects a record from Table A.b)2.php selects multiple records from Table B.c)3.php updates all chosen records of Table B with the single value of Table A1.php====[code]<?include ("../include/session.php");require ("../include/bottom.php");include ("../include/ticker.php");?><?if(!isset($_SESSION['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;}$userid = $_SESSION['userid'];echo $userid;?><form method="post" action="2.php?userid=<? echo $userid ?>"><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Custgroup` ORDER BY `Group` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CGID"], $myrowa["Group"]); } while ($myrowa = mysql_fetch_array($resulta));}?></table><p><input type="submit" value="Select"></form>[/code]2.php====[code]<?include ("../include/session.php");require ("../include/bottom.php");include ("../include/ticker.php");?> <?if(!isset($_SESSION['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;}$userid = $_SESSION['userid'];$group = $_POST["choice"];echo $userid;?><b><p><font face="Tahoma" size="2" color="#0000FF">These are the customers in your database . Selected those you wish to link to a single group . </b></font><p><table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%"><tr bgcolor="#DEEDFE"><form method="post" action="3.php?userid=<? echo $userid ?>"><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Customers` ORDER BY `Company` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"checkbox\" name=\"customer[]\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CID"], $myrowa["Company"]); } while ($myrowa = mysql_fetch_array($resulta));} else{echo "No Records Found" ;}?></table><input type="hidden" name="group" value="<? echo $group?>"><p><input type="submit" value="Select"></form>[/code]3.php[code]<?include ("../include/session.php");require ("../include/bottom.php");include ("../include/ticker.php");?> <?if(!isset($_SESSION['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;}$userid = $_SESSION['userid'];$group = $_POST["group"];$allcust = implode(',', $_POST['customer']);echo $userid;echo $allcust;I then list all the values of the $allcust, and update each record with $group.$sql = " UPDATE Customer SET CGID = $group WHERE CID = .....?>[/code] Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted December 19, 2006 Share Posted December 19, 2006 This smells like a dynamic form select menu (although I'd imagine radio would work, too). Holler if this isn't what you're looking for.http://www.phpfreaks.com/quickcode/Building-a-Dynamic-Form-Select-Menu/2.php Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 19, 2006 Author Share Posted December 19, 2006 I had looked at this earlier actually but i could not figure out how to use it for multiple tables especially as i need to select multiple values from one of them. I'm stuck trying to figure out how i can select from both tables if the php self loads just once. I dont know how to create a function for selecting multiple records.Any pointers? Thanks. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted December 19, 2006 Share Posted December 19, 2006 it depends on how many options the first form contains. If there are only two, just set two different variables to the two possible queries and dynamically change the form data when the selection changes. If there are ten or twelve options, you might consider using AJAX methodology.This wouldn't actually eleminate the three separate forms, but the end-user would only see the first one. To him, it would just look like one really smart form.You can probably dig around on the AJAX forum here on phpfreaks and get a pretty good idea of how to accomplish this. Here's the AJAX intro:http://www.phpfreaks.com/forums/index.php/topic,115581.0.html Quote Link to comment Share on other sites More sharing options...
craygo Posted December 19, 2006 Share Posted December 19, 2006 can always put everything on one page add a couple hidden values and a couple if thens.[code]<?phpinclude ("../include/session.php");require ("../include/bottom.php");include ("../include/ticker.php");$self = $_SERVER['PHP_SELF'];// Check if user is logged inif(!isset($_SESSION['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;}$userid = $_SESSION['userid'];echo $userid;// See what pageif(isset($_POST['p3'])){// page 3 contentecho $userid;echo $allcust;//I then list all the values of the $allcust, and update each record with $group.$sql = " UPDATE Customer SET CGID = $group WHERE CID = .....";} else { if(isset($_POST['p2'])){ // Page 2 content?><b><p><font face="Tahoma" size="2" color="#0000FF">These are the customers in your database . Selected those you wish to link to a single group . </b></font><p><table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%"><tr bgcolor="#DEEDFE"><form method="post" action="<?=$self?>?userid=<? echo $userid ?>"><input type=hidden name=p3 value=gp /><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Customers` ORDER BY `Company` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"checkbox\" name=\"customer[]\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CID"], $myrowa["Company"]); } while ($myrowa = mysql_fetch_array($resulta));} else{echo "No Records Found" ;}?></table><input type="hidden" name="group" value="<? echo $group?>"><p><input type="submit" value="Select"></form><?php } else { // Page 1 content?><form method="post" action="<?=$self?>?userid=<? echo $userid ?>"><input type=hidden name=p2 value=go /><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Custgroup` ORDER BY `Group` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CGID"], $myrowa["Group"]); } while ($myrowa = mysql_fetch_array($resulta));}?></table><p><input type="submit" value="Select"></form><?php }}?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 19, 2006 Author Share Posted December 19, 2006 This is great Ray but i didnt understand why the / comes at the end of this line :[code]<input type=hidden name=p3 value=gp />[/code]Another thing : Instead of Select * from Page 2, if I wanted to select only those names starting with say Del (Deloitte, Delaney) , how would i amend the form ?I would have to add something on the lines of :Type the first few letters of the names you wish to chooseName <input type="text" value="coname" size="20">The idea being if someone knows that several Delaneys are going to pop up, then they can type del, all of which are then checked as customer[]. But if they dont know theres a Delaney out there, they need to call up all the records in Table Customer.Thanks a lot ! Quote Link to comment Share on other sites More sharing options...
craygo Posted December 19, 2006 Share Posted December 19, 2006 The /> is just a way some programs end the tag. It is not required. My program happens to do it.Well now you are looking at a search field. you can make a new form at the top of page one[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Hello!</title></head><body><?phpinclude ("../include/session.php");require ("../include/bottom.php");include ("../include/ticker.php");$self = $_SERVER['PHP_SELF'];// Check if user is logged inif(!isset($_SESSION['userid'])){ echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>"; exit;}$userid = $_SESSION['userid'];echo $userid; // See what page we loadif(isset($_POST['p3'])){// page 3 contentecho $userid;echo $allcust;//I then list all the values of the $allcust, and update each record with $group.$sql = " UPDATE Customer SET CGID = $group WHERE CID = .....";} else { if(isset($_POST['p2'])){ // Page 2 content?><b><p><font face="Tahoma" size="2" color="#0000FF">These are the customers in your database . Selected those you wish to link to a single group . </b></font><p><table border="1" style="border-collapse: collapse" bordercolor="#111111" width="100%"><tr bgcolor="#DEEDFE"><form method="post" action="<?=$self?>?userid=<? echo $userid ?>"><input type=hidden name=p3 value=gp /><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Customers` ORDER BY `Company` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"checkbox\" name=\"customer[]\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CID"], $myrowa["Company"]); } while ($myrowa = mysql_fetch_array($resulta));} else{echo "No Records Found" ;}?></table><input type="hidden" name="group" value="<? echo $group?>"><p><input type="submit" value="Select"></form><?php } else { // Search page content if(isset($_POST['search'])){ // code to search by keyword $keyword = $_POST['keyword']; $sql = "SELECT * FROM `Customers` WHERE company LIKE '%$keyword%' ORDER BY `Company` ASC"; // rest of code below } else { // Page 1 content?><form method="post" action="<?=$self?>?userid=<? echo $userid ?>"><input type=hidden name=search value=go />Enter keyword: <input type=text name=keyword /><br /><input type=submit value=Search /></form><br /><form method="post" action="<?=$self?>?userid=<? echo $userid ?>"><input type=hidden name=p2 value=go /><td><b><font size=2 face=Tahoma color=blue>Select</b></td><td><b><font size=2 face=Tahoma color=blue>Company</b></td><tr><?php$sqla = "SELECT * FROM `Custgroup` ORDER BY `Group` asc";$resulta = mysql_query($sqla) or die (mysql_error ());echo $sqla;if ($myrowa = mysql_fetch_array($resulta)){do { printf("<tr><td><input type=\"radio\" name=\"choice\" value=%d><td> <font size=2 face=Tahoma color=blue>%s<td></tr>", $myrowa["CGID"], $myrowa["Group"]); } while ($myrowa = mysql_fetch_array($resulta));}?></table><p><input type="submit" value="Select"></form><?php } }}?></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 20, 2006 Author Share Posted December 20, 2006 Thanks a lot !! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.