I have a php page that updates scores in a MySQL database. Currently the page displays the value from the db. I want to change that to a dropdown list (0-9) that will then update the database. I'm a newbie and can't figure this out. Your help would be greatly appreciated. Here is what I have so far. I have truncated the code;
<h1>UPDATE SCORES</h1>
<form method="post" action="add_scores.php" >
<table width="60%" cellpadding="15" style="font-family: verdana, arial; border: #FFCC99 1px solid">
<tr>
<td height="28" colspan="24" align="center" style="font-size: 44px">
<strong><li>1st Quarter:</li></strong><br>
<strong><font color="#DB2824">NFC: </font></strong><input size="4" maxlength="1" name="away_first" value="<?php echo $record['away_first'];?>"/>
<strong><font color="#232B85">AFC: </font></strong><input size="1" maxlength="1" name="home_first" value="<?php echo $record['home_first'];?>"/>
<br><br>
didn't test it, but this function should generate the code for you:
<?php
function generateSelect($name, $value) {
$str = "<select name='$name'>";
foreach(\range(0,9) as $i) {
$selected = $i == $value ? 'selected' : '';
$str .= "<option value='$i' $selected>$i</option>";
}
$str .= "</select>";
return $str;
}
Replace your inputs with something like this:
<select name="away_first">
<?php
for ($i = 0; $i <= 9; $i++) {
if ($record['away_first'] == $i) {
echo '<option selected>' . $i . '</option>';
} else {
echo '<option>' . $i . '</option>';
}
}
?>
</select>
This is just an example, nothing I've tested.
If you want the form to autopost you could just add a simple onchange-listener to it, either inline or in a script block/file.
Related
I have the following code in my program that works perfectly with the WAMP server. But I had to change the WAMP server for XAMPP and I don’t know why it doesn’t execute some parts of the code. There’s also no error message.
The problem is that in the table code <table>, it doesn’t show after the third <tr> or when the 'Toggle' starts. It also doesn’t show the submit button.
I don’t understand why everything works correctly in WAMP but not in XAMPP. Can someone help me see why it fails? Thank you
Project.php
<?php
include_once("Conexion.php");
include_once("functions.php");
ob_start();
$_SESSION['ex_time'] = date('Y-m-d H:i:s');
$list_ex = array();
$result = doSearch($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styleProject.css">
</head>
<body>
<div id="centered_A">
<form action='' method='post'>
<table id="exercises">
<tr>
<th colspan="3"><h2>Exercises</h2></th>
</tr>
<tr>
<th><h3>Exercise_ID</h3></th>
<th><h3>Title</h3></th>
<th><h3>Difficulty
<form id="difficulty_form" method="get">
<select id="filter" name="filter" onchange="document.getElementById('difficulty_form').submit()">
<option value="All">Todas</option>
<?php
for ($i = 1; $i <= 5; $i++) {
$selected = '';
if (isset($_GET['filter']) && $_GET['filter'] == $i) {
$selected = ' selected';
}
echo "<option value=\"$i\"$selected>$i</option>\n";
}
?>
</select>
</form>
</h3></th>
</tr>
<?php
$order = 1;
$num_ex = 1;
while($row = $result->fetch_assoc())
{
array_push($list_ex, $row["exercise_id"]);
$sql_check_ex = "select * from answers a where a.student_id=".$_SESSION['user_id']." and a.exercise_id_fk=".$row["exercise_id"].";";
$result_check_ex = $conn->query($sql_check_ex);
?>
<tr>
<td><?php echo $num_ex; ?></td>
<!---Click Toggle Exercise-->
<td><a onclick="myFunction(<?php echo $row["exercise_id"] ?>)" role="button" class="btn" target="_blank" ><?php echo $row["title"]; ?></a>
</td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<!--- Toggle --->
<tr id="toggle<?php echo $row["exercise_id"] ?>" style="display:none">
<td colspan="3">
<?php
$sql = "SELECT * FROM exercises WHERE exercise_id='".$row["exercise_id"]."'";
$result_ej = $conn->query($sql);
$row_ej = $result_ej->fetch_assoc();
?>
<p><?php $row["exercise_id"] ?> <?php echo $row["text"]?></p>
<br><!--- Radio Button --->
<?php
if ($row["type"] == 0) {
$ansarray = explode("\n", $row["image_path"]);
$randomans = [];
for($i=0; $i<count($ansarray); $i++) {
$a = array($i+1, $ansarray[$i]);
array_push($randomans, $a);
}
shuffle($randomans);
for($i=0; $i<count($randomans); $i++) {
echo "<div class=\"radio\" style=\"text-align:left; display:flex; vertical-align: baseline;\">";
echo "<input type=\"radio\" style=\"margin-top: 8px; cursor:pointer;\" name=\"choice".$row["exercise_id"]."\" value= \"".$randomans[$i][0]."\" />".$randomans[$i][1]."<br>";
echo "</div>";
}
echo "<input type=\"text\" name=\"choicetext".$row["exercise_id"]."\" value='multi' style=\"display:none;\">";
} else {
?>
<input type="radio" style="margin-top: 8px" name="choice<?php echo $row["exercise_id"] ?>" value= "0" checked="checked" style="display:none;" />
<?php
}
?>
<br>
<!--- Select level --->
<p2>Select difficulty level:</p2>
<select name="choose<?php echo $row["exercise_id"] ?>" id="choose<?php echo $row["exercise_id"] ?>">>
<option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
<option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
</select>
</td>
</tr><!---Finish Toggle --->
<?php
$order++;
$num_ex++;
}
?>
</table>
<br><!---Submit Button-->
<input class="buttonSubmit" type="submit" name="submit_proj_ex" value="Submit">
</form>
<script>
function myFunction(ejer_id) {
var x = document.getElementById("toggle" + ejer_id);
if (x.style.display === "none") {
x.style.display = "table-row";
} else {
x.style.display = "none";
}
}
</script>
</div>
</body>
<?php
if (isset($_POST['submit_proj_ex'])) {
include_once("store_answers.php");
header('location: Results.php');
}
?>
</html>
functions.php
<?php
function doSearch($conn, $fields = "*") {
$sql = "SELECT $fields FROM exercises, ex_tag, tags where exercise_id = exercise_id_fk and tag_id = tag_id_fk";
if (isset($_GET['filter']) && $_GET['filter'] !== 'All') {
$filter = (int) trim($_GET['filter']);
$sql .= " and exercises.difficulty = '$filter'";
}
if (!empty($_SESSION['tags_array'])) {
$sql .= " and (";
foreach ($_SESSION['tags_array'] as $tagId)
$sql .= 'tag_id = ' . $tagId . ' or ';
$sql .= "tag_id = -1)";
}
$sql .= ' group by exercise_id_fk';
return $conn->query($sql);
}
?>
Run phpinfo() in both WAMP and XAMP. I'm not certain what your issue is, but if it works in one and not the other, perhaps they are using different versions of php. If this is the case, you will need to use the same version of php in your XAMP as you were using in WAMP, or go through your code and try to figure out how to convert your code, such that it works correctly with the version on your XAMP. The former is probably the path of least resistance.
I am Having an issue in my for loop using PHP, I want to create dynamic row and column, Each row has 10 column after 10 column, the second row also end with 10 column like this up to 5 row, how to do It for a loop.
My for loop code:
<table width="100%" border="1">
<?php
for($i=1; $i<=72; $i++)
{
?>
<tr>
<td width="100%">
<?php echo "Click Here to see Site No.'".$i."'. & Area sqft No" .$i;?></a></td>
</tr>
<?php
}
?>
</tr>
</table>
I tried like this also
<table width="100%">
<tr>
<?php
for($i=1; $i<=72; $i++)
{
$x = 10;
if ($i % $x == 0)
{
?>
<td><?php echo $i;?></td>
<?php
}
}
?>
</tr>
</table>
<?php
echo '<table width="100%" border="1">';
for($i=1; $i<=8; $i++)
{
$y=10;
$y*=($i-1);
echo '<tr>';
for ($x=1; $x <=10; $x++) {
if ($i==1) {
echo '<td>'.$x.'</td>';
}else{
$y+=$x;
echo '<td>'.$y.'</td>';
if ($y==72) {
break;
}
$y-=$x;
}
}
echo '</tr>';
}
echo '</table>';
This will print the bellow table:
As per what I understand you want simple 10 column in each row.
Here is my code which may help you:
<table style="border:1px solid #000">
<tr>
<?php $t=1; for($k=1;$k<=72;$k++){?>
<?php if($t == 10) { $t=0;?><td style="border:1px solid #000"> <?php echo $k; ?> </tr><?php } else {?><td style="border:1px solid #000"> <?php echo $k; ?></td><?php } ?>
<?php $t++;}?>
<?php $x = 10; if ($i % $x == 0) { ?>
....
<?php } ?>
If I am getting the question correct, why don't you do as below:
echo '<table>';
for($i=1; $i<=5; $i++) {
echo '<tr>';
for ($y=1; $y<=10; $y++) {
echo '<td>Row_'.$i.' - Col_'.$y.'</td>';
}
echo '</tr>';
}
echo '</table>';
It will print something like below:
Having an issue with assigning a td style class, depending on a variable from mySQL. I have a field called 'Avail' so when a seat is available, the seat should turn green. However, nothing is happening so far when I run it.
Have created the simple classes on the same html sheet but it doesn't seem to apply to any of the data.
CSS Class Definitions >>
<style>
.available {
background-color: green;
}
.unavailable {
background-color: red;
}
</style>
Code to show when style will be applied >>
<table>
<tr>
<th>Seats</th>
<tr>
<tr>
<?php
$c = 0; // Our counter
foreach($res as $row) {
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
{
$seatColour = '.available';
} else
{
$seatColour = '.unavailable';
}
if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
{
// New row
echo '</tr><tr>';
}
$c++;
?>
<td style="$seatColour">
<form method="POST" name="Seating" action="SWG.php">
<?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?>
<input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/>
</td>
<?php
}
?>
</tr>
</table>
Would really appreciate it if someone could point out where I'm wrong / how I'm assigning it incorrectly.
There seem to be several issues with your code:
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
In the above code, you assign $Avail a string, and then you check whether it's zero. I'm assuming this is not deliberate. Maybe you want to assign the database value here (instead of a string)?
$seatColour = '.available';
You probably want to do $seatColour = "available". We're not in jQuery so no need for the dot.
td style="$seatColour"
You probably want to do <td class="<?php echo $seatColour ?>"> over here
Use class attribute instead of style in td and remove . from $seatColour as follows:
<table>
<tr>
<th>Seats</th>
<tr>
<tr>
<?php
$c = 0; // Our counter
foreach($res as $row) {
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
{
$seatColour = 'available';
} else {
$seatColour = 'unavailable';
}
if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
{
// New row
echo '</tr><tr>';
}
$c++;
?>
<td class="$seatColour"><form method="POST" name="Seating" action="SWG.php">
<?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?>
<input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/>
</td>
<?php } ?>
</tr>
</table>
instead of null try -1 .
or you put name/value for th.
<!DOCTYPE html>
<html lang="en">
<head>
<h1>Table Generator</h1>
</head>
<body>
<center>Refresh</center>
<?php
$rows = (isset($_POST['rows']) ? $_POST['rows'] : null);
$cols = (isset($_POST['cols']) ? $_POST['cols'] : null);
$highlight = (isset($_POST['highlight']) ? $_POST['highlight'] : null);
if ($rows == "")
{
$rows = 10;
}
if ($cols == "")
{
$cols = 10;
}
if ($highlight == "")
{
$highlight = 5;
}
?>
<form method="post">
ROWS <input type="text" name="rows" value = "<?php echo $rows;?>" />
COLUMNS <input type="text" name="cols" value = "<?php echo $cols;?>" />
HIGHLIGHT <input type = "text" name = "highlight" value = "<?php echo $highlight;?>" /><br>
<input type="submit" value="Generate">
</form>
<?php
if(isset($_POST['rows']))
{
$randnumber = rand(0,100);
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$highlight = $_POST['highlight'];
echo '<table border="1" align = "center">';
if (is_numeric($rows) and is_numeric($cols) and is_numeric($highlight))
{
if ($randnumber % 2 == 0)
{
echo '<center>The first number is <div class = "red">even</div></center>';
}
else
{
echo '<center>The first number is <div class = "green">odd</div></center>';
}
for($row = 1; $row <= $rows; $row++)
{
echo '<tr style = "background-color:green">';
for($col = 1; $col <= $cols; $col++)
{
if ($randnumber % $highlight == 0)
{
echo '<td style = "background-color: red">';
echo $randnumber;
$randnumber++;
echo '</td>';
}
else
{
echo '<td>';
echo $randnumber;
$randnumber++;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "<center>Rows / Columns / Highlight must ALL be INTEGER values. Re-enter correct value(s).</center>";
}
echo '<pre><center>';
print_r($_POST);
echo '</center></pre>';
}
?>
<style type ="text/css">
h1 {
color: grey;
text-align:center;
}
form {
text-align: center;
padding-bottom: 20px;
}
a:link {
text-decoration: none;
}
.red {
color: red;
}
.green {
color: green;
}
</style>
</body>
</html>
So. I have this PHP code to generate a table based off the user's input and I recently ran into a problem I cant figure out how to fix.
It was working perfectly fine but now whenever I use the Refresh link it resets the entire page to default (i.e. default textbox values instead of keeping the current ones, removing the table).
So, I have 2 questions. How would I keep the data on refresh (with $_POST being used) and how to display the table with the default values when the page first loads.
Refresh
Clicking it will trigger browser's reload mechanism and you'll be asked to resubmit the form action, it will allow you to keep POST data.
You need to re-create the post if you want to keep the parameters. Can be done pretty eaily by looping thru the array.
<form method='POST' id='refresh' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php foreach($_POST as $k=>$v): ?>
<input type='hidden' name='<?php echo $k; ?>' value='<?php echo $v; ?>' />
<?php endforeach; ?>
<a href='#' onclick='document.getElementById("refresh").submit(); return false;'>refresh</a>
</form>
Note: This is a little longer than the other answer, but will not prompt to resend post data.
I want to show nine images in a table , three images in each row using PHP.
It mostly depends on how you have PHP representing the images. The HTML markup should look something like
<div class="img-table">
<img ...>
<img ...>
<img ...>
....
</div>
and the CSS:
.img-table {
width:1000px; /* or whatever works */
}
.img-table img {
width:30%;
height:auto;
display:inline;
margin: 10px 1.5%; /* spacing: 20px vertically, 5% horizontally */
}
or, if you want to use <table> tables:
<table class="img-table">
<tr>
<td><img ...></td>
<td><img ...></td>
<td><img ...></td>
</tr>
.....
</table>
If you have your image urls located in a array, like
$imgs = array('path/to/img1.jpg','path/to/img2.jpg',...);
then you can generate the HTML like this:
$index = 0;
$html = '<div class="img-table">';
for ($i=0; $i<3; $i++) {
for ($j=0; $j<3; $j++) {
$html .= '<img src="' . $imgs[$index++] . '">';
}
}
$html .= '</div>';
echo $html;
or for the table:
$index = 0;
$html = '<table class="img-table">';
for ($i=0; $i<3; $i++) {
$html .= '<tr>'
for ($j=0; $j<3; $j++) {
$html .= '<td>';
$html .= '<img src="' . $imgs[$index++] . '">';
$html .= '</td>';
}
}
$html .= '</table>';
echo $html;
Hope that helps.
Laying out column images in a table
<? $perrow=3; $n=0; ?>
<table>
<? foreach($images as $i): ?>
<? if($n == 0): print '<tr>'; endif; ?>
<td><img src="<?=$i?>"></td>
<? if(++$n >= $perrow): print '</tr>'; $n = 0; endif; ?>
<? endforeach; ?>
</table>
<?php
$images=ARRAY( a.gif","b.jpeg","c.jif","d.bmp");
?>
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<?php
for($i=0; $i< count($images); $i++) {
echo "<td>
<table width=100% border=0>
<tr><td>
<img src=Download/$images[$i] width=130 height=130 border=0></td></tr>
</table>
</td>";
if ( (($i+1) % 3) == 0 ) echo $newrow="</tr><tr>"; // change 6 to 8 and see
}
?>
</tr>
</table>