running php 5.6.29 here.
Processing about 6,000 rows of data from a MYSQL Table. Looking at the HTML Description column and making changes. Using html_entity_decode to show the changed result in the Browser.
The problem is, each row is displaying the HTML result nested inside the previous row, which makes the display impossible. Obviously I want each row to have a clean break. As these are tables, it seems not to break?
while ($row = mysqli_fetch_array($result))
{
$i++;
echo "<br>$i";
echo "<br>Product: ".$row['id'];
echo "<br>Title: ".$row['title'];
echo "<br>";
$a = $row['html'];
echo "htmlspecialchars_decode($a)";
//...change the html process here ...
$b = html_entity_decode($row['html']);
echo "<br><br><br>".$b;
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
}
I checked this in multiple browsers, so it's not just Chrome. Basically I somehow need to close the HTML before I move onto the next row right? Or is this some kind of limitation or bug? tia.
Related
For some reason after my table displays the following extra text: echo;"
I've played with the code a lot but get the same result.
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo(
"<tr>"
."<th>"
."Online Users"
.$num_rows
."</th>"
."<th>"
."Offline Users"
."</th>"
."</tr>"
);
while($row = mysqli_fetch_assoc($result)) {
echo(
"<tr>"
."<td>"
.row['name']
."</td>"
."</tr>"
);
}
echo "</table>";
} else {
echo "an error has occurred";
}
The problem is that your server is not parsing the PHP at all; it's returning it to the browser as if it were HTML. You need to do three things:
Make sure you have <?php before the code in your question.
Make sure your file's name ends in .php.
Make sure your server has PHP installed and knows to interpret .php files as PHP, not as HTML.
Many variations of this question have been asked here before, though I couldn't find an exact duplicate. It's not clear from your question which of the above 3 steps is causing your problem.
I am trying to run an infinite while loop in php:
while($resultst = $sqlst -> fetch()){
$adlist = $resultst["Monday_Morning"];
echo "<div id='slideshow'>";
echo "<div>";
echo "<img src='/$adlist'>";
echo "</div>";
}
The code above loop through the result set once and stop, however the results are images which I need to loop continuously until refreshed with new images.
I know that
while(true){
//code to execute
}
will cause an infinite loop, but how do I implement the true into my code?
This will loop forever and will generate html but the generation will never stop and so, the user will never see the result:
while(true){
if ($adlist = $resultst["Monday_Morning"]){
echo "<div id='slideshow'>";
echo "<div>";
echo "<img src='/$adlist'>";
echo "</div>";
}
}
If you want to load new results you need to use Ajax or Ajax like mechanism and add new results to the DOM dynamically.
Use ajax to load the images into the slideshow div.
It will run in background without refreshing the page and will load the new images.
The existing above code will run and can create the overhead.
I have an old PHP4 web app in which most of the pages looks like this(some pages has a left menu, some doesn't have a footer):
<?php
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
// Here i fetch data from DB
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
echo "</body>";
echo "</html>";
?>
This is a simple example, the real ones contains a lot of spaghetti code (i'm italian, i like spaghetti but not in my code) and i'm trying to refactor/redesign it in some way. Rewrite the entire app from scratch (maybe with an MVC framework) is not an option because the app contains a lot of business logic i would like to keep.
My idea (for now) is to wrap the echos inside a renderer class, something like this:
<?php
class PageRenderer {
public static function renderHeader() {
echo "<html>";
echo "<head><title>TITLE GOES HERE</title></head";
echo "<body>";
echo "<h2>THIS IS A TITLE</h2>";
}
public static function renderContent($rowsFromDB) {
echo "<table>";
echo "<tr>";
echo "</tr>";
foreach($rowsFromDB as $row) {
echo "<tr>";
// here i echo some <td> containing $row data
echo "</tr>";
}
echo "</table>";
}
public static function renderFooter() {
echo "</body>";
echo "</html>";
}
}
$renderer=new PageRenderer();
$renderer->renderHeader();
// Fetch data from DB
$renderer->renderResults($rowsFromDB);
$renderer->renderFooter();
?>
The problem with the above solution is that is difficult to extend and maintain. Do you know any design pattern or any technique i could use for a better refactoring/redesign?
Thanks in advice and sorry for my bad english
I'd add a method, maybe call it renderColumn($tdParams = array()) which only has simple job of returning a single td element (as a string):
Initialize an empty string, $td_cell
Append to $td_cell an opening <td> tag, maybe accept an array of attributes and values for said td tag as paramater $tdParams which has been set a default value of an empty array.
Append to $td_cell a closing </td> tag.
return $td_cell
For rendering out your DB Rows, you may (at later point in time) have a query that has more, or less data points - thus will result in needing more or less td cells.
For your renderHeader method, I would add at least 2 parameters: title and maybe for the <h2> as you specified, as I can see that changing frequently.
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText
My goal is to print an updating progress percentage to the console (in both Linux and Windows). Currently I just print out the percentage each 10%, but I would prefer that it updated itself every 1% without filling the screen with percentages.
Is it possible to remove text you have written to the console in PHP?
echo chr(8);
will print a backspace character.
very simple
Note the example below
$removeLine = function (int $count = 1) {
foreach (range(1,$count) as $value){
echo "\r\x1b[K"; // remove this line
echo "\033[1A\033[K"; // cursor back
}
};
echo "-----------------------------\n";
echo "--------- Start -----------\n";
echo "-----------------------------\n";
sleep(2);
$removeLine(3);
echo 'hi';
sleep(2);
$removeLine();
echo 'how are you ?';
die();
See Zend_ProgressBar
PEAR's Console_ProgressBar is useful for this sort of use case.
To clear the console entirely, you can use:
if($_SERVER['SHELL']) {
print chr(27) . "[H" . chr(27) . "[2J";
}
which is quite a bit easier than keeping track of how many characters to backspace.