Trying infinite while loop php - php

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.

Related

html_entity_decode nesting php

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.

Want to break the loop after 10 rows to show a banner and than continue the loop were it stopt using PHP

I have seen some solutions already for tables but for some reason the break also duplicated which only need to be shown once
i have the following code:
<?php
while($result=mysqli_fetch_array($query)){ echo "<div class='col-lg-12'><div class='panel panel-default'>".$result['brand']." ".$result['modelyear']." ".$result['type']."<img src='".$result['link_image']." heigth:'50px' class='img-responsive'></div></div>";}
?>
I would like to try to break it after 10 rows and than show a div with a banner in it.
i tried the following PHP add html break after every 10th mysql result
however the banner also multiplies it self.
and yes i am a noob
Regards
Bas
You can make a variable that increment every time the loop execute. After the condition met you can display your div and end the loop. Something like this.
$i=0;
while($result=mysqli_fetch_array($query)){
if($i % 10 == 0){
echo "<div>Banner Div</div>";
} else {
echo "<div>Normal Div</div>";
}
$i++;
}

using echo statement after dynamic HTML

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

Giving ids to nested xml data attributes, in php

I've got a data feed I'm importing that has a load of 'markets', I want to have a main page displaying all the markets, so for that id use a foreach loop to go through the data and on each market make a listing.
Each market has a bunch of attributes as well as nested participants, I want to then make a page for each market, that displays some information about each participant.
So the user would navigate to index.php > event.php?id101
This is the bit were ive become stuck, how can i send the user to the right page, I was thinking of using
<?php
$xml = simplexml_load_file('f1_feed.xml');
foreach($xml->response->williamhill->class->type->market as $event) {
$event_attributes = $event->attributes();
echo "<tr>";
// EVENT NAME WRAPPED IN LINK TO EVENT
echo "<td>";
echo '<a href="event.php?id=' . $market_id . '">';
echo $event_attributes['name'];
echo "</a>";
echo"</td>";
// DATE
echo "<td>";
echo $event_attributes['date'];
echo "</td>";
echo "</tr>";
}
?>
but how can I set a var $market_id (from the xml feed) to add to the end of the url, so it sends me to the right page ?
(f1_feed.xml is the same as the live xml feed, its just local for development)
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
which im bring in using simplexml
This worked for me;
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
// that gives an object (native)
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
// that gives an array (useless way!)
// $market_attributes = (array) $market->attributes();
// printf("%s\n",
// $market_attributes['#attributes']['id'],
// $market_attributes['#attributes']['name']);
}

Listing Directory files with php

I have a script which will list all the files from the image folder on the server into a drop down box.
$dir='images';
$list=scandir($dir);
if(isset($_POST['submit'])) echo 'Selected: ' . $_POST['image'];
foreach($list as $file)
{
//ignore . (current dir) and .. (parent dir)
if($file!=='.'&&$file!=='..')
{
echo "<option value=\"$file\"";
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
echo ">$file</option>";
}
}
What I am trying to do is when the file name of the image is selected that image is displayed.
echo "<img src='images/". $file ."' />";
I tried using the file verable but it only seams to display the last image in the directory. How would I go about fixing this.
You will have to do this with Javascript. Once the php script loads it has no one of knowing what is happening in the users window. You will want to create an event listener to the onChange even of your option group. And when the selection changes you will want to replace the src of the image tag being used to show a preview.
I'd highly suggest using a JS Library such as Jquery or simliar to help with the event listening, and dom manipulation.
If you mean doing this on the form load, I’d say replace
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
with
if(isset($_POST['submit'])&&$_POST['image']==$file)
{
echo 'selected="selected"';
$selected_file = $file;
}
then this instead
echo "<img src='images/". $selected_file ."' />";
Because at the moment, it’ll just load which ever one is last out of the foreach

Categories