Collapse Table when nothing is generated - php

With this PHP code I generate a table. with content from the database. Now I want to collapse it if it didn't generate any content.
It's only a part of the table otherwise there would be to much code. The rest of the table is the same.
<table class="table">
<tr class="day">
<th>
<?php
$j = 0;
thisDayIs($weekDays[$j]);
echo "<br>";
echo $weekDays[$j];
$j++;
?>
</th>
<?php
$datum = $weekDays[$j];
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
<tr class="day">
<th>
<?php
thisDayIs($weekDays[$j]);
echo "<br>";
echo $weekDays[$j];
$j++;
?>
</th>
<?php
$datum = $weekDays[$j];
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
</table>
I've tried something with jquery but it didn't work at all.
jquery:
$('.day').click(function()
{
$(this).nextUntil('tr.day').slideToggle(1000);
});
I have 5 rows and no idea what to do i have been struggeling a lot with this and can't find a solution.
Thanks in advance!!

Related

Website interface on 2 screens

I'm making this website, and it's only gonna be opened on 2 screens at the same time. I'm going to generate a table with php with output from a database.
My goal is that if the table doesn't fit on the main screen it goes to the second screen.
Is this even possible?
I've tried to come up with something but couldn't figure anything out. I have tried for loops but it has to do with the style and html or maybe javascript or jquery?
If this isn't possible should I generate a second window with jquery?
Thanks in advance!
if (!empty($_POST['check_list']))
{
$idArr = $_POST['check_list'];
$id = $idArr[0];
$parameters = array(':medId'=>$id);
$sth = $pdo->prepare("SELECT naam FROM medewerkers WHERE medewerkerid = :medId");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
echo "<h2>" . $row['naam'] . "</h2>";
}
?>
<table class="table">
<tr>
<th>Donderdag <br> 31-05</th>
<?php
$datum = "2017-05-31";
$parameters = array(':date'=>$datum,
':aid'=>$id);
$sth = $pdo->prepare("SELECT DISTINCT opdrachten.KRITISCHE_DATUM, opdrachten.PLANNINGS_DATUM, opdrachten.OPDRACHTID, onderzoekactiviteiten.A_UITVOERDER
FROM opdrachten
INNER JOIN onderzoekactiviteiten
ON opdrachten.OPDRACHTID=onderzoekactiviteiten.OPDRACHTID
WHERE PLANNINGS_DATUM = :date
AND A_UITVOERDER = :aid");
$sth->execute($parameters);
while ($row = $sth->fetch())
{
$cutKritDate = substr($row['KRITISCHE_DATUM'], 0, 10);
$cutPlanDate = substr($row['PLANNINGS_DATUM'], 0, 10);
echo "<td>Krit= " . $cutKritDate . "<br>";
echo "Plan= " . $cutPlanDate . "<br>";
echo "Opdracht= " . $row['OPDRACHTID'] . "</td>";
}
?>
</tr>
<tr>
<th>Vrijdag <br> 02-06</th>
</tr>
<tr>
<th>Maandag <br> 03-06</th>
</tr>
<tr>
<th>Dinsdag <br> 04-06</th>
</tr>
<tr>
<th>Woensdag <br> 05-06</th>
</tr>
</table>
<?php
}
else
{
$check_list = NULL;
$id = NULL;
echo "U bent vergeten een medewerker aan te vinken.";
}
I have a form with names, if one is selected it generates a table. this is how I generate the table. The goal is that the tables are below each other. But when it doesn't fit anymore (when you can scroll) the table needs to go to the second screen.

creating table rowspan php

