table header repeating in xml data table - php

Im pulling some data from an xml on a separate site and trying to display that data in a table, i can pull the data but when i tried to display the data in a table for each row i got the table headers, now i know why this is happening (because the table header echo is being called for every row) but i cant see how to fix it.
<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";
$xml = simplexml_load_file($url);
foreach($xml->CD as $cd){
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
echo "</tbody></table>";
}
?>

You mean:
<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";
$xml = simplexml_load_file($url);
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'>\n";
echo "<thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead>";
echo "<tbody>";
foreach($xml->CD as $cd){
echo "<tr>";
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
Note that you called table inside your foreach loop, and you also didn't specify the start and end of the row with tr

i think the table will come before the foreach loop so that each record will create a row rather than a table
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";
foreach($xml->CD as $cd)
{
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
}
echo "</tbody></table>";

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>

Display number sequence based on the number of data in database

How can I display the number based on the number of data from database.
For example, the above picture, shows data extract from the database. In my database there are two data. Therefore I would like to display number 1 and 2 at the No column.
And if there is four data in the database, I would like to display number 1, 2, 3 and 4 at the No column.
Coding to display the above table
<?php
$sql= mysql_query ("SELECT * FROM employee INNER JOIN cash ON employee.emp_id = cash.emp_id WHERE cash_status='Pending'");
echo "<table id='dataTable' width='850' border='1' align='center'>";
echo "<tr>
<th height='50'>No</th>
<th height='50'>Employee Number</th>
<th height='50'>Name</th>
<th height='50'>Department</th>
<th height='50'>Date Apply</th>
<th height='50'>Date Cash To Be Use</th>
<th height='50'>Amount</th>
<th height='50'>Status</th>
<th height='50'>Cash Id</th>
<th height='50'>View</th>
</tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td align='center' height='30'></td>";
echo "<td align='center' height='30'>" .$row['emp_id']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_name']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_department']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_dapply']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_duse']. "</td>";
echo "<td align='center' height='30'>RM" .$row['cash_amount']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_status']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_id']. "</td>";
echo"<td height='30'><img src='../img/view_user.png' width='20' height='20'></td>";
echo "</tr>";
}
echo "</table>";
?>
Thanks.
Try with the Below Code :
<?php
$i = 0;
$sql= mysql_query ("SELECT * FROM employee INNER JOIN cash ON employee.emp_id = cash.emp_id WHERE cash_status='Pending'");
echo "<table id='dataTable' width='850' border='1' align='center'>";
echo "<tr>
<th height='50'>No</th>
<th height='50'>Employee Number</th>
<th height='50'>Name</th>
<th height='50'>Department</th>
<th height='50'>Date Apply</th>
<th height='50'>Date Cash To Be Use</th>
<th height='50'>Amount</th>
<th height='50'>Status</th>
<th height='50'>Cash Id</th>
<th height='50'>View</th>
</tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td align='center' height='30'>".$++i."</td>";
echo "<td align='center' height='30'>" .$row['emp_id']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_name']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_department']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_dapply']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_duse']. "</td>";
echo "<td align='center' height='30'>RM" .$row['cash_amount']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_status']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_id']. "</td>";
echo"<td height='30'><img src='../img/view_user.png' width='20' height='20'></td>";
echo "</tr>";
}
echo "</table>";
?>
#Hardy Change .$++i. to .++$i.

PHP script not passing url with a array variable

