The code that i made display the header after each row in the table.
I want just one time the header appear on the top.Any help?
print "<table border=1>\n";
while ($row = mysql_fetch_array($result)){
$files_field= $row['filename'];
$files_show= "Uploads/$files_field";
$descriptionvalue= $row['title'];
print "<tr>";
print "<th>";
echo "header1";
print "</th>";
print "<th>";
echo "header2";
print "</th>";
print "</tr>";
print "<tr>\n";
print "\t<td>\n";
echo "<font face=arial size=4/>$descriptionvalue</font>";
print "</td>\n";
print "\t<td>\n";
echo "<div align=center><a href='".$files_show."' target='_blank' title='CLICK TO OPEN'>$files_field</a></div>";
print "</td>\n";
print "</tr>\n";
}
print "</table>\n";
This program will:
get a row from db
print the header
print the data
get next row until the end
Sounds like you want to:
print the header
get a row from the db
print the data
get next row until the end
This should be adequate I think:
<?php
// print the beginning of the table, and the header
echo "<table border='1'>";
echo "<tr>";
echo "<th>";
echo "header1";
echo "</th>";
echo "<th>";
echo "header2";
echo "</th>";
echo "</tr>";
// than loop through the rows and print the rest of the table
while ($row = mysql_fetch_array($result)) {
$files_field = $row['filename'];
$files_show = "Uploads/{$files_field}";
$descriptionvalue = $row['title'];
echo "<tr>\n";
echo "\t<td>\n";
echo "<font face=arial size='4'/>$descriptionvalue</font>";
echo "</td>";
echo "<td>";
echo "<div align=center><a href='{$files_show}' target='_blank' title='CLICK TO OPEN'>{$files_field}</a></div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
Some additional notes / advises:
don't mix print / echo: they are almost the same, it just causes confusion
don't try to "prettify" the source with tabs and linebreaks: you are just bloating the output for no good reason, use the browsers inspector window instead
forget the mysql driver, use mysqli: the former is so out of dae, that it is removed from the new versions of PHP (from 7.0), so your code will stop working if the platform is updated; the mysqli variant is a bit different in how you have to use it (an extra parameter mostly), but the results are in the same format, and it behaves very similarly if not identically to the old one, so you will not have to rewrite many things
Related
echo "<table id='table_id' class='display table table-hover table-inverse'>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
When I add this row with a delete link, data table is not being applied to my table.
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Without the particular line all works fine.
Please help.
TIA
can you please confirm that in your code, number of columns inside 'thead' are equal to number of columns inside 'tbody'. It should not mismatch the number of columns of both 'thead' and 'tbody'
I use a MySql query to select data from my DB and then print it in the form of a HTML Table. It works perfectly, fine but sometimes the table consists of hundreds of rows and the webpage looks incredibly akward. Is there a way to split the table side by side into 2 or 3 halves.
Present Output
Desired output
PHP
<?php
....
echo "<h3>Classes attended :</h3>";
echo "<table class='dates' border='1'>";
foreach ($results as $dates) {
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
What would be the best way to achieve it?
Help would be appreciated.
You can use PHP to determine in your loop if the loop index is divisible by a certain number using something like this:
echo "<h3>Classes attended :</h3>";
echo "<table class='dates' border='1'>";
$rowCount = 1;
$numRows = count($results);
$maxRows = 12;
foreach ($results as $dates) {
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
if($rowCount % $maxRows == 0 && $rowCount != $numRows) {
echo "</table><table class='dates' border='1'>";
}
$rowCount ++;
}
echo "</table>";
That's the basics of doing this. Basically in your loop you're testing each index to see if it's divisible by $maxRows, and if so then you're going to close your table and open a new one. You'll have to add the styling to place the tables side by side.
If you wanted to expand upon this concept you can set $maxRows to be an evaluation of $numRows. For instance if you wanted to split the items as close as possible to half in order to show just two tables, you could do... $numRows = count($results); $maxRows = round($numRows / 2);
Inspired by Robert Wade's answer:
<?php
....
echo "<h3>Classes attended :</h3>";
$i=0;
$maxRows=10;
foreach ($results as $dates) {
$a=$i/$maxRows == 0 ? "<table class='dates' border='1'>":"";
$b=$i/$maxRows == 0 ? "</table>":"";
echo $a;
echo "<tr><td width='50%'>";
echo $dates->db_date;
echo "</td>";
echo "<td width='50%'>";
echo $dates->day_name;
echo "</td>";
echo "</tr>";
echo $b;
$i++;
}
?>
At last, add some css style to the tables.
You can also use array_chunk() for splitting your results. Or instead of displaing a lot of tables next to each other you can make pagination and get only some range in your query. For example:
SELECT * FROM `clients` LIMIT 5, 10
Selects 10 rows beggining from row 5. Now, when you change your page, just change limit values.
To clarify, I am trying to get a table to output that displays my MySQL database table in a certain way. The table is populated with names, birth-years, and zodiac animals.
What I want to do is have a table row that says something like "cat zodiac signs" and then a heading of "name" and "birth year". Under that I want each persons' name and year they were born. I want this done for each zodiac animal.
The issue I am having is that the heading for is displaying each instance of the zodiac column. I know my code is wrong, I am just not sure what to do next! Please help! Here is my code
$TableName = "zodiacshared";
$SQLstring = "SELECT * FROM $TableName GROUP BY sign, birthyear";
$QueryResult = #mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) == 0){
echo "<p> There are no entries in the chinese zodiac gallery! </p>";
}
else{
echo "<table width = '100%' border = '1'>";
while ($row=mysql_fetch_array($QueryResult)){
echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
echo "<tr><th>Name</th><th>Birth Year</th></tr>";
echo "<tr>";
echo "<td>"; echo $row['name']; echo "</td>";
echo "<td>"; echo $row['birthyear']; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_free_result($QueryResult);
}
Edit: Right that may be helpful
My hopeful output is something like this (this is a rough draft made on Excel, but gets point across):
edit 2: Solution was:
$current_zodiac= '';
while ($row=mysql_fetch_array($QueryResult)){
if ($row['sign']!=$current_zodiac){
echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
echo "<tr><th>Name</th><th>Birth Year</th></tr>";
}else{
//echo the other rows
}
echo "<tr>";
echo "<td>"; echo $row['name']; echo "</td>";
echo "<td>"; echo $row['birthyear']; echo "</td>";
echo "</tr>";
$current_zodiac = $row['sign']; //assign new zodiac to current zodiac variable
}
You can have one variable to store current zodiac name in your while loop. So every time the zodiac name is unique which means new zodiac found in the while loop, you can print new header for it.
For example, the flow could be like this.
$current_zodiac = '';
while ($row=mysql_fetch_array($QueryResult)){
if ($row['sign']!=$current_zodiac){
echo "<th colspan='2'>"; echo $row['sign']; echo " zodiac signs</th>";
}else{
//echo the other rows
}
$current_zodiac = $row['sign']; //assign new zodiac to current zodiac variable
}
So I am trying to display a list from another website in mine, it all works fine but only the first word of the 'title' attribute is stored. I know that the whole title is retrieved from the other website so how do I get it to store all of it.
Here is the code if it helps.
<?php
include "simple_html_dom.php";
$page = file_get_html("http://www.blade-edge.com/images/KSA/Flights/craft.asp?r=true&db=dunai");
echo "<table id=list>";
foreach($page->find('html/body/div/div[2]/ol/a') as $key=>$element) {
if ($element->title != "No craft within this SOI"){
$ships[$key] = $element->plaintext;
$shipTitles[$key] = $element->title;
$shipLinks[$key] = $element->href;
echo "<tr>";
echo "<td title =".$shipTitles[$key]." >";
echo $ships[$key];
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Put title inside quotation marks
echo "<td title =".$shipTitles[$key]." >"; // Wrong
Right is
echo "<td title ='".htmlspecialchars($shipTitles[$key], ENT_QUOTES)."' >";
Without quotes, you will only see the word till the first space. Also, the value itself must be escaped.
I would like to add a row, after the loop, the number of rows before the LAST row is random based on the user's orders. so for example the user ordered 10, i would like to add another row, to the list. even if the order is 3 there would still be another row at the end.
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>₱".number_format(($row['price']*$row['quantity']),2)."</td>";
echo "</tr>";
if($row['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$row['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Here, i made it create another row, but it creates a row after each row, i just need to add 1 row at the end of the table. Thank you :)
Hmm Why not just add a new query? I fixed your codes. and added these.
$qryx = "SELECT DISTINCT customers.serial,customers.name,customers.payment,customers.carrier,customers.tracking_no, orders.date, order_detail.productid, order_detail.quantity, order_detail.price, order_detail.orderid, inventory.prod_name
FROM customers
RIGHT JOIN orders on customers.serial=orders.serial
RIGHT JOIN order_detail on orders.serial=order_detail.orderid
LEFT JOIN inventory on order_detail.productid=inventory.prod_id
where customers.email='{$_SESSION['email']}'
AND order_detail.orderid='{$_REQUEST['orderid']}'
GROUP BY customers.carrier";
$resulty = #mysql_query($qryx);
while ($rowz=mysql_fetch_array($resulty)){
if($rowz['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$rowz['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
I changed the name of the qry, result, and row. and added DISTINCT and GROUP BY to remove redundancy.
You should add a line right before your
echo "</table>";
line, but after the closing bracket before it. That last closing bracket is where your loop ends. It should look like this:
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>₱".number_format(($row['price']*$row['quantity']),2)."</td>";
echo "</tr>";
if($row['carrier']=="LBC"){
echo "<tr>";
echo "<td>Shippment Option Chosen: ".$row['carrier']."</td>";
echo "<td></td>";
echo "<td>₱250.00</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td>LAST LINE...</td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
?>
You can use mysqli_num_rows to get the no. of rows. Then you can add a counter in the while loop. When the counter = mysqli_num_rows, you can echo the row you wanted to echo at last
Example:
$totalrows=mysqli_num_rows($result);
$counter=1;
while ($row=mysql_fetch_array($result)){
//echo the row data
if($counter==$totalrows)
{
//echo the extra row which you wanted.
}
}
echo "</table>";