i need help with my code..how do i have output like in the image below where it will automatically insert rowspan ..my current code
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");
echo "<br /><br /><table class='table table-bordered table-striped'>
<tr>
<th>Bil</th>
<th>Department</th>
<th>Staff</th>
</tr>";
$count =1 ;
while($row = $cmsDB->fetchArray($result))
{
$deptid=$row['deptid'];
$deptname=$row['deptname'];
echo "<tr><td>".$count++."</td><td>".$deptname."</td><td>";
global $cmsDB;
$result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
while($row = $cmsDB->fetchArray($result2))
{
$name=$row['name'];
echo "".$name." <br />";
}
}
echo "</td></tr></table>";
current output and desired results
Please Note: By design performing two queries for this just a bad idea. Still going with that. All you need to do is to perform the second query ahead of creating the block. Record the counts of the result Data and use that as rowSpan. Quite Simple.
global $myDB;
$result2 = $myDB->query("SELECT * FROM ".$myDB->prefix("staff")." WHERE deptid=$deptid");
$rowSpan=$result2-num_rows;
echo "<tr>"
echo "<td rowspan=" . $rowSpan . ">" . $count++ . "</td>";
echo "<td rowspan=" . $rowSpan . ">".$deptname."</td>";
The second while loop must output
<td>$name</td></tr>
The important thing is to close the table row inside the while loop also start a new table row in case you are printing a second name for the same dept.
$result = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("departments")."");
echo "<br /><br /><table class='table table-bordered table-striped'>
<tr>
<th>Bil</th>
<th>Department</th>
<th>Staff</th>
</tr>";
$count =1 ;
while($row = $cmsDB->fetchArray($result))
{
$deptid=$row['deptid'];
$deptname=$row['deptname'];
global $cmsDB;
$result2 = $cmsDB->query("SELECT * FROM ".$cmsDB->prefix("staff")." WHERE deptid=$deptid");
$num_rows = $cmsDB->getRowsNum($result2);
$rowSpan=$num_rows;
echo "<tr>";
echo "<td rowspan=" . $rowSpan . ">" . $count++ . "</td>";
echo "<td rowspan=" . $rowSpan . ">".$deptname."</td>";
while($row = $cmsDB->fetchArray($result2))
{
$name=$row['name'];
echo "<td>$name</td></tr>";
}
}
echo "</table>";
I used this logic and it worked perfectly. You can try this.
$result = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],null);
$order_count=0;
$array_ord=array();
while ($myrow = mysql_fetch_array($result))
{
$result23 = get_pending_sales_order_details(ST_SALESORDER, $_POST['customer_id'],$myrow["order_no"]);
$row_span = mysql_num_rows($result23);
start_row();
if($currentorg != $myrow["order_no"])
{
label_cell($myrow["order_no"], "rowspan='".$row_span."' align=center");
}
label_cell($myrow["stk_code"], "align=center");
label_cell($myrow["description"], "align=left");
label_cell($myrow["pending_quantity"], "align=center");
end_row();
$currentorg = $myrow["order_no"];
}

php - how to exclude a line of code in a loop

