Jump to content

Recommended Posts

Hi

 

I am just learning PHP and have an assignment that involves arrays and sorting

 

What I would like to know is can you pull two arrays from one form

 

here is my code

 

<?php echo '<?xml version="1.1" encoding="utf-8"?>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="author" content="Colleen Spitzig" />
<meta http-equiv="description" content="ACME Widget Company" />  
<meta http-equiv="keywords" content="ACME Widget Company" />

<title>ACME Widget Company</title>
<link rel="stylesheet" type="text/css" href="css/style.css" title="style" />
</head>

<body id="body">

<div id="page">

<div id="header">
<h1>.</h1>
</div>
<!--End header-->
<div id="border">
<div id="menu">
<ul>
<li><a href="placeholder1.htm">Home</a></li>
<li><a href="placeholder2.htm">Widgets</a></li>
<li><a href="placeholder3.htm">Delivery</a></li>
<li><a href="placeholder4.htm">Contacts</a></li>
<li><a href="index.html" id="current">Online Orders</a></li>
</ul>
</div><!--End menu-->


<div id="content">
<h1>ACME Online Feedback Form</h1>


<?php
//check $_POST is true and create formal array
if ($_POST) {
$formArray = $_POST['formdata'] && $itemArray = $_POST['itemdata'];

//Extract individual variables for validation
extract ($formArray) && extract ($itemArray);

//declare variables and set value to false and null, respectively
$displayform = false;
$errmsg = '';

//nest if statements for validation
//test if firstname is empty - if true reset value for errmsg with concatenation
if (empty($fname)) {
$errmsg .= '<br />your first name<br />';
}

//nest if statements for validation
//test if lastname is empty - if true reset value for errmsg with concatenation
if (empty($lname)) {
$errmsg .= '<br />your last name<br />';
}

//nest if statements for validation
//test if phone is empty - if true reset value for errmsg with concatenation
if (empty($phone)) {
$errmsg .= '<br />your phone number<br />';
}

//nest if statements for validation
//test if email if empty - if true reset value for errmsg with concatenation
if (empty($email)) {
$errmsg .= '<br />your email address<br />';
}

//nest if statements for validation
//test if address is empty - if true reset value for errmsg with concatenation
if (empty($address)) {
$errmsg .= '<br />your address<br />';
}

//nest if statements for validation
//test if city is empty - if true reset value for errmsg with concatenation
if (empty($city)) {
$errmsg .= '<br />your city<br />';
}

//nest if statements for validation
//test if province is empty - if true reset value for errmsg with concatenation
if (empty($province)) {
$errmsg .= '<br />your province<br />';
}

//nest if statements for validation
//test if postal code is empty - if true reset value for errmsg with concatenation
if (empty($postal)) {
$errmsg .= '<br />your postal code<br />';
}

//test if subject if empty - if true reset value for errmsg with concatenation
if (empty($subject)) {
$errmsg .= '<br />a subject<br />';
}

//test if comments if empty - if true reset value for errmsg with concatenation
if (empty($comments)) {
$errmsg .= '<br />your comments<br />';
}

//if errmsg is true print message(s)
if (!empty($errmsg)) {
echo "Please <a href=\"javascript:history.go(-1)\">Go Back</a> and enter: <br /><br />$errmsg<br />";
}

else {
//if errmsg is false
//Compose text message for email
$message = "Message from $fname $lname\n$address\n$city, $province\n$postal\n\n$email\n$phone\n\nSubject: $subject\n\nComments:\n\n$comments";
//Send an email message.
mail("rc_spitzig@telus.net", "ACME Online Feedback Form", $message);
// Thank the user and advise them of the next action.
echo "Thank you for your input.  <br /><br />Your message has been sent and an ACME representative will respond to your request.\n";


//sorting area
//Print contents of original array
print "<p><b>The Original Array looks like this:</b><br /><br />";
foreach ($formArray as $index => $item) {
print "$index: $item<br />\n";
}
//Sort then Print contents of array sorted by sort
$formArrays = $_POST['formdata'];  
sort ($formArrays);
print "<p><b>With sort the array is sorted by key<br />
         the array looks like this:</b><br /><br />";
foreach ($formArrays as $indexs => $items) {
print "$indexs: $items<br />\n";
}
//Sort then Print contents of array sorted by rsort
$formArrayr = $_POST['formdata'];   
rsort ($formArrayr);
print "<p><b>With rsort the array is sorted by key in reverse order<br />
      looks like this:</b><br /><br />";
foreach ($formArrayr as $indexr => $itemr) {
print "$indexr: $itemr<br />\n";
}
//Sort then Print contents of array sorted by asort
$formArraya = $_POST['formdata'];   
asort ($formArraya);
print "<p><b>With asort the index association is maintained when the array is sorted
       <br />the array looks like this:</b><br /><br />";
foreach ($formArraya as $indexa => $itema) {
print "$indexa: $itema<br />\n";
}
//Sort then Print contents of array sorted by arsort
$formArrayar = $_POST['formdata'];   
arsort ($formArrayar);
print "<p><b>With arsort the index association is maintained and the data is sorted in reverse order
       <br />the array looks like this:</b><br /><br />";
foreach ($formArrayar as $indexar => $itemar) {
print "$indexar: $itemar<br />\n";
}
//Sort then Print contents of array sorted by ksort
$formArrayk = $_POST['formdata'];   
ksort ($formArrayk);
print "<p><b>With ksort the array is sorted by key
       <br />the array looks like this:</b><br /><br />";
foreach ($formArrayk as $indexk => $itemk) {
print "$indexk: $itemk<br />\n";
}
//Sort then Print contents of array sorted by krsort
$formArraykr = $_POST['formdata'];   
krsort ($formArraykr);
print "<p><b>With ksort the array is sorted by key in reverse order
       <br />the array looks like this:</b><br /><br />";
foreach ($formArraykr as $indexkr => $itemkr) {
print "$indexkr: $itemkr<br />\n";
}

}
}

