Convert Json to PHP Array - php

I have working code,
$output=json_encode($data);
$results=json_decode($output, TRUE);
foreach ($results['results'] as $id) {
echo $id['id'] . '<br/>';
}
But what i am trying to do is list not only the ID's of the JSON output but also the "subject" Associated with each ID right next or below it. not sure how to do so.

Try this
foreach ($results['results'] as $item) {
echo 'id: '. $item['id'] . ' subject: '. $item['subject'] .'<br/>';
}

echo "<form action=\"former.php\" method=\"post\">";
echo '<input type="text" name="TicketID" value="'. $item['id'] .'"/>';
echo "<input type=\"submit\" name=\"View\" value=\"View\" />";
the Above is working for me BUT it wont let me view the Specific ID . it just defaults to the last ID in the array. #veNuker

Related

PHP POST yields empty result using sqlite3 database

I have a simple web form in HTML using POST and PHP against an SQLite3 database. The form asks for a database id. When entered and hitting submit, the result does not output to the screen.
Here is the code. Please help! It appears the variable is empty. Where am I going wrong?
Original Form HTML (edit_entry1.html):
<body bgcolor = "#C7CFCA">
</p></p>
<center><h2>Update a Record<br>
<form method="POST" action="update_record.php">
<br />
<center>
<h3>To update a record click on 'View Database' and find the record ID you want to update and enter that ID here.</h3>
</center>
<table>
<tr><td><h2>Record ID: </td><td><h2><input style="font-size:20px" type="text" name="archivo" size="80"></td></tr>
<tr><td><input type="submit" name="save" value="Submit" style="font-size:20px"></td><td><input type=reset value="Reset Form" style="font-size:20px"></td>
</table>
</form>
</center>
</html>
This is the corresponding php script (update_record.php):
<?php
{
//open the database
$db = new SQLite3('wc.db');
// Set Variables from POST
$record = $_POST["archivo"];
//now output the data to a simple html table...
echo "<!DOCTYPE html>\n";
echo "<html lang=\"en\">\n";
echo "<body bgcolor = \"#C7CFCA\" text = \"black\">\n";
echo "<center>";
echo "<p>Record ID is <?php echo $record ?>.</p>";
echo "<table>\n";
echo "<h2>Update a Record</h2>";
echo "<tr><th><u><h3>ID</th><th><u><h3>Last Name</th><th><u><h3>First Name</th>";
echo "<th><u><h3>Middle Name</th><th><u><h3>Section</th>";
echo "<th><u><h3>Lot</th><th><u><h3>Plot</th><th><u><h3>Burial Date</th><th><u><h3>Veteran</th></tr>\n";
$results = $db->query('SELECT id,last_name,first_name,middle_initial,section,lot,plot,burial_date,veteran FROM burials WHERE id = $record');
while ($row = $results->fetchArray()) {
echo "<tr><td><center><h3>" . $row['id'] . "</td><td><center><h3>" . $row['last_name'] . "</td><td><center><h3>" .
$row['first_name'] . "</td><td><center><h3>" . $row['middle_initial'] . "</td><td><center><h3>" .
$row['section'] . "</td><td><center><h3>" . $row['lot'] . "</td><td><center><h3>" . $row['plot'] . "</td><td><center><h3>" . $row['burial_date'] . "</td><td><center><h3>" . $row['veteran'] . "</td></tr>\n";
}
echo "</table>\n";
echo "<p>Record ID is <?php echo $record ?>.</p>";
echo "<label for=\"sql\"><h3>What do you want to update? </label>";
echo "<select id=\"option\">";
echo "<h3><option value=\"last_name\"><h3>Last Name</option>";
echo "<option value=\"fist_name\"><h3>First Name</option>";
echo "<option value=\"middle_initial\"><h3>Middle Name</option>";
echo "<option value=\"section\"><h3>Section</option>";
echo "<option value=\"lot\"><h3>Lot</option>";
echo "<option value=\"plot\"><h3>Plot</option>";
echo "<option value=\"burial_date\"><h3>Burial Date</option>";
echo "<option value=\"veteran\"><h3>Veteran Status</option>";
echo "</select>";
echo "<h2><input style=\"font-size:15px\" type=\"text\" name=\"opt\" size=\"30\">";
echo "</body>\n";
echo "</html>";
}
?>
When I put, say, 1 as the record id in the form, nothing is outputted. I'm new to this and would definitely appreciate some pointers/tips.
To prevent a SQL injection attack, you should consider using the prepare/bind/execute pattern. Use example 1 in the SQLITE3::prepare doc as a guide.
Regarding the problem at hand: From the PHP: Strings doc:
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
Since the SQL query is enclosed in single-quotes ('), the $record variable is not parsed. In other words, what you see is what is being sent to the database, thus no rows are returned.

How do I obtain the value of the option that was chosen in a select from?

In the PHP code below I have created a form that is used to create a drop down list.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $network_name['network_id'];
$network_name = $result1['network_code'];
echo '<option name="entry" value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>
This code works as desired. It populates a drop down list based on the results of a database query. After the form has been submitted, I want to obtain the option that was selected and use it in another query. After the submit, I print the contents of the $_POST variable using:
print_r($_POST);
and my results are as follow:
Array ( [network] => [submit] => Send )
I want to get the value that was selected for network but it appears to be blank. Can anyone tell me what I'm doing wrong.
By the way, I am really new at coding and this is my first time using SO so please excuse any usage errors on my part. Thanks.
$network_id = $result1['network_id'];
instead of
$network_id = $network_name['network_id']
Output like this:
Array ( [network] => 2 [submit] => Send )
You are setting a wrong value to network_id, that's why it's output is blank. Also, the 'name' attribute on option tags is not needed. Try the changes below.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $result1['network_id'];
$network_name = $result1['network_code'];
echo '<option value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>

Stuck on Array PHP - only uses last value in array?

I have the following,
when the below code process', it gives me a list of ID's etc and Description and Subject. Now since there is a form i put within the loop, when i go to click on "View" button which then display only that specific "Ticket" the issue is, it always displays the same ticket. the last one in the array. why?
foreach ($results['results'] as $item) {
echo 'Ticket ID # '. $item['id'] . ' Subject: '. $item['subject'] .'<br/>';
echo 'Description : '. $item['description'] .'<br/>';
echo "<form action=\"view.php\" method=\"post\">";
echo '<input type="text" name="TicketID" value="'. $item['id'] .'"/>';
echo "<input type=\"submit\" name=\"View\" value=\"View\" />";
echo "<br>";
echo "<br>";
}
You do not close the <form> tag - simply add between the last <input> element and the <br> elements an echo '</form>'; line.

Looping through $_POST Variable

I have a PHP script that generates some form on the page I have. I also have a table and submit button within the form which all works fine and the data is sent to the next page as intended. Now on the next page I have a PHP code that reads the $_POST variable. Here is my code:
echo "<form name=\"myForm\" id=\"myForm\" method=\"post\" action=\"\">";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = mysql_fetch_array($qryRes))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><input name=\"aName" . $row['id'] . "\" id=\"aName" . $row['id'] . "\" type=\"text\" value=\"" . $row['aName'] . "\" /></td>";
echo "<td><select name=\"gID" . $row['id'] . "\">";
while($gRow = mysql_fetch_array($grpsQry))
{
if($gRow['id'] == $row['groupID'])
echo "<option id=\"" . $gRow['id'] . "\" selected=\"selected\">" . $gRow['gName'] . "</option>";
else
echo "<option id=\"" . $gRow['id'] . "\">" . $gRow['gName'] . "</option>";
}
echo "</select></td>";
echo "</tr>";
mysql_data_seek( $grpsQry, 0 );
}
echo "</table>";
echo "<input type=\"submit\" name=\"submitForm\" value=\"Save\" />";
echo "</form>";
Then comes my PHP Code to read the $_POST:
if(isset($_POST['submitForm']))
{
print_r($_POST);
foreach($_POST as $x)
echo $x . "<br />";
}
Everything works fine, except that I need to read only the text field in the foreach loop as I will generate a query on them and will compare the values I will have with the value returned from the List ..
As yuo can see, the textfield is named aName then the id of the user. I need to read this only. But I will use it to generate a query which will be then compared to the value of the List gID.
try this:
foreach($_POST as $key => $value){
if(substr($key,0,5) == 'aName')
echo $value . "<br />";
}

