How do i define from which table that specific column comes from? - php

I made a page in which one can edit the prices for each article that each webshop has. The price that it already has is in the textfield and it can be changed to whatever the user may want. Then the user hits the Edit (Wijzigen is Dutch for Edit) button and it should change to whatever was in the textfield before pressing it.
But I'm having quite some trouble with the UPDATE query since I need to give it some ID's to select the right one. And, as you'll be able to see in the source, right after starting the while() loop, I can't specify the right ID from the right table.
I would appreciate it if you could help me out with what I need to change, add or delete to make this work.
<?php
include 'inc/inc.php';
$artikel = mysqli_query($mysql, "SELECT a.Artikel_ID, a.Productnaam, aw.Artikel_ID, aw.Webshop_ID, aw.Prijs, w.Webshop_ID, w.Shopnaam FROM artikel a, artikel_webshop aw, webshops w WHERE a.Artikel_ID = aw.Artikel_ID AND aw.Webshop_ID = w.Webshop_ID GROUP BY aw.Webshop_ID, aw.Artikel_ID");
echo '<table id="specialtable"><tr style="background-color:#F8F8F8;"><td>Productnaam</td><td>Webshop</td><td>Prijs</td><td>Wijzigen</td></tr>';
$i = 0;
$j = 1;
while($artikelR = mysqli_fetch_assoc($artikel)) {
$artikel_id = $artikelR['Artikel_ID'];
$webshop_id = $artikelR['Webshop_ID'];
echo '<form method="post">';
if($i == 0){
echo '<tr style="background-color:#DDD;">';
$i = $i + 1;
}else{
echo '<tr style="background-color:#F8F8F8;">';
$i = $i - 1;
}
echo '<td>' . $artikelR['Productnaam']. '</td><td>' . $artikelR['Shopnaam'] . '</td><td>€ <input type="text" name="' . $j . '" value="' . $artikelR['Prijs']. '"></td>';
echo '<td><input type="submit" name="' . $j . '" value="Wijzigen"></td></tr></form>';
$jNew = $_POST['' . $j . ''];
$j++;
$test = $_POST['' . $j . ''];
if(isset($test)) {
$sql = mysqli_query($mysql, "
UPDATE artikel_webshop
SET Prijs = '" . $jNew . "'
WHERE Artikel_ID = '$artikel_id'
AND Webshop_ID = '$webshop_id'");
echo 'U heeft succesvol de prijs van het artikel ' . $artikelR['Productnaam'] . ' van de webshop ' . $artikelR['Shopnaam'] . ' gewijzigd.<br><br>';
$url = 'prijsWij.php';
echo '<meta http-equiv="refresh" content="4;URL=' . $url . '">';
}
}
echo '</table></form>';
?>
Edit
I finally got it to work. I'm pretty bad at explaining, so I'll just add the new code:
<?php
include 'inc/inc.php';
$artikel = mysqli_query($mysql, "
SELECT a.Artikel_ID, a.Productnaam, aw.Artikel_ID as awa_id, aw.Webshop_ID as aww_id, aw.Prijs, w.Webshop_ID, w.Shopnaam
FROM artikel a, artikel_webshop aw, webshops w
WHERE a.Artikel_ID = aw.Artikel_ID
AND aw.Webshop_ID = w.Webshop_ID");
echo '<table id="specialtable"><tr style="background-color:#F8F8F8;"><td>Productnaam</td><td>Webshop</td><td>Prijs</td><td>Wijzigen</td></tr>';
$i = 0;
$j = 1;
while($artikelR = mysqli_fetch_assoc($artikel)){
$artikel_id = $artikelR['awa_id'];
$webshop_id = $artikelR['aww_id'];
echo '<form method="post">';
if($i == 0){
echo '<tr style="background-color:#DDD;">';
$i = $i + 1;
}else{
echo '<tr style="background-color:#F8F8F8;">';
$i = $i - 1;
}
echo '<td>' . $artikelR['Productnaam']. '</td><td>' . $artikelR['Shopnaam'] . '</td>';
echo '<td>€ <input type="text" name="prijsTest" value="' . $artikelR['Prijs']. '"></td>';
echo '<td><input type="submit" name="' . $j . '" value="Wijzigen"></td></tr></form>';
$kNew = $_POST['prijsTest'];
$test = $_POST['' . $j . ''];
$j = $j + 1;
if(isset($test)){
$sql = mysqli_query($mysql, "
UPDATE artikel_webshop
SET Prijs = " . $kNew . "
WHERE Artikel_ID = '$artikel_id'
AND Webshop_ID = '$webshop_id'");
echo 'U heeft succesvol de prijs van het artikel ' . $artikelR['Productnaam'] . ' van de webshop ' . $artikelR['Shopnaam'] . ' gewijzigd.<br><br>';
$url = 'prijsWij.php';
echo '<meta http-equiv="refresh" content="4;URL=' . $url . '">';
}
}
echo '</table></form>';
?>

SELECT a.Artikel_ID as a_id, a.Productnaam, aw.Artikel_ID as aw_id ...

In order to know which article was updated, you need to keep track of it in your form so that when the form gets submitted, you will be able to update the correct article.
It seems like you have one form per article. In this case, it's easy. When you generate your form, add a hidden input containing the article ID:
<input type="hidden" name="article_id" value="<?php echo $articleId; ?>"/>
When the form will be submitted, the article ID will be posted along with the new price. You can retrieve it through $_POST['article_id']. Then, you can use this variable in your MySQL query to update the right article!

Related

How to retain the value of Select Option inside a For Loop using PHP

I have a working code below. Basically, the code prints the options 50 times and 4 is the default selected option.
for ($i = 1; $i <= 50; $i++) {
if($i == 4){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
but every time i click the submit button, the option field resets back to the selected option even though the variables was captured properly.
It would be good if the option selected by the user is preserved after the button was clicked and the only time it reset back to the selected defaults is if the page was refreshed.
check if the value is sent by post if not assign 4 to the variable option
<form action="#" method="post" accept-charset="utf-8">
<select name="select">
<?php
if(isset($_POST['select']))
{
$option= $_POST['select'];
}
else
$option=4;
for ($i = 1; $i <= 50; $i++) {
if($i == $option){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
?>
</select>
<input type="submit" name="" value="Sumbit">
</form>
This should do it - it uses the value sent from the client if it exists, or 4 if not.
if (isset($_POST['selected_value'])) {
$selected = $_POST['selected_value'];
} else {
$selected = 4;
}
for ($i = 1; $i <= 50; $i++) {
if($i == $selected){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}
Take value from posted field, say "select", if exist then take it as selected otherwise your default value.
Using below code:
$selected = (isset($_POST['select']) ? $_POST['select'] : 4);
for ($i = 1; $i <= 50; $i++) {
if($i == $selected){
echo '<option value="' . $i . '" selected>' . $i . '%</option>';
}else {
echo '<option value="' . $i . '">' . $i . '%</option>';
}
}

Issues adding a set of database columns to array

I have the following table that queries users from my database and then adds 14 players under each user. I added a couple of columns to the same database table and I am trying to get the columns to echo out and it is throwing an undefined index error, but I'm not entirely sure where I can define it in here.
This is the part I am trying to get the new columns to echo out at. I have 14 players, so this array runs through them all for each user. I am adding the same number of 'positions' So it will be player1 position1, player2 position2, etc.
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';
I tried to do this and this is why I am getting the errors...
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>';
How else can I format this, so that I can get the position to show up next to the player. Like this:
Player1 - Position1
Full code:
<?php $userPlayerStore = array(); ?>
<table class="draft_border_table">
<tr>
<th>Rnd</th>
<?php
// Output usernames as column headings
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`');
while($userPlayer = mysqli_fetch_array($userResults)) {
$userPlayerStore[] = $userPlayer;
echo '<th><div>' . $userPlayer['username'] . '</div></th>';
}
?>
</tr>
<?php
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
if (empty($userPlayer['player'.$playerNum])){
$extra_class = 'emptycell';
}else{
$extra_class = 'filledcell';
}
echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>';
}
echo '</tr>';
}
?>
</table>
The $userPlayer var is a reflection of each record in your database table with each index in this array being the column name. The code below should help you:
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
// Start the player row
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
// Retrieve extra class name? Did you mean to use this?
$extraClass = empty($userPlayer['player' . $playerNum]) ? 'emptycell' : 'filledcell';
// Output player name and position
$playerName = $userPlayer['player' . $playerNum];
$playerPosition = $userPlayer['position' . $playerNum];
echo '<td class="draft_table_td">' . $playerName . ' - ' . $playerPosition . '</td>';
}
// End the player row
echo '</tr>';
}

php add random array not repeating most recent 5

I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>

Creating dynamic dropdown lists

I want to create dynamic select lists. For example: I have 5 students in my db. My goal is to create 5 selects ,and in every select all students which are in database. So if user inserts a 6th student in the database, the page should display 6 selects, and in every select names of 6 students.
I've tried with this code, but it only creates 1 select, containing 4 students(the first one from the db is missing).
The Code:
$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$i=1;
$execute=mysqli_query($con,$exe);
while($result=mysqli_fetch_array($execute))
{
echo '<select name=student'.$i.'>';
echo '<option value="-1" >Choose student</option> <br/>';
while($res=mysqli_fetch_array($execute))
{
echo '<option value='.$res["id_student"].'>'.$res["name"].'</option> <br/>';
}
echo '</select>';
$i++;
}
$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$i=1;
$execute=mysqli_query($con,$exe);
$select = '';
for($j = 0; $j < mysqli_num_rows($execute); $j++) {
$select .= '<select name=student' . $j . '>';
$select .= '<option value="-1" >Choose student</option> <br/>';
while($result=mysqli_fetch_array($execute)) {
$select .= '<option value=' . $res["id_student"].'>' . $res["name"] . '</option>';
$i++;
}
$select .= '</select>';
}
echo $select;
UPDATE
inside the while
$select = '<select name=student' . $j . '>';
$select .= '<option value=' . $res["id_student"].'>' . $res["name"] .
change this lines by this other
$select = '<select name="student' . $j . '">';
$select .= '<option value="' . $res["id_student"]."'>' . $res["name"] .
we missed the double quotes for value and in select name
Problem solved, I just needed to insert vars $exe and $execute inside for loop, so after every iteration $result is refreshed,otherwise it just prints options in first select...
CODE:
$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$execute=mysqli_query($con,$exe);
$select = '';
for($j = 0; $j < mysqli_num_rows($execute); $j++)
{
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$execute=mysqli_query($con,$exe);
$select .= '<select name=student' . $j . '>';
$select .= '<option value="-1" >Choose student</option> <br/>';
while($result=mysqli_fetch_array($execute))
{
$select .= '<option value=' . $res["id_student"].'>' . $res["name"] . '</option>';
}
$select .= '</select>';
}
echo $select;

How do I add x-number HTML-formatted items to my page?

I'm trying to use PHP to do this:
<if !empty($list) {echo
.
.
.
?>
And get the result:
Additional options:
<p><input type="checkbox" name="1">1</input></label></p>
<p><input type="checkbox" name="2">2</input></label></p>
.
.
.
<p><input type="checkbox" name="n">n</input></label></p>
</legend>
</fieldset>
Given the context of the question, I am guessing here. But it seems that the you do not understand the checkbox, given that you do not even assign it a value that and this would be a pain to loop through on the form processing end.
Assuming that $list is an array (borrowing some code from Gazler)
$cnt = count($list);
$checkBoxes = "";
for ($i=1; $i<$cnt; $i++) {
$checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}
echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>';
Then on your form processing side, it will be easy to loop through the checked boxes like so:
if (isset($_POST['checkBoxes'])) {
foreach ($_POST['checkBoxes'] as $val) {
// $val will contain the value of the selected boxes
}
}
Using this system, it should get you to where you want to be.
Assuming $list is an array
for($i=1; $i<count($list); $i++)
{
echo '<p><input type="checkbox" name="'.$i.'">'.$i.'</input></label></p>'."\n";
}
If not, please provide the contents of $list.
Part 1:
// echo the 3-bar, expanded series (alternating sequence), of the tags array
<legend>Additional Options:</p>
$cnt = count($list);
$checkBoxes = "";
for ($i=1; $i<$cnt; $i++) {
$checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}
echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>' . "\n" . </legend>;
Part 2:
// loop through the checkboxes
if (isset($_POST['checkBoxes'])) {
foreach ($_POST['checkBoxes'] as $val) {
// $val will contain the value of the selected boxes
}
}

Categories