Dealing with "random" POST data - php

I have a page that pulls 5 random rows from a table. It looks similar to the below.
<table>
<tr>
<td><input type = 'radio' name='bills[1]' value = 'y'><label for='1'>Yes</label> </td>
<td><input type = 'radio' name='bills[1]' value = 'n'><label for='1'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[8]' value = 'y'><label for='8'>Yes</label> </td>
<td><input type = 'radio' name='bills[8]' value = 'n'><label for='8'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[2]' value = 'y'><label for='2'>Yes</label> </td>
<td><input type = 'radio' name='bills[2]' value = 'n'><label for='2'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[6]' value = 'y'><label for='6'>Yes</label> </td>
<td><input type = 'radio' name='bills[6]' value = 'n'><label for='6'>No</label> </td>
</tr>
<tr>
<td><input type = 'radio' name='bills[3]' value = 'y'><label for='3'>Yes</label> </td>
<td><input type = 'radio' name='bills[3]' value = 'n'><label for='3'>No</label> </td>
</tr>
</table>
This returns an array that looks like the following,
Array
(
[bills] => Array
(
[6] => y
[2] => n
[5] => n
[1] => y
[8] => y
)
)
By using a foreach($_POST['bills'] as $bill) statement I can loop through that array, but how do I get the value of the id, and its respective answer? In the above case, 6, 2, 5, 1, 8.

Include the key on your foreach construct like this
foreach($_POST['bills'] as $bill=>$answer)
{
echo "The value of $bill is $answer\n"; $bill will be 6,2,5,1,8 and $answer will be y,n,n,y,y
}
OUTPUT :
The value of 6 is y
The value of 2 is n
....

You can use key():
<?php
$array = array(
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4
);
while($element = current($array)) {
echo key($array)."\n";
next($array);
}
?>
OR
foreach($_POST['bills'] as $bill=>$answer)
{
echo "$bill and $answer\n";
}

Related

Creating Movie Survey - PHP questions