This code is not passing URL to make topic clickable main problem is in third line after while loop. I got this error :
Parse error: syntax error, unexpected 'id' (T_STRING), expecting ',' or ';' in D:\xmapp\htdocs\forum\main_forum.php on line 37
Code :
while($rows=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>",$rows['id'],"</td>";
echo "<td bgcolor='#FFFFFF'>",'$rows['topic']. ',"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['view'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['reply'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['datetime'],"</td>";
}
Please try this. there is syntax error in your code.
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>",$rows['id'],"</td>";
echo "<td bgcolor='#FFFFFF'>",''.$rows[topic]. ' ',"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['view'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['reply'],"</td>";
echo "<td align='center' bgcolor='#FFFFFF'>",$rows['datetime'],"</td>";
This should correct your problem, Use
echo "<td bgcolor='#FFFFFF'><a href='".$view_topic.".php?id=".$rows['id']."'>".$rows['topic']."</a></td>";
instead of
echo "<td bgcolor='#FFFFFF'>",'$rows['topic']. ',"</td>";
One more thing, Why dont you use php tag only when needed. If you do like this it will be clean and easy.
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td> <?php $rows['topic']; ?> </td>
.......
.......
</tr>
<?php } ?>
Try this :
while($rows = mysql_fetch_array($result)){
echo "<tr>";
echo "<td align='center' bgcolor=#FFFFFF>".$rows['id']."</td>";
echo "<td bgcolor='#FFFFFF'><a href='".$view_topic.".php?id=".$rows['id']."'>".$rows['topic']."</a></td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['view']."</td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['reply']."</td>";
echo "<td align='center' bgcolor='#FFFFFF'>".$rows['datetime']."</td>";
}

how to pass php value as parameter in anchor tag href

I want to pass php value as parameter in anchor hreh.Here i have the problem while fetching the 'c_id'.c_id is the primary key.
Here I attached my html code and php code
<?php
include "config.php";
$sql="select c_name from client order by c_name asc limit 10";
$sql1=mysql_query($sql);
echo "<table border='1' align=center width=500 style='border-collapse: collapse;'>";
echo "<tr height =10><td width=300 align=center><b>Client name</b></td><td colspan=2 align=center><b>Action</b></td></tr>";
while($fet=mysql_fetch_assoc($sql1))
{
$id=$fet['c_id'];
echo "<tr>";
echo "<td align=center width=100>".$fet["c_name"]."</td>";
echo "<td align=center width=100><a href='client_details.php?id=".echo $id."'>Edit</a></td><td><a href=''>Delete</a></td>";
echo "</tr>";
echo "</table><br>";
}
?>
Just change:
echo "<td align=center width=100><a href='client_details.php?id=".echo $id."'>Edit</a></td><td><a href=''>Delete</a></td>";
To:
echo "<td align=center width=100><a href='client_details.php?id=".$id."'>Edit</a></td><td><a href=''>Delete</a></td>";
Because you already used the echo. You can't use echo inside another echo.
Try this, I hope It works for you
Read more

How to Show Popup window on clicking dynamically generated link

T have one page on which results are get created dynamically. And I
want to display popup window on clicking image named view.jpg. The
image is hyperlink. So please tell me how should i do it. I am showing
my code as follows.
code :
echo "<table width='' height='' border='1' align='center'>
<tr><td>Title</td>
<td >Type</td>
<td >Date</td>
<td>Expiry Date</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td>View</td>
<td>Edit</td>
<td>Delete</td>";
}
echo "</tr>";
$qry2 = mysqli_query($con,"SELECT nId,nTitle,nDescription,nDate,nExpiryDate FROM tblnoticemanager where Society_id = '$socId' and category='General'") or die(mysqli_error($con));
while($NoticeData = mysqli_fetch_array($qry2))
{
echo "<tr>";
echo "<td align='center' class='tdletter' style='text-transform:capitalize;'>" .$NoticeData['nTitle']. "</td>";
echo "<td align='center' class='tdletter' ><div class='overflowDiv'>" . $NoticeData['nDescription']."</div></td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nDate'] ."</td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nExpiryDate'] ."</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."'><img src='images/view.png' width='30' height='30' align='center' /></a></div></td>";
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."' ><img src='images/edit.jpg' width='30' height='30' align='center'/></a></div></td>";
echo "<td align='center' class='tdletter'><span id='".$NoticeData['nId']."' class='trash'><img src='images/delete1.jpg' width='30' height='30' align='center' /></span></td>";
}
echo "</tr>";
}
echo "</table>";
You can use attribute selector [attribute='value']
$("[src='images/view.png']").click(function(){
alert("clicked");
});

Categories