I cant echo Invalid as i use else statement - php

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

Related

Width table column bootstrap 3.3 with button

I got the following table with a "view" button in a template.php. Im using bootstrap 3.3.7
<?php
if($num>0){
echo "<table class='table table-responsive table-sm table-hover table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th></th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>{$description}</td>";
echo "<td>";
echo "<a href='view_thing.php?id={$id}' class='btn-info btn-sm'>";
echo "<span class='glyphicon glyphicon-eye-open'></span> View";
echo "</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
It looks like:
table test 1
What i tried:
<th class="col-md-1"></th>
<th style="width: 10%"></th>
What it should do:
The last column should fit the button size.
Solution:
echo <th style=width: '10%'></th>

html after td Rowspan, the Next column data goes to next row

Following is the code
I want out put as
http://crysol.com/crysol_soft/Test/Screenshot_3.png
With following code I am getting output as
http://crysol.com/crysol_soft/Test/Screenshot_4.png
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
echo " </tr>";
}
echo "<td rowspan='10'>Good</td>";
?>
What are the changes required
Here is the code with your desired output
used if condition for print only 1 time good column and used rowspan='5'
<?php
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
?>
and also you can used this code
for($i=1;$i<=5;$i++){
echo '<tr>';
if($i==1){
echo "<td rowspan='5'>Cat</td>";
}
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
Create Table inside td:
<?php
echo "<table border='1'>";
echo "<tr><th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th></tr>";
echo "<tr><td style=''>Cat</td>";
echo "<td><table style='width:100%;'>";
for($i=1;$i<=5;$i++){
echo "<tr>";
echo "<td style='border-bottom:1pt solid black;'>".$i.'</td>';
echo "</tr>";
}
echo "</table></td>";
echo "<td>Good</td></tr>";
?>

PHP foreach loop with array table

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>

Resolved Undefined Error, but now only 1 record displays instead of expected 3 for query

After resolving an Undefined offset error, the error message is no more, but the results now displayed are only one record instead of the expected number of records, for instance 3.
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
} $result = mysql_query($sql);
{
I can remove everything from $time down and then it returns all of the expected records.
As requested all of the code. I should preface this that I am still very very new to the world of PHP, so please go easy on me:
<?php
include('../datalogin.php'); // include your code to connect to DB.
mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$rID = (int)$_GET['id'];
$sql = "a bunch of SQL code removed to save space
WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
} $result = mysql_query($sql);
{
echo "<table border='0' width='640'>";
echo "<tbody>";
echo "<tr>";
echo "<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2'])). "
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_vertrek'>(D)
".htmlspecialchars($row['luchthavennaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='18%'><strong>Duration:</strong></td>";
echo "<td width='41%'>".timeInWords($time)."</td>";
echo "<td rowspan='4' width='41%' align='center' valign='middle'><img
src='../logos/".$row['logo']."'</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Equipment:</strong></td>";
echo "<td>".$row['toestel']." ".$row['erlr']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Class:</strong></td>";
echo "<td>".$row['reisklass']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Miles:</strong></td>";
echo "<td>".$row['afstand']." miles</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='12'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_aankomst'>(A)
".htmlspecialchars($row['aankomstnaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='1' bgcolor='#585858'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
This should do it, you called the mysql_query with the same query over again, so that would give you the same result all the time.
I cleaned the start of you while loop, so it only fetches the data from your result object and prints it. Your time function does not need to be inside the loop so I moved it outside.
I also cleaned the loop from some echo's
<?php
include('../datalogin.php'); // include your code to connect to DB.
function timeInWords($time) {
list($hours, $minutes) = explode(':', $time);
return (int)$hours . " hrs " . (int)$minutes . " min";
}
mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$rID = (int)$_GET['id'];
$sql = "a bunch of SQL code removed to save space
WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$time = $row['vluchttijd'];
echo "<table border='0' width='640'>
<tbody>
<tr>
<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_vertrek'>(D)
".htmlspecialchars($row['luchthavennaam'])."</span></td>
</tr>
<tr>
<td width='18%'><strong>Duration:</strong></td>
<td width='41%'>".timeInWords($time)."</td>
<td rowspan='4' width='41%' align='center' valign='middle'><img
src='../logos/".$row['logo']."'</td>
</tr>
<tr>
<td><strong>Equipment:</strong></td>
<td>".$row['toestel']." ".$row['erlr']."</td>
</tr>
<tr>
<td><strong>Class:</strong></td>
<td>".$row['reisklass']."</td>
</tr>
<tr>
<td><strong>Miles:</strong></td>
<td>".$row['afstand']." miles</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3' height='12'></td>
</tr>
<tr>
<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_aankomst'>(A)
".htmlspecialchars($row['aankomstnaam'])."</span></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3' height='1' bgcolor='#585858'></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";
}
echo "</table>";
?>

PHP loop - creating a table using a loop and giving it headers

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

Categories