I have a code in HTML in a table. And I want the loop to just ignore them
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
</thead> // This two line of code
<tbody> // is the one I want to exclude in the while loop
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
Is this even possible?
You need to end your first loop, spit out the html and then start the loop again, havent tested but i think the below should now work.
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin)) {
echo "<th>" . $row['a'] . "</th>";
}
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while ($row2 = mysql_fetch_array($rs_admin2)) {
echo " <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time'] . "</td>";
echo "</tr>";
}
?>
I'm guessing you want those lines printed once, not to be outside the loop, per se. You could use a variable to track it:
$linesNeeded = true;
while (...) {
...
if ($linesNeeded) {
echo $line1;
echo $line2;
$linesNeeded = false;
}
...
}
Please use mysqli instead of mysql. Take a look: MySQL vs MySQLi when using PHP
+
Your problem's answer too.
<?php
$sel_admin = "query ";
$rs_admin = mysqli_query($connection,$sel_admin);
while($row = mysqli_fetch_array($rs_admin))
{
echo "<th>". $row['a']. "</th>";
?>
</thead>
<tbody>
<?php
$sel_admin2 = "query2 ";
$rs_admin2 = mysqli_query($connection, $sel_admin2);
while($row2 = mysqli_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>
This is honestly just a guess but based on the code you provided you actually need to add more code after remove the code you do not want:
<?php
$sel_admin = "query ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{
echo "<tr><th>". $row['a']. "</th></tr>"; // Notice the <tr></tr>
$sel_admin2 = "query2 ";
$rs_admin2 = mysql_query($sel_admin2);
while($row2 = mysql_fetch_array($rs_admin2))
{
echo" <tr class='gradeX'> ";
echo "<td>" . $row2['sched3_time']. "</td>";
echo"</tr>";
}
}
?>

Insert items from MySql through PHP to HTML Bootstrap page

I'm trying to match results from this PHP code into this Bootstrap HTML structure.
What I'm able to achieve so far is printing items from database in 3-column layout, but I don't known how to combine this results with/ info my bootstrap html "structure/ table". I think I need to work with "id" and increment it after every "echo" (h4)? But I'm very new to PHP. Thank you for any practical advices.
PHP CODE:
// Run query on connection
$query = "SELECT
product.id,
product.name as product_name,
product.price,
product.type
FROM pizza_project.product
INNER JOIN pizza_project.product_type on product.type = product_type.id
where product.type in (1,2)
order by product.type ASC, product.price ASC";
$result = $con->query($query);
?>
<table>
<tr>
<?php
$split = 0;
$id = $row['id']; // just an idea I think I need to start with
if (mysqli_num_rows($result) > 0) {
foreach ($result as $row) {
echo '<td>' . $row['product_name'] . ' ' . $row['id'] . '</td>';
$split++;
if ($split%3 == 0) {
echo '</tr> </tr>';
}
}
}
?>
</tr>
</table>
<?php
$itemsInRow = 0;
foreach ($result as $row) {
if ($itemsInRow === 0)
echo "<div class=\"row text-center\">";
echo "<div class=\"col-md-4\">";
echo "<span class=\"fa-stack fa-4x\">";
echo "<img class=\"product-image\" src=\"" . $row['img'] . "\">";
echo "</span>";
echo "<h4 class=\"service-heading\">" . $row['headline'] . "</h4>";
echo "<p class=\"text-muted\">" . $row['text'] . "</p>";
echo "</div>";
if ($itemsInRow === 0)
echo "</div>";
if ($itemsInRow % 3 === 0) {
$itemsInRow = 0;
continue;
}
$itemsInRow++;
}
Is this what you are trying to achieve? I will improve my answers if its the right way. This is not tested.

Display SQLITE output in column rather than row

At the moment I have the below script which auto generates the table names and row data automatically by looking at a sqlite table. So regardless of if you have 2 or 10 columns this script works.
At the moment the script outputs the results like this:
Output currently appears as a Row
I have tried altering the script so that it outputs the results like below. Can someone assist or guide me in the right direction to achieve this?
Is it possible to output the results of the query in the below format: going down in a column rather than across as a row ?
Output should appear as a Column
<?
$ED = $_GET['ED'];
$ID = $_GET['ID'];
$table_name = $_GET['table'];
?>
<table border="1">
<tr>
<td>
<table>
<?php // Display all sqlite column names for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
if ($table['name'] == "ID") {
echo "<tr><td>" . $table['name'] . "</td></tr>";
} else {
$table_name_header = ucwords(strtolower(str_replace('_', ' ', $table['name'])));
echo "<tr><td>" . $table_name_header . "</td></tr>";
}
}
?>
</table>
</td>
<td>
<table>
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
</table>
</td>
</tr>
</table>
Modifying the code to the below by putting the data into an combined array and then pulling it back via a loop it will display as required:
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
$column_data[] = $row[$col];
// echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
<?
$array = $columns;
$array2 = $column_data;
$result = array_combine($array, $array2);
//print_r($result);
?><br><br>
<center>
<table border="0" cellpadding="2" cellspacing="2" color="#4B708D">
<thead>
<?
foreach($result as $key => $value) {
echo "<tr><td bgcolor='#c6d5e1'>$key</td><td bgcolor='#FFFFFF'>$value</td></tr>";
}
?>
</thead>
</table>

Categories