Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store multiple values arrays OOP ????????

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
Hi Comrades

I have a problem here’s what I want to do !

I have a three step registration form

Step1
Choose Interest $_POST['Interest']
Choose Subject $_POST['Subject']
Choose Level $_POST['Level]

Step2
Choose Country $_POST['County’]
Choose Town $_POST['Town']

Step 3
Enter contact name $_POST['Name']
Enter contact number $_POST['contact_number]

In step 3 you can either (A) proceed to checkout, or (B) register another interest

The problem is how do I store the values if a user wants to register for example 3 interests.

Do I use arrays if so How do I do it, do I use OOP if so does anyone know a good tutorial.

The second problem is this if the user has now registered 3 Interests I want to be able to confirm their chooses before they proceed to checkout, hence I want to be able to show them all there chooses but I also want them to be able to delete an Interest if they want.

So for example if they want to delete the second interest, how is this done if it’s an array
Or part of a class

I’m very confused as to how this is done, is there any good book I can get.

Regards
Graham
 
This doesn't sound to me like a programming-paradigm problem but rather an datastructure problem and algorithm problem. How you implement the solution (OOP or non-OOP) is completely up to you.

In situations where I have multiple pages as part of an input process, I write each script in the process so that it both outputs the form it needs to gather information and also processes the information gathered. This is done by the form's "action" attribute pointing back to the script that output the form.

The scripts each take the form of:

Code:
<?php

//check if there's any input
if (isset ($_POST['someformelement']))
{
   //we have input
   process_input();
   
   //send browser on to the next page in the stream
   header ('Location: next_step.php');
}
else
{
   //we had no input, which means this is the first run
   print '<html><body>
   <form method="POST" action="' , $_SERVER['PHP_SELF'] . '">
      <input type="text" name="someformelement"><br>
      <input type="submit">
   </form>
   </body></html>';
}
?>

Typically, each step of the input process requires that information be carried forward through multiple steps. For that, I use session variables. (see
It's just a matter of the scripts getting the flow right.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Tks for the answer TANSTAAFL i understand your point,
but what Iwas trying to explain is how do I store multiple
values in the same session variable

ie can i fill the $_SESSION['interest'] variable with multilpe values before i submit to my database ie until
payment has been handled.

TKs
 
$_SESSION is an array. Any element of the array can itself be an array. Something like:

Code:
<?php
session_start();

$_SESSION['foobar'] = array();

$_SESSION['foobar'][1] = 'foo';
$_SESSION['foobar'][2] = 'bar';
?>

You can also store objects in a session variable. The gotcha is to make sure that the class is defined before your script invokes session_start(). Otherwise, you'll get a "class not defined" error.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
TANSTAAFL

I know i am asking alot but if you could look at the following code and maybe show me how I can make the following arrays.

Interest
Subject
Level

They are coming from a mysql database with some javascript involved to link the tables

Many Thanks


------------------------- Code --------------------------

<?php
// Include the header file
include_once ('includes/header.htm');









// Include the configuration file
require_once ('includes/config.inc');

// Set the page title and include the header
$page_title = 'Step One';

// Connect to the database
require_once ('../mysql_connect3.php');

// MySQL statement to allow the dropdowns to work
mysql_select_db($database_findagrind, $findagrind);
$query_step_one = "SELECT interests.id_interest,interest_name,subjects.id_subject,subject_name,levels.id_level,level_name
FROM interests,levels,subjects
WHERE interests.id_interest = levels.id_interest
AND interests.id_interest = subjects.id_interest
ORDER BY interest_name,subject_name,id_level ";


$step_one = mysql_query($query_step_one, $findagrind) or die(mysql_error());
$row_step_one = mysql_fetch_assoc($step_one);
$totalRows_step_one = mysql_num_rows($step_one);

// To allow the values to be remembered
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

if (!isset($_SESSION['Level']))
{
$_SESSION['Level']="";
}

// Start of content
echo'<div id = "content">';

echo'<br>';
echo'<br>';
echo'<div id = "middlebox">';
echo'<h2>Step One: Choose a Grind</h2>';




echo'<h3>>> Step One:</h3><p>Please choose what grind you would like to give, if you would like
to register more than one grind you will be given this option in Step Three.</p>';


// Validation check to ensure all of the field have been used

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

// Check if the form has been submitted
{

if (($_POST['Interest']) == 1)
// Validate the secondary exam
{
$_SESSION['Interest']="";
$_SESSION['Subject']="";
$_SESSION['Level']="";
$a1 = FALSE;
echo '<p class="validation">You forgot to choose an interest</p>';
}
else
{
$a1 = $_POST['Interest'];
}

if (($_POST['Subject']) == 1)
// Validate the secondary exam
{
$b1 = FALSE;
echo '<p class="validation">You forgot to choose a subject</p>';
}
else
{
$b1 = $_POST['Subject'];
}

if (($_POST['Level']) == 1)
// Validate the secondary exam
{
$c1 = FALSE;
echo '<p class="validation">You forgot to choose a level</p>';
}
else
{
$c1 = $_POST['Level'];
}



if ($a1 && $b1 && c1)
{

$_SESSION['Interest'] = $a1;
$_SESSION['Subject'] = $b1;
$_SESSION['Level'] = $c1;



ob_end_clean(); // Delete the buffer
header("Location: . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/step2.php");
exit();
}

// End Of Check if the form has been submitted
}

?>





<!-- Start of main application form -->

<form name = "signup" id="signup" action= "<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table align="center">
<tr>
<td colspan="2" class="labelcell"><label for = "username">Choose Interest</label></td>
<td colspan="2" class="fieldcell"> <select name="Interest" id="Interest" onChange="TCN_reload(this)">
<option selected>Interest</option>
</select>
</select>
</td>
</tr>
<tr>
<td colspan="2" class="labelcell"><label for = "username">Choose Subject</label></td>
<td colspan="2" class="fieldcell"><select name="Subject" id="Subject" onChange="TCN_reload(this)">
<option selected>Subject</option>
</select>
</td>
</tr>

<tr>
<td colspan="2" class="labelcell"><label for = "username">Choose Level</label></td>
<td colspan="2" class="fieldcell"><select name="Level" id="Level" onChange="TCN_reload(this)">
<option selected>Level</option>
</select>
</td>
</tr>




<script language="JavaScript">
TCN_contents=new Array();
TCN_tempArray=new Array();
TCN_counter=0;
function TCN_addContent(str){
TCN_contents[TCN_counter]=str;
TCN_counter++;
}
function TCN_split(){
TCN_arrayValues = new Array();
for(i=0;i<TCN_contents.length;i++){
TCN_arrayValues=TCN_contents.split(separator);
TCN_tempArray[0]=TCN_arrayValues;
}
}
function TCN_makeSelValueGroup(){
TCN_selValueGroup=new Array();
var args=TCN_makeSelValueGroup.arguments;
for(i=0;i<args.length;i++){
TCN_selValueGroup=args;
TCN_tempArray=new Array();
}
}
function TCN_makeComboGroup(){
TCN_comboGroup=new Array();
var args=TCN_makeComboGroup.arguments;
for(i=0;i<args.length;i++) TCN_comboGroup=findObj(args);
}
function TCN_setDefault(){
for (i=TCN_selValueGroup.length-1;i>=0;i--){
if(TCN_selValueGroup!=""){
for(j=0;j<TCN_contents.length;j++){
if(TCN_arrayValues[j][(i*2)+1]==TCN_selValueGroup){
for(k=i;k>=0;k--){
if(TCN_selValueGroup[k]=="") TCN_selValueGroup[k]=TCN_arrayValues[j][(k*2)+1];
}
}
}
}
}
}
function TCN_loadMenu(daIndex){
var selectionMade=false;
daArray=TCN_tempArray[daIndex];
TCN_comboGroup[daIndex].options.length=0;
for(i=0;i<daArray.length;i++){
existe=false;
for(j=0;j<TCN_comboGroup[daIndex].options.length;j++){
if(daArray[(daIndex*2)+1]==TCN_comboGroup[daIndex].options[j].value) existe=true;
}
if(existe==false){
lastValue=TCN_comboGroup[daIndex].options.length;
TCN_comboGroup[daIndex].options[TCN_comboGroup[daIndex].options.length]=new Option(daArray[daIndex*2],daArray[(daIndex*2)+1]);
if(TCN_selValueGroup[daIndex]==TCN_comboGroup[daIndex].options[lastValue].value){
TCN_comboGroup[daIndex].options[lastValue].selected=true;
selectionMade=true;
}
}
}
if(selectionMade==false) TCN_comboGroup[daIndex].options[0].selected=true;
}
function TCN_reload(from){
if(!from){
TCN_split();
TCN_setDefault();
TCN_loadMenu(0);
TCN_reload(TCN_comboGroup[0]);
}else{
for(j=0; j<TCN_comboGroup.length; j++){
if(TCN_comboGroup[j]==from) index=j+1;
}
if(index<TCN_comboGroup.length){
TCN_tempArray[index].length=0;
for(i=0;i<TCN_comboGroup[index-1].options.length;i++){
if(TCN_comboGroup[index-1].options.selected==true){
for(j=0;j<TCN_tempArray[index-1].length;j++){
if(TCN_comboGroup[index-1].options.value==TCN_tempArray[index-1][j][(index*2)-1]) TCN_tempArray[index][TCN_tempArray[index].length]=TCN_tempArray[index-1][j];
}
}
}
TCN_loadMenu(index);
TCN_reload(TCN_comboGroup[index]);
}
}
}
function findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
TCN_makeSelValueGroup("","","<?php echo $_SESSION['Level']; ?>");
TCN_makeComboGroup("Interest","Subject","Level");
var separator="+#+";
<?php do{?>
TCN_addContent("<?php echo $row_step_one['interest_name']; ?>+#+<?php echo $row_step_one['id_interest']; ?>+#+<?php echo $row_step_one['subject_name']; ?>+#+<?php echo $row_step_one['id_subject']; ?>+#+<?php echo $row_step_one['level_name']; ?>+#+<?php echo $row_step_one['id_level']; ?>");
<?php } while ($row_step_one = mysql_fetch_assoc($step_one)); ?>
TCN_reload();

</script>



<tr>
<td colspan="2" class="labelcell"><label for = "confirm_password"></label></td>
<td colspan="2" align="center"><input type="submit" name="submit" value=">> Step Two"/></td>
</tr>
</table>
</form>


</div>
<!-- End of Middlebox Div -->

</div>
<!-- End of Content Div -->


<?php
// Include the footer
include_once('includes/footer.htm');
mysql_free_result($step_one);
?>
 
First, sessions don't work unless your script invokes session_start()

Second, you're trying to do all of the steps in one run of the script. That's not now web applications work. In an executable application, the program runs the whole time, spending most of its time waiting for user input. Web applications, however, are discontinuous:[ol][li]A user points his browser to a script on a server.[/li][li]The web server runs the script and sends the output to the browser.[/li][li]The script stops running.[/li][li]The browser renders the HTML and the user interacts with the page[/li][li]Optionally, the user directs the browser to submit data to another script on the web server[/li][li]The script runs, processes the intput and produces an HTML page[/li][li]The script stops running.[/li][/ol]

It's the "the script stops running" part to which I want to direct your attention.

Typically, a PHP script that will either produce a form or process the input from that form looks like:

Code:
<?php
if (isset($_POST['somefield']))
{
   //process input
}
else
{
   //produce the form to collect input
}
>?

Each script can use logic more complex than the above, producing many different pages depending on input. Conversely, you can have several special-purpose scripts, each producing its own form and processing its own input, with each script using "Location" headers to direct the browser through all the scripts.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top