I have a query that echos the results of a row...
<?php
$sql = "SELECT dba_name, contact_owner, phone, confirmation_code, physical_address, physical_city, physical_state, physical_zip, urep FROM mpas";
$result = $conn->query($sql);
?>
Later, I echo some results for the user to see...
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
?>
Further in the code, I want to use the same result as a value of a hidden field. I've tried a few things with no success. The value is blank/empty. Here's my most recent attempt.
<?php echo "<input hidden name='confc' value='".$row['confirmation_code']."'>" ; ?>
I'm sure it's something simple that I am not doing, but hoping someone can help me out. I've tried looking around the web for an answer, but am having a hard time coming up with an answer that applies to this specific issue.
I'm pretty certain you've done another query in the meantime (and even if you haven't, you're not looping anything). Try something like this:
<?php
$codes = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$codes[] = $row['confirmation_code'];
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
foreach ($codes as $c) {
echo "<input type='hidden' name='confc' value='".$c."'>" ;
}
?>
Note that if you get multiple rows back, you're only going to get one confc
value (the last one). If that's what you want, then great I guess (although you could then just use a string rather than an array). If not, you'll have to put an identifying number on the name of the hidden:
foreach ($codes as $k=>$c) {
echo "<input type='hidden' name='confc".$k."' value='".$c."'>" ;
}
I think you are not declaring hidden properly:
<input type="hidden" ...
There's no need to echo more than you need to so.
<input type="hidden" name="confc" value="<?php echo $row['confirmation_code']; ?>" />
Why not set the value to a php variable if you're going to use it multiple times? ( This might not fix the issue but it could iron out some possible problems )
if ($result->num_rows > 0) {
$row = $result->fetch_assoc()) {
$confc = $row[`confirmation_code`];
Then place it as the value:
<input type="hidden" name="confc" value="<?php echo $confc; ?>">
Or if you're going to continue with echoing it, something like this:
<?php echo "<input type= 'hidden' name='confc' value='".$confc."'>";?>
Why don`t you store that in a variable?
while($row = $result->fetch_assoc()) {
$confirmation_code = $row["confirmation_code"];
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
<?php echo "<input hidden name='confc' value='"$confirmation_code"'>" ; ?>
You can declare another array that stores all the rows referenced by the primary key of the SELECT database table. If none of your select columns is a primary key then you have to add it in your select statement e.g. id column
$sql = "SELECT id, dba_name, contact_owner, phone, confirmation_code, physical_address, physical_city, physical_state, physical_zip, urep FROM mpas";
if ($result->num_rows > 0) {
$all_rows = array();
while($row = $result->fetch_assoc()) {
$all_rows[$row["primary_key_of_your_table"]] = $row;
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
}
}
So, later you can use $all_rows to print some HTML like this:
if (isset($all_rows[1])) { //Access the row with primary key 1 (as an example)
echo "<input type=\"hidden\" name='confc' value='".$all_rows[1]['confirmation_code']."'>" ;
}