Jump to content

[SOLVED] A more efficent way to process multiple submits.


swatisonee

Recommended Posts

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 A


1.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]
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

can always put everything on one page add a couple hidden values and a couple if thens.

[code]<?php
include ("../include/session.php");
require ("../include/bottom.php");
include ("../include/ticker.php");
$self = $_SERVER['PHP_SELF'];

// Check if user is logged in
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;

// See what page
if(isset($_POST['p3'])){
// page 3 content

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 = .....";

} 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

Link to comment
Share on other sites

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 choose
Name <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 !

Link to comment
Share on other sites

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>

<?php
include ("../include/session.php");
require ("../include/bottom.php");
include ("../include/ticker.php");
$self = $_SERVER['PHP_SELF'];

// Check if user is logged in
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;   

// See what page we load
if(isset($_POST['p3'])){
// page 3 content

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 = .....";

} 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]
Link to comment
Share on other sites

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.