How to display the selected data from this form?

So this is part of my code :
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($qsq))
{
echo"<input type='checkbox' name='choice[]' value='" . $row['question_id'] . "' /> ". `$row['question_text'] . '<br />';`
}
echo"<br>";
echo "<input type='submit' value='submit' /></form>";
What should i do in the next.php ? I'm thinking to put the selected info in an array and display it. Then store the selected results in a table.But i am not sure with the codes.I am beginner in php can someone help me with the coding ?Thanks in advance!
First of all I cleaned up your code a little bit. (Using single quoutes around HTML trributes is uncanny).
echo ('<form name="whatever" action="next.php" method="get">');
while ($row = mysql_fetch_assoc ($qsq)) {
echo ('<input type="checkbox" name="choice[' . $row["question_id"] . ']" value="1" /> ' . $row["question_text"] . '<br />';
}
echo '<br />';
echo '<input type="submit" value="submit" /></form>';
Then you can iterate thru the values with a foreach loop in next.php.
echo ('<ul>');
foreach ($_GET["choice"] as $key => $value){
echo ('<li>' . $key . ' is ticked</li>');
}
echo ('</ul>');
Note that the array's keys hold the real information here,values wil only contain the number 1.
Take a look ath the source of resulting HTML. This is not theonly possible solution but good for learning purposes.

Categories