LB1234
asked on
Add value to end of associative PHP array
I have some questions about this code, please. Will the value of $_POST["id"] be overwritten with each loop? If so, that's not what I want. I'd like to end up with an array that has as many elements as there are loop iterations which correspond to each respective value of $id. My thinking was : $_POST["id"][]= $id; but this returned an error. Thanks.
<?php
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["transaction_id"];
$_POST["id"]= $id;
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To simplify, i'm forgetting about the loop right now and just trying to add to the end of an associative array. Still getting the scalar warning, and the value, 3, isn't being added to the array as expected. Should i be using array_push() or something?
<?php
$values = array();
$values["test"] = 2;
$values["test"][] = 3;
print_r($values);
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is a related question, which is why i'm bothering with this at all. When I hit the submit button on a form, what information exactly is the processing page getting? It's of course getting everything specified by the name="" elements, but is it getting anything else?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gr8 said: (But again, use a new array, not _$POST).
I was trying to avoid having to create a session variable to get the value of $id to the processing page (by using the $_POST). So just use a new array and a session superglobal and that should do the trick, yes?
I was trying to avoid having to create a session variable to get the value of $id to the processing page (by using the $_POST). So just use a new array and a session superglobal and that should do the trick, yes?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok getting back to the original code, I need to do this because I need to get the value of $id back into an array so I can use the corresponding values to insert the data back into the database in the appropriate field.
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["transaction_id"];
?>
<tr>
<!-- ID --> <td class="td-center"><?php echo $id; $_POST["id"][] = $id; ?></td>
<!-- Date--> <td class="td-center"><?php echo date("n-d-y", strtotime($row["date"]));?></td>
<!-- Amount--> <td class="td-center"><?php echo "$" . number_format($row["amount"], 2)?></td>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I hate programming!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok Guys, here's the full script. It's a table that pulls info from a database, and will allow the user to make edits and then write it all back to the database. I need to for each row of the table to have the appropriate ID number so it's loaded into the database correctly by ID. Using session instead of POST did solve my problem, though I know it's not ideal.
<?php session_start(); ?>
<?php include ("db_connection.php");?>
<?php include ("includes/layout/header.php");?>
<div id="table">
<form action="process_table.php" method="post">
<table class="gridtable" width="95%" border="0" cellspacing="0" cellpadding="2">
<tr>
<th width="3%" scope="col">id</th>
<th width="3%" scope="col">Date</th>
<th width="2%" scope="col">Amount</a></th>
<th width="5%" scope="col">Vendor</th>
<th width="8%" scope="col">Description</th>
<th width="1%" scope="col">Bind Cat.</th>
<th width="5%" scope="col">Category</th>
<th width="7%" scope="col">Notes</th>
<th width="1%" scope="col">Bind Notes</th>
<th width="5%" scope="col">Last Updated</th>
<th width="5%" scope="col">Updated By</th>
<th width="1%" scope="col">Done</th>
</tr>
<?php
$query = "SELECT * FROM transaction LIMIT 10";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["transaction_id"];
?>
<tr>
<!-- ID --> <td class="td-center"><?php echo $id; $_SESSION["id"][] = $id; ?></td>
<!-- Date--> <td class="td-center"><?php echo date("n-d-y", strtotime($row["date"]));?></td>
<!-- Amount--> <td class="td-center"><?php echo "$" . number_format($row["amount"], 2)?></td>
<!-- Vendor--> <td><?php echo $row["vendor"]?></td>
<!-- Description--> <td><?php echo $row["description"]?></td>
<!-- Bind Category--> <td class="td-center"><input name="bind_category[]" type="checkbox" checked="checked"></td>
<!-- Category Drop Down-->
<td class="td-center">
<?php
$category_options = "";
$category_query = "SELECT * FROM category";
$category_result_set = mysqli_query($connection, $category_query);
?>
<?php
while ($category_row = mysqli_fetch_assoc($category_result_set)) {
$selected = ($category_row["category"] == $row["category"] && $row["done"]==1) ? "selected":"";
$category_options .= sprintf("<option value = '%s' '%s' >%s</option>", $category_row['category'], $selected, $category_row['category']);
}
echo "<select name=\"category[]\">" . $category_options . "</select>";
?>
</td>
<!-- Notes--> <td><textarea name="notes[]" id="notes">test</textarea></td>
<!-- Bind Notes--> <td class="td-center"><input name="bind_notes[]" type="checkbox" checked="checked"></td>
<!-- Last Updated--> <td class="td-center"><?php echo date("n-d-y")?></td>
<!-- Updated By--> <td class="td-center"><?php echo "temp" ?></td>
<!-- Done--> <td class="td-center"><input name="done[]" type="checkbox" checked="checked"></td>
</tr>
<?php
}
?>
</table><br>
<input name="submit" type="submit" align="right">
</form>
</div>
<?php include ("includes/layout/footer.php");?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It may get easier if you start from the beginning.
http://www.php.net/tut.php
http://www.php.net/tut.php
ASKER
Guys this has been sooooooo helpful! I'm very appreciative for the detailed answers and information and review. Really I am. Gr8, many of the things in the code are sort of weird, but are there for testing (for example, the value of the checkboxes all being checked), so that every time i see if the data is being written to the database properly, I don't have to manually fill in stuff on the form.
And the hidden input type was something I'd never even heard of before you mentioned it. Wow. Really helpful. That has never ever been covered in any of the resources I've used for html or PHP. It's amazing how many resources you need to read for the same topic because it's just wasn't on the author's radar (and I understand of course that no book or tutorial series can be comprehensive).
And Gr8, the reason why I have the category fetch in every loop is because I need the values in the $row array to process this correctly. The category_now loop uses values from the $row result to check values. So it seems logical to me that the category loop could only come after the $row resource has been created.
And Ray, I've checked out many of the resources you've sent in the past, as well as starting from scratch on the php.net site. Granted, it's very easy to forget some of this stuff. But I like to try to figure things before looking up the answer. I'm sure that may not be the best method in all situations.
And the hidden input type was something I'd never even heard of before you mentioned it. Wow. Really helpful. That has never ever been covered in any of the resources I've used for html or PHP. It's amazing how many resources you need to read for the same topic because it's just wasn't on the author's radar (and I understand of course that no book or tutorial series can be comprehensive).
And Gr8, the reason why I have the category fetch in every loop is because I need the values in the $row array to process this correctly. The category_now loop uses values from the $row result to check values. So it seems logical to me that the category loop could only come after the $row resource has been created.
And Ray, I've checked out many of the resources you've sent in the past, as well as starting from scratch on the php.net site. Granted, it's very easy to forget some of this stuff. But I like to try to figure things before looking up the answer. I'm sure that may not be the best method in all situations.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The warning was this: Warning: Cannot use a scalar value as an array in C:\wamp\www\expenses\test.
When I did a print_r, the number three was not part of the array, which is odd behavior if there was only a warning!
Open in new window