Recently we had a project due that I did not do very well on. I know it wont help my current grade, but I think understanding the concepts will be extremely beneficial to me in the long run. The assignment was to create a movie survey.
This is what I have so far for my formprocessor.php
<body>
<H1><u>The Vermont Web Designers Movie Survey</u></h1>
<?php
//I want to hold the users information that they entered, so they know that this is their survey
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
print "<p>$firstName $lastName's Movie Review - Spring 2018</p><hr>";
//I have values from 1 to 5 (ratings), I want to create a variable for each of them
$great = 0;
$good = 0;
$ok = 0;
$soso= 0;
$terrible = 0;
//I create an array to hold my selections. One for the movie ratings, another for the actor ratings.
$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);
//I use a loop to collect my movie choices.
for($i = 0; $i < 3; $i++){
if($movieChoice[$i] == 1){
//I increase the variable by 1 if it was selected.
$great++;
}
else if($movieChoice[$i] == 2){
$good++;
}
else if($movieChoice[$i] == 3){
$ok++;
}
else if($movieChoice[$i] == 4){
$soso++;
}
else{
$terrible++;
}
}
//I use another loop to hold my actor selections.
for($i = 0; $i < 3; $i++){
if($actorChoice[$i] == 1){
$great++;
}
else if($actorChoice[$i] == 2){
$good++;
}
else if($actorChoice[$i] == 3){
$ok++;
}
else if($actorChoice[$i] == 4){
$soso++;
}
else{
$terrible++;
}
}
print ('<table align= "center">
<tr><th colspan= "3">Movie</th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>
<tr><td colspan= "3">1: The Godfather Part 1</td>
<td>Quality of Movie</td>
<td>'.$movieChoice[$great].'</td>
<td>'.$movieChoice[$good].'</td>
<td>'.$movieChoice[$ok].' </td>
<td>'.$movieChoice[$soso].'</td>
<td>'.$movieChoice[$terrible].'</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>)
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">2: Men of Honor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">3: Three Days of the Condor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>');
?>
This is my form
<H1><u>The Vermont Web Designers Movie Survey</u></h1>
First Name: <input type= "text" size= "10" maxlength= "25" name= "firstName">
Last Name: <input type= "text" size= "10" maxlength= "25" name= "lastName"><br />
<hr>
<p>Please use a scale of 1 (first-rate; great; awesome) through 5 (really terrible) to answer each of these questions</p><br />
<hr>
<table align= "center">
<tr><th colspan= "3">Movie </th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>
<tr><td colspan= "3">1: The Godfather Part 1</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie1" value="1">1</td>
<td><input type= "radio" name= "movie1" value="2">2</td>
<td><input type= "radio" name= "movie1" value="3">3</td>
<td><input type= "radio" name= "movie1" value="4">4</td>
<td><input type= "radio" name= "movie1" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors1" value="1">1</td>
<td><input type= "radio" name= "actors1" value="2">2</td>
<td><input type= "radio" name= "actors1" value="3">3</td>
<td><input type= "radio" name= "actors1" value="4">4</td>
<td><input type= "radio" name= "actors1" value="5">5</td>
</tr>
<tr><td colspan= "3">2: Men of Honor</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie2" value="1">1</td>
<td><input type= "radio" name= "movie2" value="2">2</td>
<td><input type= "radio" name= "movie2" value="3">3</td>
<td><input type= "radio" name= "movie2" value="4">4</td>
<td><input type= "radio" name= "movie2" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors2" value="1">1</td>
<td><input type= "radio" name= "actors2" value="2">2</td>
<td><input type= "radio" name= "actors2" value="3">3</td>
<td><input type= "radio" name= "actors2" value="4">4</td>
<td><input type= "radio" name= "actors2" value="5">5</td>
</tr>
<tr><td colspan= "3">3: Three Days of the Condor</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie3" value="1">1</td>
<td><input type= "radio" name= "movie3" value="2">2</td>
<td><input type= "radio" name= "movie3" value="3">3</td>
<td><input type= "radio" name= "movie3" value="4">4</td>
<td><input type= "radio" name= "movie3" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors3" value="1">1</td>
<td><input type= "radio" name= "actors3" value="2">2</td>
<td><input type= "radio" name= "actors3" value="3">3</td>
<td><input type= "radio" name= "actors3" value="4">4</td>
<td><input type= "radio" name= "actors3" value="5">5</td>
</tr>
</table>
<hr>
<input type= "submit" value= "submit">
</form>
The real problem that I'm having trouble with is filling my array of movie choices, and actor choices, then printing that data for the user.
This is what I get when I run my code:
This is what I should be getting
DB Setup:
movies table
Fields: movie_id, movie_title
movie_ratings table
Fields: movie_ratings_id, movie_id, user_id, rating
actor_ratings table
Fields: actor_ratings_id, movie_id, user_id, rating
This way, you can store each individual rating individually, and do a COUNT and GROUP BY on your sql statement. That may get a little intense, since you're doing basic DB manipulation. You could always do a SELECT *, and then increase your Movie Ratings based on something like below.
The scope of this is not within answering the question though, and if you'd like to message/chat me, I can help you out that way.
ORIGINAL ANSWER BELOW:
First get your results from the database
$sql = "SELECT * FROM ratings";
// fetch results
Now that you have the results, you can structure it properly
I would do something like this. The reason I choose to do this is because then you have an associate array with named keys, and you'll be able to recall them later in a foreach loop or something similar.
$movieChoices = array(
array('title'=>"The Godfather Part 1",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
),
array('title'=> "Men Of Honor",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
),
array('title'=>"Three Days of Condor",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
)
);
Which will give you something like this:
Array
(
[0] => Array
(
[title] => The Godfather Part 1
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
[1] => Array
(
[title] => Men Of Honor
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
[2] => Array
(
[title] => Three Days of Condor
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
)
Store your post variables the same way for ease of code use.
$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);
Use a loop to store both items.
There's no sense in looping through the same thing twice. Re use the code.
I use += 1, because it's more apparent what you're doing to the variable.
It's the same thing as ++, it's just my preference.
Also, while you're in here, you could write a SQL statement to update the row in the Database.
$sql = "UPDATE movie_ratings SET RATING = RATING + 1 WHERE movie_id = MOVIE_ID";
But, that's for later. So go ahead and update your current values you already have.
for ($i = 0; $i < 3; $i++) {
if ($movieChoice[$i] == 1) {
//I increase the variable by 1 if it was selected.
$movieChoices[$i]['movieRatings']["great"] += 1;
} else if ($movieChoice[$i] == 2) {
$movieChoices[$i]['movieRatings']['good'] += 1;
} else if ($movieChoice[$i] == 3) {
$movieChoices[$i]['movieRatings']['ok'] += 1;
} else if ($movieChoice[$i] == 4) {
$movieChoices[$i]['movieRatings']['soso'] += 1;
} else {
$movieChoices[$i]['movieRatings']['terrible'] += 1;
}
if ($actorChoice[$i] == 1) {
//I increase the variable by 1 if it was selected.
$movieChoices[$i]['actorRatings']["great"] += 1;
} else if ($actorChoice[$i] == 2) {
$movieChoices[$i]['actorRatings']['good'] += 1;
} else if ($actorChoice[$i] == 3) {
$movieChoices[$i]['actorRatings']['ok'] += 1;
} else if ($actorChoice[$i] == 4) {
$movieChoices[$i]['actorRatings']['soso'] += 1;
} else {
$movieChoices[$i]['actorRatings']['terrible'] += 1;
}
}
Now when you want to print the results, it's even easier to do so. You can use a foreach() loop to go through each movie result, and print their individual values out. Much easier than what you were doing, less code.
print '
<table align="center">
<tr>
<th colspan="3">Movie</th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>';
foreach($movieChoices as $choice) {
print '
<tr>
<td colspan="3">'.$choice['title'].'</td>
<td>Quality of Movie</td>
<td>'.$choice['movieRatings']['great'].'</td>
<td>'.$choice['movieRatings']['good'].'</td>
<td>'.$choice['movieRatings']['ok'].' </td>
<td>'.$choice['movieRatings']['soso'].'</td>
<td>'.$choice['movieRatings']['terrible'].'</td>
</tr>
<tr>
<td colspan="3"> </td>
<td>Quality of Actors</td>
<td>'.$choice['actorRatings']['great'].'</td>
<td>'.$choice['actorRatings']['good'].'</td>
<td>'.$choice['actorRatings']['ok'].' </td>
<td>'.$choice['actorRatings']['soso'].'</td>
<td>'.$choice['actorRatings']['terrible'].'</td>
</tr>
';
}
print '</table>';
In all honesty, this is a REALLY long result for this bit of code. If I had more to work with, I could break it down to a lot simpler for you. However, this will help with what you're trying to do.
I had some free time and decided to play around with your idea. You should be able to copy/paste this into a single file and it should work.
I don't intend my answer to be a final solution but I hope that it gives you some new ideas about to structure your program. Feel free to pick and choose what you will from it.
The main idea here is that it is storing the user's selections into the session so that we can increment it and display a running total in the last table. I tried to document the code as I went along but pls feel free to ask questions.
I've also tried to make it relatively dynamic so you can add movies, questions, choices and the form and form processor should handle it OK as long as you keep the data structures the same. The arrays that store the movies, choices, and questions also use associative keys (strings such as '1', '2' etc.) which slightly mimics what a relational DB would use. Add/remove elements to these arrays to see how the form changes.
Finally, there are some language constructs (??, square arrays []) which require PHP 7 to run. There are also some lines of code which are... ugly. I didn't try to find a nicer way to code them.
Ex. list($movieId, $questionId) = array_pad(explode('_', $key, 2), 2, null);.
Let me know if you run into issues.
<?php
session_start();
// The following 3 arrays just store movie, choices, and questions
// which we will use in form processing and also to display the form to the user.
// Could come from DB, file, or hard-coded like this.
//
// Play around with these 3 arrays to see how the application changes.
// A list of movies.
$movies = [
[
'id' => '1',
'title' => 'Taxi Driver',
'director' => 'Mr. Scor-say-zee',
],
[
'id' => '2',
'title' => 'The Mask',
'director' => 'I do not know',
],
];
// A list of choices that a user can select.
$choices = [
'1' => 'Terrible',
'2' => 'So-So',
'3' => 'OK',
'4' => 'Good',
'5' => 'Great',
];
// A list if rating categories.
$questions = [
'1' => 'Quality of Movie',
'2' => 'Quality of Actors',
'3' => 'Quality of Producers',
];
// The reset button was clicked. Clear the session and redirect back to self.
if (isset($_POST['reset'])) {
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
die;
}
// The form was submitted. Store the user's selection in to the session.
// Storing it in the session for simplicity but could store it to a file or DB or some other persistent storage.
if (isset($_POST['submit'])) {
// Loop over the POST data and extract the question ID and movie ID for each submitted answer.
foreach ($_POST as $key => $value) {
// The incoming POST data looks like: 1_1, 2_1, 2_2 etc.
// Split the string on the underscore which will give us the movie ID and question ID respectively.
// Once we know which movie and question is submitted store them, along with the actual rating, in the session.
// If it doesn't exist in the session, set it to 1.
// If it does exist in the session, just increment the value.
list($movieId, $questionId) = array_pad(explode('_', $key, 2), 2, null);
if (is_numeric($questionId) && is_numeric($movieId)) {
// Check if it is in the session already.
if (!isset($_SESSION['ratings'][$movieId][$questionId][$value])) {
$_SESSION['ratings'][$movieId][$questionId][$value] = 1;
} else {
$_SESSION['ratings'][$movieId][$questionId][$value] = $_SESSION['ratings'][$movieId][$questionId][$value] + 1;
}
}
}
}
?>
<!-- Display the form -->
<form method="post">
<?php
foreach ($movies as $movie) {
echo '<h2>' . $movie['title'] . '</h2>';
foreach ($questions as $questionId => $questionDescription) {
echo '<h3>' . $questionDescription . '</h3>';
foreach ($choices as $choiceValue => $choiceDescription) {
// Each row will have a name like: 1_1, 1_2, 2_1 etc.
// These represent the movie ID and question ID respectively.
// During form processing (above) we split the name on the underscore which give use the movie ID and question ID that the user submitted.
// There is a probably a better way to do this.
echo '<label>';
echo '<input type= "radio" name= "' . $movie['id'] . '_' . $questionId . '" value="' . $choiceValue . '">';
echo $choiceDescription;
echo '</label>';
}
}
echo '<hr />';
}
?>
<br>
<br>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="reset" value="Reset">
</form>
<!-- Display the results from the session -->
<table border="1">
<caption>Favorite and Least Favorite Things</caption>
<tr>
<th>Movie</th>
<th>Question</th>
<?php
echo "<th>" . implode('</th> <th>', $choices) . "</th>";
?>
</tr>
<?php
foreach ($movies as $movie) {
echo '<tr>';
// Notice the rowspan grows with each question added.
echo '<th rowspan="' . count($questions) . '">' . $movie['title'] . '</th>';
foreach ($questions as $questionId => $questionDescription) {
// Open a new row on the 2nd and subsequent iterations.
if ((int)$questionId > 1) {
echo '<tr>';
}
echo '<th>' . $questionDescription . '</th>';
// Output the actual ratings from the session.
foreach ($choices as $choiceId => $choiceDescription) {
echo '<th>';
echo $_SESSION['ratings'][$movie['id']][$questionId][$choiceId] ?? 0;
echo '</th>';
}
}
echo '</tr>';
}
?>
</table>

Incorrect values being passed from HTML form

The undernoted form gives a series of options to choose using a select form. When the values are retrieved from the $_POST array although the correct id is being passed the value for season simply seems to be the last value in the form.
I have checked my code and league is not being updated anywhere else as far as I can see.
I'm sure I'm missing something obvious but can't figure it out!
<form method = 'post' action = 'index.php?choice=matcht&stage=1'><table width = '50%' border = '1'><thead><tr>
<th>Select</th>
<th>League</th>
<th>Season</th>
</tr></thead>
<tr><td><input type = 'radio' name = 'id' value = '1' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '12' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '25' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '13' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '49' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '14' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '73' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '15' readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '97' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season' value = '16' readonly></td>
</tr>
<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>
</table>
</form>
Here is the PHP generating the HTML:
$sql='SELECT `TeamID`, `League`,`Season` FROM teams WHERE `FranchiseID` = 0 GROUP BY `League`,`Season` ORDER BY `League` ASC ,`Season` ASC ';
$unmatched = $db2->get_results($sql);
if ($db2->num_rows>0) {
echo "<form method = 'post' action = 'index.php?choice=matcht&stage=1'>";
echo "<table width = '50%' border = '1'>";
echo "<thead><tr>\n";
echo "<th>Select</th>\n";
echo "<th>League</th>\n";
echo "<th>Season</th>\n";
echo "</tr></thead>\n";
foreach ( $unmatched as $unmatch )
{
// Access data using object syntax
echo "<tr>";
echo "<td><input type = 'radio' name = 'id' value = '$unmatch->TeamID' ></td>\n";
echo "<td><input type = 'text' name = 'league' value = '".$unmatch->League."' readonly></td>\n";
echo "<td><input type = 'text' name = 'season' value = '".$unmatch->Season."' ></td>\n";
echo "</tr>\n";
}
echo "<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>\n";
echo "</table>\n";
echo "</form>\n";
All of the text inputs have the same name and the last is overwriting the others. Use an array and set the index to the id so you know which ones to get:
<tr><td><input type = 'radio' name = 'id' value = '1' ></td>
<td><input type = 'text' name = 'league[1]' value = 'MLB21' readonly></td>
<td><input type = 'text' name = 'season[1]' value = '12' readonly></td>
Then you can get it like:
$id = $_POST['id'];
$league = $_POST['league'][$id];
$season = $_POST['season'][$id];

How to have dropdown data WHERE we get from other table for the editing data

I want to have the same drop down Hardware_ID in Adding new record form WHERE I get the data from another table for my Update form.
This is the example for my Update (Edit) form.
Update Form
And this is my Adding new record form.
Adding new record Form
this is my code for Update Form. Im using the same code like my adding new record but I got an error(The error is in the Update Form).
<?php
//get the data
$Asset_ID = $_GET['Asset_ID'];
$Hardware_ID = $_GET['Hardware_ID'];
$Vendor_ID = $_GET['Vendor_ID'];
$PO_ID = $_GET['PO_ID'];
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10'
bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo
$Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo
$Hardware_ID?>">
<?php
$query = "SELECT * FROM hardware2 ORDER BY Hardware_ID";
$result = mysql_query($query);
if(mysql_num_rows($result))
{
while ($id = mysql_fetch_row($result))
{
echo "<option value='" . $id[0] . "'>" . $id[0] . " : " .
$id[1] . " </option>";
}
}
?>
</select>
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo
$Vendor_ID; ?>"></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?
>"></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>
thanks for your help, Regards
Wrong select box syntax
<?php
//get the data
$Asset_ID = isset($_GET['Asset_ID']) ? $_GET['Asset_ID'] : 0;
$Hardware_ID = isset($_GET['Hardware_ID']) ? $_GET['Hardware_ID'] : 0;
$Vendor_ID = isset($_GET['Vendor_ID']) ? $_GET['Vendor_ID'] : 0;
$PO_ID = isset($_GET['PO_ID']) ? $_GET['PO_ID'] : 0;
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10' bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo $Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo $Hardware_ID?>">
<select id="Your_id" name="Your_name">
<?php
$query = "SELECT * FROM hardware2 ORDER BY Hardware_ID";
$result = mysql_query($query);
if(mysql_num_rows($result))
{
while ($id = mysql_fetch_row($result))
{
echo "<option value='".$id[0]."'>".$id[0]." : ".$id[1]."</option>";
}
}
?>
</select>
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo $Vendor_ID; ?>" /></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?>" /></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>
Maybe no need the select box in this form
<?php
//get the data
$Asset_ID = isset($_GET['Asset_ID']) ? $_GET['Asset_ID'] : 0;
$Hardware_ID = isset($_GET['Hardware_ID']) ? $_GET['Hardware_ID'] : 0;
$Vendor_ID = isset($_GET['Vendor_ID']) ? $_GET['Vendor_ID'] : 0;
$PO_ID = isset($_GET['PO_ID']) ? $_GET['PO_ID'] : 0;
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10' bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo $Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo $Hardware_ID?>">
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo $Vendor_ID; ?>" /></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?>" /></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>

Codeigniter multiple insert using radio button

HI I NEED YOUR HELP..
I have this VIEW which show all items according to its company
<?php foreach($item_list as $item2):?>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="<?php echo $item2->item_id ?>" ></td>
<td><?php echo $item2->item_desc?></td>
<td><?php echo $item2->serial_num ?></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "job order" ></td>
</tr>
<?php endforeach; ?>
---------------------------------------- html output ---------------------------------------
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="146" ></td>
<td>sample item 01</td>
<td>123</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="147" ></td>
<td>sample item 02</td>
<td>21344</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
My problem is that I want to insert this to new value (status, item_id, item_desc, serial_num, JobStatus) to quotation table in mysql. If Click I on radio item_id 146 row it will insert all values under item_id 146 likewise to item_id 147 it will insert all item values under 147 item_id
So you want the radio button to be checked according to it's job status?????
If so, try something along these lines ( i don't know how your your database is set up so you will have to alter the following):
<?php foreach($query as $item):?>
<td><input type = "text" name = "item_id" value ="<?php echo $item->item_id ?>" ></td>
<td><?php echo $item->item_desc?></td>
<td><?php echo $item->serial_num ?></td>
<td><input type="radio" name="JobStatus" value="quotation" <?php $item->job_status == 'quotation' ? echo "checked" : null; ?> ></td>
<td><input type="radio" name="JobStatus" value="job order" <?php $item->job_status == 'job order' ? echo "checked" : null; ?> ></td>
</tr>
<?php endforeach; ?>
Enclose the whole loop inside a form tag with action="index.php/controller/method"
In your controller,
public function method() {
$items = $this->input->post('item_id', TRUE);
$job_status = $this->input->post('JobStatus', TRUE);
$stati = $this->input->post('status', TRUE);
foreach($items as $key => $row) {
$this->your_model->insert_new($row, $job_status[$key], $stati[$key]);
}
}
In your_model, write the database query to insert these data wherever you need. Of course you need to change the names - method, controller and your_model to suit your application.

input multiple value to one row in codeigniter

I have 3 pieces of the column, which I will inserting in one table in my database. The name of each form like this:
<h3><?= $title; ?></h3><?php echo form_open("admin/artikel/buat/");?>
<table width="95%">
<tr>
<td><b>Pilih Referensi Jurnal</b></td>
<td>
<input type="hidden" name="IDJurnal" id="IDJurnal" value="<?php echo $IDJurnal; ?>" />
<input type="text" name="volume" id="volume" value="<?php echo $volume; ?>" readonly="readonly" class="sedang" />
<?php echo anchor_popup('admin/artikel/popup', 'Referensi Jurnal', array('class' => 'button')); ?>
</td>
</tr>
<tr>
<td><b>Kategori</b></td>
<td>
<?php
echo form_dropdown('IDKategori', $kategori) . "";
?>
</td>
</tr>
<tr>
<td width="125"><strong>Judul</strong></td>
<td><input type="text" name="judul" class="panjang"></td>
</tr>
<tr>
<td width="125"><strong>Pengarang</strong></td>
<td>
<input type="text" name="pengarang" class="panjang">
<input type="text" name="pengarang" class="panjang">
<input type="text" name="pengarang" class="panjang">
</td>
</tr>
<tr>
<td><b>Abstract</b></td>
<td>
<?php
$data = array('name' => 'abstract');
echo $this->ckeditor->editor($data['name']);
?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" class="button" value="Simpan">
<input type="button" class="button" value="Batal" onClick="javascript: history.go(-1)" />
</td>
</tr>
<?php
echo form_close();
?>
i want inserting the value from each form name to my sql table, like this:
CREATE TABLE `pengarang`(
`IDPengarang` INT(11)NOT NULL AUTO_INCREMENT,
`IDArtikel` INT(11)NOT NULL,
`nama_pengarang` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDPengarang`))ENGINE = INNODB DEFAULT CHARSET = utf8;
in my model like this:
function addArtikel() {
$now = date("Y-m-d H:i:s");
$data = array(
'IDJurnal' => $this->input->post('IDJurnal'),
'IDKategori' => $this->input->post('IDKategori'),
'judul' => $this->input->post('judul'),
'abstract' => $this->input->post('abstract'),
'created_time' => $now,
'created_by' => $_SESSION['username']
);
// $data1 = array(
// 'IDJurnal' => $this->input->post('IDJurnal'),
// 'IDKategori' => $this->input->post('IDKategori'),
// 'judul' => $this->input->post('judul'),
// 'abstract' => $this->input->post('abstract'),
// 'created_time' => $now,
// 'created_by' => $_SESSION['username']
// );
// $data2 = array(
// 'IDArtikel' => $this->input->post('IDArtikel'),
// 'nama_pengarang' => $this->input->post('pengarang')
// );
$this->db->insert('artikel', $data);
// $this->db->insert_batch('artikel', $data1);
// $this->db->insert_batch('pengarang', $data1);
}
in my controller
function buat() {
if ($this->input->post('judul')) {
$this->MArtikel->addArtikel();
$this->session->set_flashdata('message', 'Artikel telah di buat !');
redirect('admin/artikel/index', 'refresh');
} else {
// konfigurasi ckfinder dengan ckeditor
$this->load->library('ckeditor');
$this->load->library('ckfinder');
$this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
$this->ckeditor->config['toolbar'] = 'Full';
$this->ckeditor->config['language'] = 'en';
$this->ckfinder->SetupCKEditor($this->ckeditor, '../../../asset/ckfinder/');
$data['title'] = "Tambah Artikel";
$data['main'] = 'admin/artikel/artikel_buat';
$data['jurnal'] = $this->MJurnal->getJurnalDropDown();
$data['kategori'] = $this->MKategori->getKategoriDropDown();
$this->load->vars($data);
$this->load->view('dashboard/template');
}
}
What should I add to my controller and my model, that would make the results in my table looks like this
IDPengarang IDArtikel nama_pengarang
1 1 testing 1
2 1 testing 2
3 1 testing 3
thank's before

Categories