else {
//set displayform to true.
$displayform = true;
}
//if displayform tests true display the form.
if ($displayform) {
?>
  <h1>How can we help you?</h1>

  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <!--php_self is used to bring back file info to cause page reload -->

<!--create the array data from form-->

   <p>First Name:          <input type="text" name="formdata[fname]" /></p>
   <p>Last Name:          <input type="text" name="formdata[lname]" /></p>
   <p>Phone Number:    <input type="text" name="formdata[phone]" /></p>
   <p>Email:                   <input type="text" name="formdata[email]" /></p>
   <p>Address:              <input type="text" name="formdata[address]" /></p>
   <p>City:                     <input type="text" name="formdata[city]" /></p>
   <p>Province:             <input type="text" name="formdata[province]" /></p>
   <p>Postal Code:        <input type="text" name="formdata[postal]" /></p>
   <p>Subject:               <input type="text" name="formdata[subject]" size="40" /></p>
   <p>Comments:</p>
   <p><textarea name="formdata[comments]" cols="40" rows="8"></textarea></p>
   <p><input type="checkbox" name="itemdata[order]" value="MGC000001" />  Metal Garbage Can</p>
   <p><input type="checkbox" name="itemdata[order]" value="RIH000002" />  Rod Iron Handle</p>
   <p><input type="checkbox" name="itemdata[order]" value="OMS000003" />  Ornamental Metal Shelf</p>
   <p><input type="checkbox" name="itemdata[order]" value="CPS000004" />  Copper Pot Set</p>
   
   <p><input type="submit" value="Submit" />  <input type="reset" value="Reset" /></p>

   
  </form>
<?php 
//close the if statement that encases the form.
}
?>
</div>
<!--End content-->
</div>
<!--End border-->
<div id="footer">
<h2>ACME Widget Company, Unit 140 &#45; 3080 Townline Road, Abbotsford, BC &#45; 604-855-1571 &#45; email nbsp; </h2>
</div>
<!--End footer-->

</div>
<!--End Page-->

</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/73908-newbie-needs-help/
Share on other sites

Sorry to be so blunt, but we have [ code ][ /code ] tags (without the spaces) that preserve formatting and syntax highlight code. This may make your code more readable and hence encourage more help.

 

But to answer your question...

 

What I would like to know is can you pull two arrays from one form

 

Yes. What part exactly are you stuck with?

Link to comment
https://forums.phpfreaks.com/topic/73908-newbie-needs-help/#findComment-372952
Share on other sites

Sorry to be so blunt, but we have [ code ][ /code ] tags (without the spaces) that preserve formatting and syntax highlight code. This may make your code more readable and hence encourage more help.

 

But to answer your question...

 

What I would like to know is can you pull two arrays from one form

 

Yes. What part exactly are you stuck with?

 

 

I am not sure how to code the arrays properly, I can make it work if I have only one array but I really think I messed up with the 2nd one.

Link to comment
https://forums.phpfreaks.com/topic/73908-newbie-needs-help/#findComment-372966
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.