creating table rowspan php - 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"];
}

Related

Find % according to values in one column (Need to add iteration loop) in php

I want to update 3rd column according to values based in 2nd column. Right to now I could print % value only for 1st team correctly. Other teams have wrong values.
Any help please.
echo "<table border="."1"."align="."right".">
<tr>
<th align="."center".">ID</th>
<th>Internal Team Names</th>
<th width="."10px"." >Passed % for aw34_0530_tc1015</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['TeamInternalNames'] . "</td>";
$TotalTestsForTeam = mysqli_query($conn ,"SELECT * from aw34_0530_tc1015 where Application="."'".$application_l."'"); //rows for ace application
$TotalTestsForTeamCount = $TotalTestsForTeam->num_rows; //rows for ace application
$PassedTestsForTeam_query= "SELECT * from aw34_0530_tc1015 where Result='passed' AND Application='".$row['TeamInternalNames']."'"; //ace passed count
$passed = mysqli_query($conn ,$PassedTestsForTeam_query);
$TotalPaassedTestsForTeam = $passed->num_rows; //rows for ace application
//echo "Count of passed ace in aw34_0530_tc1015 = TotalPaassedTestsForTeam Results "; //rows for ace application
$passed_percentForTEam = ($TotalPaassedTestsForTeam/$TotalTestsForTeamCount)*100 | 0;
echo "<td>" . $passed_percentForTEam . "</td>";
// if ($row['Result'] == "passed") {
// echo "<td style="."background-color:#33FF38".">P</td>";
// } else {
// echo "<td style="."background-color:#FF334C".">F</td>";
// }
echo "</tr>";
}
echo "</table>";

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>";
}
}
?>

Writing the attributes of a database in PHP

I am writing an application in which user can enter a database name and I should write all of its contents in table with using PHP.I can do it when I know the name of database with the following code.
$result = mysqli_query($con,"SELECT * FROM course");
echo "<table border='1'>
<tr>
<th>blablabla</th>
<th>blabla</th>
<th>blablabla</th>
<th>bla</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['blabla'] . "</td>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['bla'] . "</td>";
echo "</tr>";
}
echo "</table>";
In this example I can show it since I know the name of table is course and it has 4 attributes.But I want to be able to show the result regardless of the name the user entered.So if user wants to view the contents of instructors there should be two columns instead of 4.How can I accomplish this.I get the table name with html.
Table:<input type="text" name="table">
Edit:Denis's answer and GrumpyCroutons' answer are both correct.You can also ask me if you didnt understand something in their solution.
Quickly wrote this up, commented it (This way you can easily learn what's going on, you see), and tested it for you.
<form method="GET">
<input type="text" name="table">
</form>
<?php
//can be done elsewhere, I used this for testing. vv
$config = array(
'SQL-Host' => '',
'SQL-User' => '',
'SQL-Pass' => '',
'SQL-Database' => ''
);
$con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con));
//can be done elsewhere, I used this for testing. ^^
if(!isSet($_GET['table'])) { //check if table choser form was submitted.
//In my case, do nothing, but you could display a message saying something like no db chosen etc.
} else {
$table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection.
$sql = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data
$sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns
$result = mysqli_query($con, $sql);
$Headers = mysqli_query($con, $sql2);
//you could do more checks here to see if anything was returned, and display an error if not or whatever.
echo "<table border='1'>";
echo "<tr>"; //all in one row
$headersList = array(); //create an empty array
while($row = mysqli_fetch_array($Headers)) { //loop through table columns
echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's.
array_push($headersList, $row['Field']); //Fill array with fields
} //$row = mysqli_fetch_array($Headers)
echo "</tr>";
$amt = count($headersList); // How many headers are there?
while($row = mysqli_fetch_array($result)) {
echo "<tr>"; //each row gets its own tr
for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad
echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data
} //$x = 1; $x < $amt; $x++
echo "</tr>";
} //$row = mysqli_fetch_array($result)
echo "</table>";
}
?>
$tablename = $_POST['table'];
$result = mysqli_query($con,"SELECT * FROM $tablename");
$first = true;
while($row = mysqli_fetch_assoc($result))
{
if ($first)
{
$columns = array_keys($row);
echo "<table border='1'>
<tr>";
foreach ($columns as $c)
{
echo "<th>$c</th>";
}
echo "</tr>";
$first = false;
}
echo "<tr>";
foreach ($row as $v)
{
echo "<td>$v</td>";
}
echo "</tr>";
}
echo "</table>";
<?php
$table_name = do_not_inject($_REQUEST['table_name']);
$result = mysqli_query($con,'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='. $table_name);
?>
<table>
<?php
$columns = array();
while ($row = mysql_fetch_assoc($result)){
$columns[]=$row['COLUMN_NAME'];
?>
<tr><th><?php echo $row['COLUMN_NAME']; ?></th></tr>
<?php
}
$result = mysqli_query($con,'SELECT * FROM course'. $table_name);
while($row = mysqli_fetch_assoc($result)){
echo '<tr>';
foreach ($columns as $column){
?>
<td><?php echo $row[$column]; ?></td>
<?php
}
echo '</tr>';
}
?>
</table>

Similar Queries different results, why?

This one is a bit perplexing since it works perfectly on some records, and not on others. I am trying to pull all records with a specific email and only getting one record in about half of the cases. I have two query sets one pulling the data one is pulling it half of the time.
The first that works all the time is:
$tiera = mysql_query("SELECT SUM(datamb) AS tiera FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tiera1 = mysql_fetch_assoc($tiera);
$tiera2 = $tiera1['tiera'];
$tieradata = mysql_query("SELECT SUM(dataplan) as datatotal FROM maindata2 WHERE dataplan = '2' and email='".mysql_real_escape_string($_POST['email'])."'");
$tieradata1 = mysql_fetch_assoc($tieradata);
$tieradata2 = $tieradata1['datatotal'];
echo "<table id='display1'>
<tr>
<th>Tier:</th>
<th>Total Data in Tier:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
</tr>";
echo "<tr>";
echo "<td>A</td> ";
echo "<td>". $tieradata2 . " MB</td> ";
echo "<td>". $tiera2. " MB</td> ";
echo "<td>".($tieradata1['datatotal'] - ROUND ($tiera1['tiera'],2)) . "MB</td></font> ";
echo "</table>";
I cant use this for my second query because I need it in the loop like this:
$sql = "SELECT phonenumber,date,email, dataplan AS currentplan, SUM(datamb) AS value_sum FROM maindata2 WHERE email='".mysql_real_escape_string($_POST['email'])."' GROUP BY dataplan, phonenumber ";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No Data Found For This Email";
exit;
}
$row = mysql_fetch_assoc($result);
$query = mysql_query($sql) or die(mysql_error());
$header_printed = false;
while($row = mysql_fetch_array($query)) {
if ($row['phonenumber']) {
if ($header_printed === false) {
echo "
<table id='display'>
<tr>
<th>Phone Number:</th>
<th>Data Plan:</th>
<th>Data Usage This Period:</th>
<th>Remaining:</th>
<th>Date Reporting:</th>
</tr>";
$header_printed = true;
}
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "MB</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td><font color=$color>".($row['currentplan'] - ROUND ($row["value_sum"],2)) . "MB</td></font> ";
echo "<td>".$row['date'] . "</td></tr>";
}
}
echo "</table>";
So the question is, what is missing from the second query that is in the first?

Display result from database in two columns

EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

Categories