I have a problem with flush() and a HTML table using "Internet Explorer".
I edited the following code to display a "real time" echo in a table with flush(), I put the flush() call after the </table> tag because in "Internet Explorer" it doesn't work, but I don't know why only the first echo of my cycle goes into the table, the others go outside. Any help?
<html lang="en">
<head>
</head>
<table border='1'>
<tr>
<td>
<?php
$total = 10;
for($i=1; $i<=$total; $i++){
echo "sometext";
//other stuff
?>
</td>
</tr>
</table>
<?php
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
?>
</body>
</html>
Put simply, you've closed the table inside the loop rather than outside of the loop. So after the first iteration, your table tags don't match up. Move </table> beyond your closing loop curly bracket }.
Concerning the Internet Explorer problem. You may want to look at this: http://php.net/manual/en/function.flush.php
It talks about some versions of Internet Explorer requiring a certain amount of bytes before it will begin to flush data. You may also want to try calling ob_flush(); above your flush(); statement.
The browser might be waiting for the closing because it does not know the content of all cells, and thus also the width of the columns. Try to hint it with fixed values for the td element, for example:
<td style="width: 200px">
You are closing your table inside of your loop.
<html lang="en">
<head>
</head>
<body>
<table border='1'>
<tr>
<td>
<?php
$total = 10;
for($i=1; $i<=$total; $i++){
echo "sometext";
//other stuff
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
?>
</td>
</tr>
</table>
</body>
</html>
Proof:
Related
All the code is within the same index file and same class. I can't seem to figure out why its not calling. Both objects themselves work in a call 2-3 lines right above, outside this table.... I tried calling them inside the table to no avail. They just pop up the word '(Array)' in both boxes. Is their a syntactical thing i am missing? On my website I print the table, with the missing functions inside table, and the two tables that are supposed to be inside the table printed right above and below the table. For reference, my website for the school project: http://www.wallofkron.x10host.com
<?php
$companyobject = new BurgerJoint(); //object created
$companyobject->getLeftNavBar($companyobject->navbar_array); //call to leftnav bar works
$companyobject->displayProduct($burgerarray); // burger table also works outside of table
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($burgerarray)
</TD></TR>
</TABLE>";
function displayProduct($array){
print "<TABLE BORDER = '1'>";
foreach ($array as $oneitem) {
print "<TR><TD>$oneitem</TD></TR>";
} print "</TABLE>";
}
function getLeftNavBar($array){
print "<TABLE BORDER = '1'>";
foreach($array as $key => $value) {
print "<TR><TD><A HREF=$value>$key</A></TD></TR>";
}
print "</TABLE>";
}
?>
i think i noticed the problem:
<?php
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($companyobject->burger_array)
</TD></TR>
</TABLE>";
?>
i think that the problem was that you were not reffering to the actual burgerarray inside the object companyobject.
as told in the comment it would look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Burgers</title>
</head>
<body>
<header style="background:orange; width:100%;">
<div style="text-align:center;">
<?php foreach($companyobject->navbar_array as $nav){
echo $nav
}?>
</header>
<main style="width:100%;">
<table style="height:200px; border:1px solid black;">
<?php foreach($companyobject->burger_array as $burger):?>
<tr><td><?php echo $burger; ?></td></tr>
<? endforeach; ?>
</table>
</main>
</html>
I'm new to PHP and trying to make the tables appear next to each other instead of appearing under the first one. I was trying to use "table style="float: left"" but that apparently doesn't work in PHP, anyone have an idea of how to do this?
Tried to fix it using your help, but nothing happens, maybe I'm misunderstanding something, I'm not very great at this.
<html>
<head>
<title>Produktsida</title>
<style type=”text/css”>
table {
display: inline-block
width: 500px;
}
</style>
</head>
<body>
<?php
require_once 'dbconfig.php';
$result = mysqli_query($con,"SELECT * FROM produkt JOIN bild ON bild.idBild = produkt.idProdukt JOIN ingrediens ON ingrediens.idIngrediens = produkt.idProdukt");
?>
<table border='1'>
<?php
while($row = mysqli_fetch_array($result))
{
$imgData = base64_encode($row['Bild']);
?>
<tr>
<td>
<img src="data:image/jpeg;base64,<?php echo $imgData ?>" /> </td>
</tr>
<tr>
<td>
<?php echo $row['Produkt_Namn'] ?>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
</body>
PHP has very little to do with this. The browser (which does the rendering) only sees the output from PHP (and any static files you reference).
table style="float: left" should work fine, just watch your quotes. You can't use an unescaped " inside a PHP string literal that is delimited by " characters.
Either use ' or escape the quote character.
PHP isn't a client-side language, it's server-side. Any output that you make in PHP is rendered as HTML.
By default, I believe tables are block level items, meaning that they don't allow content next to them.
What you can do is wrap your tables in a DIV element.
CSS
div.table-holder1 { width:50%; float:left; }
div.table-holder2 { width:50%; float:right; }
PHP
print '<div class="table-holder1"><table>...</table></div>';
print '<div class="table-holder2"><table>...</table></div>';
Unless you gave your tables a class, I'd do this:
<table class="myTableClass">
....
</table>
.myTableClass{width:40%; float:left; margin-right:5%}
That oghtta fix it
Not directly related to the question
My first tip would be to reformat your code without echoing every line of code:
<?php
$result = mysqli_query($con,"SELECT * FROM produkt JOIN bild ON bild.idBild = produkt.idProdukt JOIN ingrediens ON ingrediens.idIngrediens = produkt.idProdukt");
?>
<table border='1'>
<?php
while($row = mysqli_fetch_array($result)) { /* Begin looping the rows */
$imgData = base64_encode($row['Bild']);
?>
<tr>
<td>
<img src="data:image/jpeg;base64,<?php echo $imgData ?>" />
</td>
</tr>
<tr>
<td>
<?php echo $row['Produkt_Namn'] ?>
</td>
</tr>
<?php } /* End looping the rows */ ?>
</table>
<?php
mysqli_close($con);
?>
The answer
You have to style your table with css. By default tables are a block level element which automatically put a line break after the element.
There are two main types of display properties of css:
//The default for the tables, divs etc. Most elements use display: block.
display: block
//The default for span, img and some others
display: inline
As you may know, a span element doesn't put a line break after the element, so you could've used it if it was not for one problem: They don't have any real dimensions, so you can't set a width or height to them for example.
To solve this modern browsers has implemented display: inline-block which combines the no line break of a span and the dimensions of a div. According to caniuse it has closely to 93% browser support, so it's almost ready to be used.
If you want to go with display: inline-block, you could use this css snippet:
table {
display: inline-block
width: 500px;
}
If you would like to read more about inline-block, refer to this question about float vs inline-block.
EDIT: I solved my problem in total differnt way, thanks to those who respond anyways! helped a lot!
I need to send a table on HTML through a get variable.
The code is:
<html>
<head>
<meta charset="utf-8" />
<title>Itinerario</title>
<link rel="stylesheet" type="text/css" href="itine.css"/>
</head>
<body bgcolor="#FFC334" link="black" vlink="black" alink="white">
<?php
$html='
<font face="arial">
<center>
<table bgcolor="#F3F3F3" style="border:2px solid black;" cellpadding="10" cellspacing="10">
<tr>
<td></td>
<td>CELL 1</td>
<td>CELL 2</td>
</tr>
</table>
</center>
</font>
';
echo $html;
echo 'LINK ME';
?>
</body>
</html>
The other part its just a $_get:
<?php
$variable = $_GET['itine'];
?>
If I replace the $html in the URL for text it works fine but adding table structure sends nothing (no error), only blank.
Any ideas?
You wouldn't be able to send such complex HTML via GET, especially unescaped.
If you must pass HTML from page to page, you could base64_encode() the HTML, and probably gzcompress() it too. This would help prevent hitting the character limit for GET requests, but wouldn't eliminate the problem for very large tables.
I did once have to do something similar, which required passing a lot of data into GET variables. I had to create a couple of functions to make them GET friendly though:
function shrink($str) {
$str=strtr(base64_encode(addslashes(gzcompress($str,9))), '+/=', '-_,');
return $str;
}
function unshrink($str) {
$str = gzuncompress(stripslashes(base64_decode(strtr($str, '-_,', '+/='))));
return $str;
}
The shrink() function there gzips the string at compression level 9, and removes any characters from the encoded output that could cause issues for GET requests.
unshrink() does the opposite.
If you were to include those functions, you could do something like this in your example:
$html='
<font face="arial">
<center>
<table bgcolor="#F3F3F3" style="border:2px solid black;" cellpadding="10" cellspacing="10">
<tr>
<td></td>
<td>CELL 1</td>
<td>CELL 2</td>
</tr>
</table>
</center>
</font>
';
echo $html;
echo 'LINK ME';
Then to de-compress at the other end:
echo unshrink($_GET['itine']);
This was my first actual post here, so I hope it offers some help!
urlencode(), and no need to decode them, because when they are assigned to $_GET or $_POST they should already have been decoded.
The first problem is that you have a double quote in the HTML you want to send, this closes off the double quote of your a href.
Secondly, you might bump in the limitation of GET requests, theoretically they can only be 256 characters long.
Lastly, I would propose to you to urlencode/decode the data you try to send trough the url:
Encode:
echo 'LINK ME';
Decode:
<?php
$variable = urldecode($_GET['itine']);
?>
Well since you have some limitations about string length on $_GET, it's preferable not to do this. If you explain better what you need, maybe we can provide you another solution.
I'm a complete newbie when it comes to HTML, I've never needed to do any webpage stuff before so apologies if I'm being a simpleton.
I have a high score table for an iOS app, and I want to display it on the website for the app as well. The leaderboard is working fine in the app using PHP and mySQL, but when I try to access the database using PHP inside an HTML file it doesn't seem to pay any attention to the logic of the PHP (i.e. if I have an if/else statement, it will run both even cases).
Here is a simplified version of my code, if connection to the database is successful then the first column in the table header will say "success", otherwise it will say "failure".
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
else
{
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
?>
</table>
</body>
Can anyone see what has been done incorrectly here? I assumed that the if/else would work as normal, but instead I get both rows inserted into the table, "failure" and "success".
Edit:
I have also tried using echo ""; etc, but not sure which way is correct as they both give me the same error where it ignores the PHP.
Change your code to
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
?>
</table>
I think that you need to put your HTML tags in the echo command like this
echo '<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th> <th>Draws</th><th>Losses</th>
</tr>';
this should work
Check extension of your file. It need to be filename.php
You need a server to launch php code
You can use echo heredoc like here
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
echo <<<_END
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
else
{
echo <<<_END
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
?>
</table>
</body>
That seems to be working
Between <?php and ?> is just PHP code. It is treating you HTML as PHP.
Either
Use echo
put ?> before the HTML and <?php> after (i.e. stop and start PHP processing)
try this:
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php } ?>
</table>
</body>
Very often I have heard people suggesting (and I have done it myself too a few times) to keep things separate: PHP code here, HTML there, external CSS, external JS and so on and so on.
Aside from the obvious readibility and maintenance advantages of doing this are there other strong advantages (e.g. in terms of server load or page processing time) in doing it?
As a trivial example, say we want to implement a table containing some products we read from a DB.
The output we want would be something like
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<tr>
<td>Prod 1</td>
<td>Yes</td>
<td>$100</td>
</tr>
...
...
</table>
<div class="info">Some generic info on the products here</div>
So here we have some static output (the 2 div elements and the table header) and some dynamic output (the actual table content).
We could leave all the static things out of PHP tags and try to keep PHP only where needed
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
?>
</table>
<div>.....</div>
On the other hand we could embed everything in PHP
<?
echo '<div class="description">This table lists all our products</div>';
echo '<table class="products"><tr><th>Name</th>'.
'<th>Available</th><th>Price</th></tr>';
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<div>.....</div>';
What are the reasons to choose one over the other?
The alternative syntax for control structures seems to be more readable to me:
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?php foreach($products as $p): ?>
<tr>
<td><?php echo $p["name"]; ?></td>
<td><?php echo $p["availability"]; ?></td>
<td><?php echo $p["price"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="info"><?php echo $info; ?></div>
If its just a piece of code for you to play with, it doesn't really matter at all.
But if an application grows more and more complex (and more people work in it), it will usually come to a point where it is vital to separate the view layer (here: HTML) from the code layer (here: PHP) - so you can assign designers to play around with the output and coders to play around with the functionality behind it.
This ain't a php-only topic, this is very general. Architectural models like MVC are based on similar theories.
I think that it is a very compact string <?= $var ?>
I do not suggest to use short sytax of php. Sometimes it can be problem to move code from one server to another.
Reason you need to do so is time. Nice code is simple to support and upgrade. In some cases it is performance issue also, but not in your case.
Ideal example in your case is:
You have to files
index.php
products.php
File products.php contain
<?php
...
foreach($products as $product)
{
$productHTML[] = '<tr><td>' . $product["name"] . '</td></tr>';
}
$productHTML = implode("", productHTML);
?>
index.php:
<html>
...
<?php echo $productsHTML;?>
...
</html>
Ofcourse more advence developers use more hard constructions, we use functions, class, template idea and etc. But such way is enough for small project.