0

I'm new to PHP and have seen a few similar queries, but mine involves 2 arrays and I'm not sure how to put them next to each other. Can someone help? very much appreciated! Im moving out and making a site to sell my stuff, I've got a select item and offer amount section on the form. I want them to appear next to each other on the email.

You can see the site i'm making here: http://www.stasale.com

I have the following code:

<table border="1">
<tr>
<th>Image</th>
<th>Name</th>
<th>Condition</th>
<th>Description</th>
<th>Asking Price</th>
<th>Select Item</th>
<th>Offer Value</th>
</tr>

<?php   
 require_once('appvars.php');
 require_once('connectvars.php');
 // Connect to the database 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
$query = "SELECT * FROM stasale ORDER BY date DESC";
$data = mysqli_query($dbc, $query);

while ($row = mysqli_fetch_array($data)) { 

  if (is_file(GW_UPLOADPATH . $row['image']) && filesize(GW_UPLOADPATH . $row['image']) > 0) {
  echo '<tr><td><img src="' . GW_UPLOADPATH . $row['image'] . '" alt="' . $row['image'] . '" width="100" height="100"/></td>
  <td>' . $row['name'] . '</td>
  <td>' . $row['condition'] . '</td> 
  <td>' . $row['description'] . '</td> 
  <td>' . $row['askingprice'] . '</td> 
  <td><form method="post" action="mail.php"><input type="checkbox" name="itemname[]" value="' . $row['id'] . '">Select</td> 
  <td>Offer: <input type="text" name="offer[]"></td></tr>';
}
else {
  echo 'Invalid row';
}
} ?>

<tr><td colspan="3">Contact Name:</td><td colspan="4"><input type="text" name="coname" width="300" ></td></tr>
<tr><td colspan="3">Contact Number:</td><td colspan="4"><input type="text" name="conumber" width="300" ></td></tr>
<tr><td colspan="3">Contact Email:</td><td colspan="4"><input type="text" name="coemail" width="300" ></td></tr>
<tr><td colspan="3">Questions/Comments:</td><td colspan="4"><input type="text" name="comments" width="300" height="100"></td></tr>

<tr><td colspan="7" align="center"><input type="submit" value="Send Request" name="submit" style="width:500px"/></form></td></tr>
</table>

I can't seem to work out how to get the values from itemname[] or offer[] to show up in my mail.php page which is completely wrong I feel:

if (isset($_POST['submit'])) {
$name = $_POST['coname'];
$email = $_POST['coemail'];
$number = $_POST['conumber'];
$comments = $_POST['comments'];
foreach ($_POST['itemname'] as $itemname)
foreach ($_POST['offer'] as $offer)

{

}
$to = '[email protected]';
$subject = 'STASALE order';
$msg = "$name, $email, $number, $comments \n" .
  "I would like: $itemname" .
  "$offer";

mail($to, $subject, $msg, 'From:' . $email);

$to = "$email";
$subject = 'I have received your request - Si';
$msg = 'Thank you for contacting me! I have received your request and will reply shortly! ~ Si x';
$from = '[email protected]';

mail($to, $subject, $msg, 'From:' . $from); 

echo '<p><strong>Thanks for your request. I will respond as soon as possible.</strong></p>';


}

?>

Thank you for your help in advance!

1
  • Very refreshing to see mysqli_ functions
    – Kermit
    Feb 4, 2013 at 19:36

1 Answer 1

0

You can do something like this:

foreach ($_POST['offer'] as $key => $value) {
        if ($value) {
            $offer_arr[] = $value;
        }
    }

    $str = '';
    for ($i = 0; $i <= count($_POST['itemname']); $i++) {
        if ($_POST['itemname'][$i]) {
            $str .= 'Item: ' . $_POST['itemname'][$i] . ' Offer: ' . $offer_arr[$i] . ', ';
        }
    }

$str will contain all the items and offers next to each other.

7
  • Hi Sean, the code works really well except when the checkbox is not selected for an item, I get an "Item: Offer:" in the email as well instead of the email just ignoring the ones that are not selected. Is there any way to avoid that? Thanks Feb 4, 2013 at 20:12
  • If first and third checkboxes are checked, $_POST['itemname'][0] and [2] would countain values, wouldn't it be better to add another counter so that there are no gaps?
    – bpoiss
    Feb 4, 2013 at 21:09
  • I have a problem with the new edited code. If I select the 1st and 3rd options, the 1st one shows up fine but the third one doesnt show anything: "Item: prok Offer: 5252," Feb 4, 2013 at 21:13
  • Yes Bernhard, that would be useful but I am not quite sure what to do in this instance. Im going to have about 100 items on here and its quite important to have only the selected one and their corresponding offers to avoid confusion. You guys have been brilliant with me so far, much appreciated Feb 4, 2013 at 21:20
  • So far only seans original: $str = ''; for($i = 0; $i <= count($_POST[itemname]); $i++) { $str .= "\n" . $_POST[itemname][$i] . ' for ' . '£' . $_POST[offer][$i] . "\n"; } Would bring up the correct selected ones, but includes a weird extra 'for £': prok for £2233 test for £ for £42424 Feb 4, 2013 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.