How do I change it into table form with three headings which are: Company name (ABC and XYZ), Branch (Kuching and Sibu for both companies respectively), and Staff name (for both companies according to the array). I'm a beginner. Please help as I'm stuck. Something's wrong with my code.
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table>
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
foreach($v_company as $v_company_name=>$v_company_info){
echo "<tr>";
echo "<td>";
echo "$v_company_name <br>";
echo "</td>";
echo "</tr>";
foreach($v_company_info as $v_branch=>$v_staffs){
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
foreach($v_staffs as $v_staff){
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>";
}
}
}
?>
</table>
</body>
</html>
You can use this code
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>
array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table>
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
$company = '';
$branch = '';
$staff = '';
foreach($v_company as $v_company_name=>$v_company_info){
foreach($v_company_info as $v_branch=>$v_staffs){
foreach($v_staffs as $v_staff){
echo "<tr>";
echo "<td>";
if($company == '' || $company != $v_company_name){
$company = $v_company_name;
echo "$v_company_name <br>";
}
echo "</td>";
echo "<td>";
if($branch != $v_branch){
$branch = $v_branch;
echo "$v_branch <br>";
}
echo "</td>";
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>
You can copy paste if you want. Happy coding
I updated your foreach loop echo value's try this:
<?php
foreach($v_company as $v_company_name=>$v_company_info){
$rowcount = 0;
foreach($v_company_info as $v_branch=>$v_staffs){
echo "<tr>";
echo "<td>";
echo ($rowcount == 0 )? $v_company_name : "";
echo "</td>";
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
echo "<td>";
foreach($v_staffs as $v_staff){
echo "$v_staff <br/>";
}
echo "</td>";
echo "</tr>";
$rowcount +=1;
}
}
?>
Is this your desired Output?
You can use this to get all the employees in separate rows
<html>
<style>
</style>
<body>
<?php
$v_company= array(
'ABC'=>array('Kuching'=>array('Michael', 'Jenny'),
'Sibu'=>array('Sally', 'Muhammad', 'Mutu')
),
'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'),
'Sibu'=>array('John', 'Alicia')
)
);
?>
<table border="1" width="100%">
<tr><th>Company's Name</th>
<th>Branch</th>
<th>Staff's Name</th>
</tr>
<?php
foreach($v_company as $v_company_name=>$v_company_info){
foreach($v_company_info as $v_branch=>$v_staffs){
foreach($v_staffs as $v_staff){
echo "<tr>";
echo "<td>";
echo "$v_company_name";
echo "<td>";
echo "$v_branch <br/>";
echo "</td>";
echo "<td>";
echo "$v_staff <br/>";
echo "</td>";
echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>";
echo "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>
Related
here the problem is i need to view the whole row data in view details page when the button is clicked but when i click the last row data is sent through session variable and last row is displayed but i need particular row to display fully when the name is selected of the employee. please help me out in this .
thank you
index.php
<?php
include('connection.php');
$sql = "SELECT empname,salary,contact_no,department,empdesg FROM add_emp ORDER BY empname ASC ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
?>
<center>
<table cellpadding="20px" cellspacing="40px;" border="2px" align="center" width="device">
<tr>
<th>Name</th>
<th>Department</th>
<th>Designation</th>
<th> </th>
<?php
while($row = $result->fetch_assoc())
{
$name='';
$name=$row["empname"];
$department=$row["department"];
$designation=$row["empdesg"];
?>
</tr>
<td>
<?php echo $name?>
</td>
<td>
<?php echo $department?>
</td>
<td>
<?php echo $designation?>
</td>
<td><a href="viewdetails.php">
<button>Details</button>
</a>
<?php
$_SESSION['name']= $name;
?>
</td>
<?php
}
}
else {
echo "0 results";
}
?>
viewdetails.php
<?php
session_start();
include('connection.php');
$sql="select * from add_emp where empname='$_SESSION[name]'";
$result = mysqli_query($con, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo " <table cellpadding='5px' cellspacing='10px' border='1px' align='center'>";
while ($row = mysqli_fetch_assoc($result))
{ // Important line !!! Check summary get row on array ..
echo "<tr>";
echo "<th>" ."Name". "</th>";
echo "<td>" .$row['empname']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."DOB". "</th>";
echo "<td>" .$row['eage']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Adhaar Number". "</th>";
echo "<td>" .$row['adhaar']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Address". "</th>";
echo "<td>" .$row['address']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Salary". "</th>";
echo "<td>" .$row['salary']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Phone Number". "</th>";
echo "<td>" .$row['contact_no']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Department". "</th>";
echo "<td>" .$row['department']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Designation". "</th>";
echo "<td>" .$row['empdesg']. "</td>";
echo "</tr>";
echo "<tr>";
}
echo "</table>";
?>
I cant view Invalid, any suggestion to make it run?
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Strand</th>
<th>Section</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","search");
$set=$_POST['search'];
if ($set > 0) {
$show="SELECT * FROM search_student WHERE fname='$set'";
$result=mysqli_query($conn,$show);
while ($rows=mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>";
echo $rows['fname'];
echo "</td>";
echo "<td>";
echo $rows['lname'];
echo "</td>";
echo "<td>";
echo $rows['strand'];
echo "</td>";
echo "<td>";
echo $rows['section'];
echo "</td>";
echo "<tr>";
echo "</br>";
}
}
The else statement here doesn't work
else{
echo "invalid";
}
?>
</table>
if(mysqli_num_rows($result)>0){
while ($rows=mysqli_fetch_array($result) ) {
echo "<tr>";
//etc
}
}
else
{
echo "invalid";
}
Hi I'm creating a questionnaire. The questions and their answers are stored in a table like so:
Now, I want to display the questions and their answers in a table where I get the questions and their possible answers on different rows. Like so:
In my php file, I echo the table and the rows but I cannot figure out how to put the questions and answer in different rows. here is how it looks like:
and here is my php code:
<?php
session_start();
$status=$_GET["status"];
include 'dbh.inc.php';
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
text-align: left;
padding: 8px;
}
#td_box{
text-align: center;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #006689;
color: white;
}
</style>
</head>
<body>
<?php
if ($status=="disp") {
$sql="SELECT * FROM questions";
$result = mysqli_query($conn,$sql);
echo "<table>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"; echo $row["question"]; echo "</td>";
echo "<td>"; echo $row["optionA"];echo $row["optionB"];echo $row["optionC"];
echo $row["optionD"];echo $row["optionE"];echo "</td>";
}
}
echo "</table>";
?>
</body>
</html>
Try:
<?php
if ($status=="disp") {
$sql="SELECT * FROM questions";
$result = mysqli_query($conn,$sql);
echo "<table>";
while ($row=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"; echo $row["optionA"];echo $row["optionB"];echo $row["optionC"];
echo $row["optionD"];echo $row["optionE"];echo "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
What I did:
I added a </tr> after your question echo and opend a new one directly after. Then I closed that <tr> after your last <td> as you forgot to do that.
1st : Put all option radio buttons in another tr .
2nd : Maintain proper name attribute for radio button .name="question[$row['question_id']]"
3rd : And use colspan="4" for question td
while ($row=mysqli_fetch_array($result)) {
echo "<tr >";
echo "<td colspan='4'>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionA']\" >".$row['optionA'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionB']\" >".$row['optionB'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionC']\" >".$row['optionC'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionD']\" >".$row['optionD'];
echo "</td>";
echo "<td>";
echo "<input type=\"radio\" name=\"question[$row['question_id']]\" value=\"$row['optionE']\" >".$row['optionE'];
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='4'>"; echo $row["question"]; echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"; echo $row["optionA"];echo "</td>";
echo "<td>"; echo $row["optionB"];echo "</td>";
echo "<td>"; echo $row["optionC"];echo "</td>";
echo "<td>"; echo $row["optionD"];echo "</td>";
echo "</tr>";
this way you will have proper answers in tabular format in aligned manner
I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"
I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP