Data in table not displaying the way I want it to - php

I have a spinner and what happens is that whatever number is in the spinner, when the form is submitted, it should display the word "quest" as many times as the number in the spinner.. E.g if number in spinner is 3, then it will display "quest" 3 times in the table.
The problem is displaying it in the table.
At the moment with my current code it is displaying it like this:
quest
quest
quest
Question Id, Option Type, Duration .... These are table headings
It is displaying the words quest outside the table
Instead I want the word "quest" to be displayed in the Question Id column like this:
Question Id, Option Type, Duration...
quest
quest
quest
How can I get it to display it like the example above?
Below is code
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
echo "<tr>quest";
}
}
?>
<td class='qid'></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
</table>
I did try echo "<td class='qid'></td>"; but this completely failed as well

Try this:
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
?>
<tr>
<td class='qid'><?php echo $quest; ?></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
} // For
} // If
?>
</table>

Is this what you want to do? Display "quest" in the first column?
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) { ?>
<tr>
<td class='qid'>quest</td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
}
}
?></table>
?>

Related

How to hide table row?

I am trying to hide the table row for user_id. I only want to show the full name, user name, and actions in the table. I tried to remove the code for the user id row, but it affected the other functionalities like the edit and delete.
here is the code:
<form method="post" id="editForm">
<table class="table">
<tbody>
<tr>
<th scope="col">USER ID</th>
<th scope="col">FULL NAME</th>
<th scope="col">USER NAME</th>
<th scope="col">ACTIONS</th>
</tr>
<?php $a = 0?>
<?php while ($row = mysqli_fetch_array($res)) {
echo "<tr id=".$a++.">
<th scope='row' class='row-data'>".$row['user_id']."</th>
<td class='row-data'>".$row['full_name']."</td>
<td class='row-data'>".$row['user_name']."</td>
<td><input type='button'
value='EDIT'
onclick='editUser()'' /></td>
<td><input type='button'
value='DELETE'
onclick='deleteUser()' /></td>
</tr>
</tbody>";
}; ?>
You could try modifying the editUser() and deleteUser() Javascript methods to accept a userId parameter. It would probably clean up some of the code as well on the JS side. Just a suggestion though. That snippet then might look something like this:
<form method="post" id="editForm">
<table class="table">
<tbody>
<tr>
<th scope="col">FULL NAME</th>
<th scope="col">USER NAME</th>
<th scope="col">ACTIONS</th>
</tr>
<?php $a = 0?>
<?php while ($row = mysqli_fetch_array($res)) {
echo "<tr id='".$a++."'>
<td class='row-data'>".$row['full_name']."</td>
<td class='row-data'>".$row['user_name']."</td>
<td><input type='button'
value='EDIT'
onclick='editUser(".$row['user_id'].")'' /></td>
<td><input type='button'
value='DELETE'
onclick='deleteUser(".$row['user_id'].")' /></td>
</tr>
</tbody>";
}; ?>
Otherwise you could use Ken's solution which would probably work with your existing code.
Instead of removing the column, you can set the display style to 'none' for this column
(add style="display:none;")
Hence (1) change
<th scope="col">USER ID</th>
to
<th scope="col" style="display:none;">USER ID</th>
and (2) change
<th scope='row' class='row-data'>".$row['user_id']."</th>
to
<th scope='row' class='row-data' style="display:none;">".$row['user_id']."</th>

Display fields in one row + PHP

