0

I think I have a relatively simple problem, but forgive me, I'm new at this.

I'm trying to take output from a dynamically created checkbox in php to a foreach statement to make a new string that I can use to request an update from mysql. In the foreach loop, I'm having trouble making a string that mysql will parse because the format of the value output in the form. It adds a period to the end and the beginning of all the output numbers. Below you can see the checkbox value is changed based on the row id. Here's the php form:

<?php

$id = $_POST['id'];

$query = "SELECT * FROM food"; 

$result = mysql_query($query);

echo "<form method='post' action='itemsnewrestadded.php'>";
echo "<input type='hidden' name='id' value='$id'>";
$i=1;

while($row = mysql_fetch_array($result)){
    echo "<input type='checkbox' name='name[$i]' **value='$row[FID]'** />$row[FNAME]";
    $i++;
    echo "<br />";
}

echo "<input type='submit' value='Add Food'>";
echo "</form>";

?>

The output is making the array, but with the ID's listed: .101. .102. .103.

I just want to get rid of the periods! Is it the $row assignment that makes the periods?

Here's what I have so far in the foreach coding:

   <?php

$foodadd = $_POST['name'];
$rid = $_POST['id'];

    $newfoodadd = "";
    // For every checkbox value sent to the form.
foreach ($foodadd as $key => $value) {
    // Append the string with the current array element, and then add a comma and a space at the end.
    $newfoodadd = " F_ID".$key. " = ". $value .", ";
    echo "$newfoodadd";
    }  

?>
2
Contributors
1
Reply
9
Views
5 Years
Discussion Span
Last Post by diafol
0

TRY THIS:

while($row = mysql_fetch_array($result)){
    echo "<input type=\"checkbox\" name=\"name[]\" value=\"{$row['FID']}\" />{$row['FNAME']}<br />";
}

And this:

$foodadd = $_POST['name'];
$rid = $_POST['id'];
$newfoodadd = "";
foreach ($foodadd as $key => $value) {
    $newfoodadd[] = "F_ID$key = $value";
}  
$nfstring = implode(", ", $newfoundadd);
echo $nfstring;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.