table within php not displayed as wished - php

I am attempting to put the first column of the table to be "control" names, which are fetched from the database.
I want columns 2 and 3 to be fetched from a second database with times & penalty letters.
I am able to display all the data on the screen, but not able to get this in the correct columns in the table.
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
foreach ($all_cols as $col => $value)
{
echo '
<tr>
<th><a href="#" id="'.$col.'" data-type="text" data-pk="'.$t["id"].'" data-title="t1" class="edit editable-click" >'.$t[$col].'</a></th>
';
}}
$counter = 1;
while($row = $results->fetch_assoc()) {
for ($i=1; $i <= 20; $i++) {
echo '
<th><a href="#" id="t1'.$i.'" data-type="text" data-pk="'.$row["id"].'" data-title="Enter t1'.$i.'" class="editable editable-click" >'.$row["t1".$i].'</a></th>
<th><a href="#" id="t1'.$i.'" data-type="text" data-pk="'.$row["id"].'" data-title="Enter t1'.$i.'" class="editable editable-click" >'.$row["t1".$i].'</a></th>
</tr>
';
}
$counter++; //increment count by 1
}
I have tried moving the code around but unable to get the desired output.

Related

PHP table, to add, remove and edit rows

I have issues with my table, I cannot use Javascript (which I know would be easier). I can only use HTML, PHP and CSS. This is my code that I currently have. The issues I need to resolve are the following:
I am able to add rows, delete and I am so also able to edit them by using the "contenteditable", however my issue is that every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Also if there is a way to have an edit button instead of my conteneditable method.
Here is my code:
input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}
if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Without Javascript you can't.
if there is a way to have an edit button instead of my conteneditable method
It's rather hard to suggest what your code should be since you don't ofer a description of what the code is intended to do, other than the code which doesn't do what you want. I think you mean something like:
<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
#print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";

Generate dropdown lists which will send the selectted item back to server

This code allows somebody to pick a class from a drop down menu, in which I convert numbers to alphabet letters. Now I want to send the selected value back to the server:
<table id="example" class="display table" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th>Code</th>
<th>Name</th>
<th>Hours</th>
<th>Class</th>
<th>Add</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Code</th>
<th>Name</th>
<th>Hours</th>
<th>Class</th>
<th>Add</th>
</tr>
</tfoot>
<tbody>
<?php
$query = "SELECT * FROM class";
$result = mysqli_query($connection,$query) or die ("Couldn’t execute query.");
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>
<td>$row[code]</td>
<td>$row[name]</td>
<td>$row[hours]</td>";
$query1 = "SELECT total FROM classtot where code='$row[code]'";
$result1 = mysqli_query($connection,$query1);
while ($row=mysqli_fetch_assoc($result1))
{
$a=$row['total'];
}
$alphabet = range('A','Z');
$i = 1;
echo "
<td><select id='selectError' data-rel='chosen' name='class'>";
while ($i<=$a)
{
$kls=$alphabet[$i-1];
echo "<option value=$kls> $kls </option>";
$i=$i+1;
}
echo "</select></td>
<td>
<a class='btn btn-primary btn-addon m-b-sm btn-xs' href='home_member.php?page=add&id=$row[code]'>
<i class='fa fa-plus'></i> Add</a>
</td>
</tr>";
}
?>
</tbody>
</table>
How can I pass the slected value from the drop down menu 'class' to the server? I can pass the code, but don't know how to pass the selected class.
To add the chosen class to the URL, you will need to build the URL in JavaScript, because PHP does not know beforehand what the user will choose. Here is your code (only the part that goes through the SQL. In comments I describe several improvements I made (unrelated to your question):
<?php
// Make your query return ALL you need. Avoid second query:
$query = "SELECT class.code, class.name, class.hours, classtot.total
FROM class
INNER JOIN classtot ON classtot.code = class.code";
$result = mysqli_query($connection,$query) or die ("Couldn’t execute query.");
// Keep track of row number, for use in generating unique id property values
$rowIndex = 0;
while($row = mysqli_fetch_assoc($result))
{
$rowIndex++;
echo "<tr>
<td>$row[code]</td>
<td>$row[name]</td>
<td>$row[hours]</td>";
// Just pick the total from the combined query:
$a = $row['total'];
$alphabet = range('A','Z');
// Change id for select: You cannot assign the same id to several elements
echo "
<td><select id='select$rowIndex' data-rel='chosen' name='class' >";
// Use a zero-based for loop instead of a while
for ($i = 0; $i < $a; $i++)
{
// Reference $i now, not $i-1:
$kls = $alphabet[$i];
echo "<option value='$kls'> $kls </option>";
}
// Instead of hard-coding the URL, call a JS function that will make the url
echo "</select></td>
<td>
<a class='btn btn-primary btn-addon m-b-sm btn-xs' href='#'
onclick='gotoClass($rowIndex, \"$row[code]\")'>
<i class='fa fa-plus'></i> Add</a>
</td>
</tr>";
}
?>
</tbody>
</table>
<script>
// The JS function that builds the URL and triggers the navigation to it
function gotoClass(rowIndex, classCode) {
var sel = document.getElementById('select' + rowIndex);
location.href = 'home_member.php?page=add&id=' + classCode
+ '&something=' + sel.value;
return false;
}
</script>
In the above code, you need to change "something" to the correct URL parameter you want to use to pass the selected "alphabet" value.
If you have another select in the table rows, like this:
<select id='select2$rowIndex' data-rel='chosen' name='code' >
then extend the above javascript function as follows:
function gotoClass(rowIndex, classCode) {
var sel = document.getElementById('select' + rowIndex);
var sel2 = document.getElementById('select2' + rowIndex);
location.href = 'home_member.php?page=add&id=' + classCode
+ '&something=' + sel.value
+ '&othervalue=' + sel2.value;
return false;
}

php +1 per set of content

I'm creating a series of content blocks. (4 in total).
It's stupid to create all for of them if you can let php create them for you.
So I created the first one and wrote a for loop that adds the content as long as it haven't a max number of 4.
Here it is:
<table>
<tr>
<?php
$counter =0;
for ($i=0; $i <= 3; $i++){
$counter++;
?>
<td>some content to repeat with id='$id++' and '$id++'</td>
<?php
if ($counter == 2){
echo '</tr><tr>';
$counter=0;
}
}
?>
</tr>
</table>
What this does is repeat the <td> two times and then end the rule and start a new one. Do this until you reach an amount of 4 (3 because 1=0)
The problem I have is that some of the content contains id's
The id's should also number up but only if the complete loop is repeated.
So $i++ should both have the same value if the loop runs once
So that would mean that if there are two id's in the first run they should both have the id1 and the next repeat should have id2
I have no idea how to do this.
The output should be:
<table>
<tr>
<td>
This content has id 1
And this content has id 1 to
<td>
<td>
This content has id 2
And this content has id 2 to
<td>
</tr>
<tr>
<td>
This content has id 3
And this content has id 3 to
<td>
<td>
This content has id 4
And this content has id 4 to
<td>
</tr>
</table>
M.
How about this
<table>
<?php
$id = 1
for ($i=0; $i < 2; $i++){
echo '<tr>';
for ($j=0; $j < 2; $j++){
echo 'This content has id $id';
echo' And this content has id $id too';
}
$id += 1;
echo '</tr>';
}
}
?>
</table>
Update: added an id counter.
Is this what you mean? You don't need $counter as you have $i keeping count for you.
<table>
<tr>
<?php for ($i=0; $i <= 3; $i++){ ?>
<td<?php
if ($i < 2) echo ' id="1"';
else echo ' id="2"';
?>>some content to repeat</td>
<?php if ($i == 1){ echo '</tr><tr>'; } ?>
<?php } ?>
</tr>
</table>