Good Day, I want to display the value in my DB in one row. But what is happening now, it is being displayed horizontally
Image Sample
<table class="table table-bordered">
<thead style="font-size:20px;text-align:center;"class="thead-dark">
<tr>
<th class="tblHeader">MODEL</th>
<th class="tblHeader">PERIOD 1</th>
<th class="tblHeader">PERIOD 2</th>
<th class="tblHeader">PERIOD 3</th>
<th class="tblHeader">PERIOD 4</th>
<th class="tblHeader">PERIOD 5</th>
<th class="tblHeader">PERIOD 6</th>
<th class="tblHeader">PERIOD 7</th>
<th class="tblHeader">PERIOD 8</th>
<th class="tblHeader">PERIOD 9</th>
<th class="tblHeader">PERIOD 10</th>
<th class="tblHeader">PERIOD 11</th>
<th class="tblHeader">PERIOD 12</th>
</tr>
</thead>
<?php
$connect = mysqli_connect("localhost", "root", "", "hh_bpm");
$query = "SELECT *
FROM bpm_periods_instance
WHERE Category_Name=1
";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["Text_Value"];?></td>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
I want to display the string value in line with Period 1 - 12`
I'm still trying to learn.
First of all, move your while loop just before the <tr>, for you want to have just one table body. Then it's better to have the same number of <th>s and <td>s. I see you have 12 <th>s, so make 12 <td>s in each <tr> (leave them empty if you want, but include them)
The tags is a row containing headings for your table. For your to Match your column names you must have equal number of as th in your table.
<tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr>
You can now iterate this for your rows.
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
You are showing just a singe TD for each row.
What you are missing is a row loop:
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<?php
foreach($row as $k=>$v)
echo "<td>$v</td>";
?>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
Alternatively, you should output TDs for all columns manually:
<tr>
<td><?php echo $row["Column1"];?></td>
<td><?php echo $row["Column2"];?></td>
<td><?php echo $row["Column3"];?></td>
<td><?php echo $row["Column4"];?></td>
<td><?php echo $row["Column5"];?></td>
<td><?php echo $row["Column6"];?></td>
...
</tr>
You need a crosstab query to achieve the results that you want. Since there is very little information I'll just give and outline
SELECT info,
sum( if(MONTH(dt)=1,1,0) as Period_1,
sum( if(MONTH(dt)=2,1,0) as Period_2,
//--- repeat for all 12 months
Your query will return 13 columns (info and Period_1 to Period_12). You'll need to adjust the html to cater for this output.

How to create an html link to another page from "<?=$objResult["id"];?>"

I'm working on a php/mysql application and need to make the output of one column, one row an html link to another html page. Have not been able to find any relavant information. The "" needs to be the link. Thanks for any help.
<table id="display" style="width:800px;">
<tr>
<th width="40">ID</th>
<th width="70">Last</th>
<th width="70">First</th>
<th width="10">Middle</th>
<th width="70">Birth</th>
<th width="70">Death</th>
<th width="170">Notes</th>
<th width="100">Cemetery</th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td style='text-align:center;'><?=$objResult["id"];?></td>
<td style='text-align:center;'><?=$objResult["last"];?></td>
<td style='text-align:center;'><?=$objResult["first"];?></td>
<td style='text-align:center;'><?=$objResult["middle"];?></td>
<td style='text-align:center;'><?=$objResult["birth"];?></td>
<td style='text-align:center;'><?=$objResult["death"];?></td>
<td><?=$objResult["notes"];?></td>
<td style='text-align:center;'><?=$objResult["cemetery"];?></td>
</tr>
<?
}
?>
</table>

Running to Selects and grouping results with PHP

I am trying to display a row of data from my main table (parents details) and then under each of those rows display the children's names from another table. I have had a go using the following code:
$sql="select distinct members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_main.contact_id=members_family.contact_id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
echo '<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>';
echo '<tr>
<td valign="top">'.$rows['main_firstname'].'</td>
<td valign="top">'.$rows['main_lastname'].'</td>
<td valign="top">'.$rows['main_address_1'].'</td>
<td valign="top">'.$rows['main_address_2'].'</td>
<td valign="top">'.$rows['main_address_3'].'</td>
<td valign="top">'.$rows['main_address_4'].'</td>
<td valign="top">'.$rows['main_post_code'].'</td>
<td valign="top">'.$rows['main_home_tel'].'</td>
<td valign="top">'.$rows['main_mobile'].'</td>
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';
$result2=mysql_query($sql2);
echo '<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>';
while($rows=mysql_fetch_array($result2)){
echo '<tr>
<td valign="top">'.$rows['fam_firstname'].'</td>
<td valign="top">'.$rows['fam_lastname'].'</td>
</tr>';
}
}
echo "</table>";
I am getting all rows from the members_family table per each record from the members_main. There can be more than one record in the members_family which will be associated to one record in the members_main. Bascially, members_main (parent) can have multiple children in the members_main.
I think you just need to retrieve your members_main.contact_id from first table to use later in second while loop. Then you will have to move your second query inside first query while loop so the code will look like that:
$sql="select distinct
members_main.contact_id as ID,
members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result)) {
//script goes on with table printing untill
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';
Now it's time for the query
$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_main.contact_id=members_family.contact_id and members_main.contact_id = '".$rows['ID']."'";
As you see i added in WHERE condition to limit result for specified ID wich i retrieved from first query. Now you can print the rest of the table. Oh and by the way you closing table should look like that
echo "</table></td></tr></table>";
This is because you opened two tables.
Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO
I feel some confusion with your second query. I am thinking that you have to construct your second query with the contact_id which is coming from first query(members_main). I assumes in that way and i have coded below. May be it will help you
<?php
$sql = "select distinct members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$result = mysql_query($sql);
?>
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
{
$conId = $rows['contact_id']; //Which is comming from first Query
$sql2 = "select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_family.contact_id=$conId";
$result2 = mysql_query($sql2);
?>
<tr>
<td valign="top"><?= $rows['main_firstname']; ?></td>
<td valign="top"><?= $rows['main_lastname']; ?></td>
<td valign="top"><?= $rows['main_address_1']; ?></td>
<td valign="top"><?= $rows['main_address_2']; ?></td>
<td valign="top"><?= $rows['main_address_3']; ?></td>
<td valign="top"><?= $rows['main_address_4']; ?></td>
<td valign="top"><?= $rows['main_post_code']; ?></td>
<td valign="top"><?= $rows['main_home_tel']; ?></td>
<td valign="top"><?= $rows['main_mobile']; ?></td>
<td valign="top"><?= $rows['main_home_email']; ?></td>
</tr>
<tr>
<td colspan="10">
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>
<?php
while ($rowsFamily = mysql_fetch_array($result2))
{
?>
<tr>
<td valign="top"><?= $rowsFamily['fam_firstname']; ?></td>
<td valign="top"><?= $rowsFamily['fam_lastname']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>

Tablesorter plugin not sorting my tables when data comes from database

I have the following code:
<table id="box-table-a" class="tablesorter">
<thead>
<tr>
<th scope="col">B-House/Dorm Name</th>
<th scope="col">Address</th>
<th scope="col">Price Range</th>
<th scope="col">Date Added</th>
<th scope="col">Status</th>
</tr>
</thead>
<?php
$q=mysql_query("select * from property");
while( $f=mysql_fetch_array($q, MYSQL_ASSOC))
{ $p_id=$f["p_id"];
echo"
<tbody>
<tr>
<td onblurr='hover2()' onmouseover='hover(".$p_id.")' onclick='showUser(".$p_id.")'>
<span style='cursor:pointer'>".$f['p_name']."</span></td>
<td id='pretty'>".$f['address']."</td>
<td>".$f['p_name']."</td> <td>".$f['payment_type']."</td> <td>".$status."</td> </tr>
</tbody>
";
}
?>
</table>
Any idea what may be wrong here?
Don't add <tbody></tbody> to every loop in the while! Tablesorter is very sensitive.
You did'nt sort your DB :
$q=mysql_query("select * from property ORDER BY p_name");

Categories