How to categorized results from SQL, display 6 columns and set colorize on every category

I would like to ask for help on how to achieve this. I have this code below that pull records from DB and display it in 6 columns.
What I want to achieve is that, I want to display results on 6 columns but I want to categorize and set different color on every category.
let say i want to display the whole set of fruits starting from letter A with 6 columns with colors of gray, then below all letters starting with B with 6 columns with white color on background then below is C with gray colors in 6 columns. thanks.
<?php
fruits = $stmt->prepare("SELECT * FROM fruits ORDER by fruit_id ASC");
$fruits->execute();
$cols = 6;
do {
echo "<tr>";
for ($i = 1; $i <= $cols; $i++) {
$row = $fruits->fetch(PDO::FETCH_ASSOC);
if ($row) {
$fruit_id = $row['fruit_id'];
$fruit_name = $row['fruit_name'];
?>
<td>
<table>
<tr valign="top">
<td>
<?php echo '<input type="checkbox" id="fruit_id[]" name="fruit_id[]" value="' . $fruit_id . '"/>' . $fruit_name . "\n"; ?>
</td>
<td width="30"> </td>
</tr>
</table>
</td>
<?php
} else {
echo "<td> </td>";
}
}
} while ($row);
?>
Do it in the database:
SELECT fruits.*,if(#evenodd>0,'grey','white') AS color, #evenodd:=-1*evenodd AS dummy FROM (#evenodd:=1) as init, fruits ORDER by fruit_id ASC

Mysql fetch array, table results 2

I've recently posted a similar question but that was to create one row of cells across and after it reaches 3 columns, it creates a new row, so I can have 3 columns of infinite rows. That was solved.
Now what I need is this (GIVEN A and B should be records from the database using $row = mysql_fetch_array($results) so basically it would be something like $row['username']; for A and B ).
<tr>
<td><img src="images/ava/A.png" /></td>
<td>A</td>
<td width="2px" rowspan="3"></td>
<td><img src="images/ava/B.png" /></td>
<td>B</td>
</tr>
<tr>
<td><div class="gauge"><div class="innergauge"></div>A</div></td>
<td><div class="gauge"><div class="innergauge"></div>B</div></td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
As what you can see, the record I got from my database has to fill in to this format, where A is one record and B is another, then if there is record C, this whole thing should repeat again to form a second row.
Build each section as you go, combine them at the end. Here's how you do sections 1 and 3, the middle section is left as an exercise:
$section1 = '<tr>';
$section2 = // start $section2
$section3 = '<tr>';
$i = 0;
while($row = mysql_fetch_array(...)){
if($i > 0){
// won't happen the first time through the loop
$section1.= '<td width="2px" rowspan="3"></td>';
}
$section1 .= '<td><img src="images/ava/'.$row['username'].'"/></td>';
$section1 .= '<td>'.$row['username'].'</td>';
// now do $section2
$section3 .= '<td>'.$row['username'].'</td>';
$i++;
}
$section1 .= '</tr>';
// finish $section 2
$section3 .= '</tr>';
// now output
echo $section1 . $section2 . $section3;

Categories