Not sure I worded the title correctly, I'm unsure of what this is called.
I have a while loop which is populated by an SQL query
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
echo '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_'.$Tchannel.'-320x180.jpg" alt="Thumbnail"><br />';
echo '<p>';
}
My problem is that when the page loads it does it in sections. I've seen other websites populate these straight away on page load. How would I replicate something like this?
See Example: http://puu.sh/oTh6v/73446c0c15.jpg
Could this be what you meant to achieve...? And by the way, the opening Paragraph Tag:< p > could contribute to some unexpected &/or sluggish behaviour since it was just opened without being closed anywhere in your code... I commented it out... You may try to see if it helps....
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
// WHY DO YOU NEED THIS OPENING PARAGRAPH TAG? WHERE IS IT CLOSED IN YOUR CODE?
//echo '<p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?
If you need the images wrapped in paragraph tags, you can do it differently:
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<p>'; // <== OPEN A PARAGRAPH
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
$strOutput .= '</p>'; // <== CLOSE THE PARAGRAPH
// OR ALL IN ONE LINE:
// $strOutput .= '<p><img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br /></p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?>
Related
I have got a problem with opening the urls. It will not open for each url when I have output the list of urls in the PHP after I have extract the urls from the mysql database.
Here is the php:
<?php
//Connect to the database
require_once('config.php');
$qrytable1="SELECT links FROM channels_list";
$result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
while ($row = mysql_fetch_array($result1))
{
echo $row["links"];
$baseUrl = file_get_contents($row["links"]);
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
foreach ($links as $link)
{
echo "we are now opening for each url";
}
}
Here is the output for the urls:
http://example.com.com/some_name/?id=963
http://example.com.com/some_name/?id=102
http://example.com.com/some_name/?id=103
http://example.com.com/some_name/?id=104
http://example.com.com/some_name/?id=171
http://example.com.com/some_name/?id=106
http://example.com.com/some_name/?id=107
http://example.com.com/some_name/?id=108
http://example.com.com/some_name/?id=402
http://example.com.com/some_name/?id=403
http://example.com.com/some_name/?id=404
http://example.com.com/some_name/?id=405
http://example.com.com/some_name/?id=406
http://example.com.com/some_name/?id=408
http://example.com.com/some_name/?id=407
http://example.com.com/some_name/?id=409
http://example.com.com/some_name/?id=435
http://example.com.com/some_name/?id=436
http://example.com.com/some_name/?id=439
http://example.com.com/some_name/?id=440
http://example.com.com/some_name/?id=410
http://example.com.com/some_name/?id=411
http://example.com.com/some_name/?id=413
http://example.com.com/some_name/?id=414
http://example.com.com/some_name/?id=415
http://example.com.com/some_name/?id=417
http://example.com.com/some_name/?id=418
http://example.com.com/some_name/?id=421
I think there is a problem with this code:
$links = $domdoc->getElementsByTagName('a');
I don't have the html tag in my PHP page, it is only show the strings of the actual urls like what I show on above.
What I'm expect is I want to open each url when I get the list of urls from mysql.
Can you please help me with how I can open for each url when I get the urls from mysql database?
i'm not exactly sure, what you mean by "open for each url".
if you want as output a list of links, on which you can click:
while ($row = mysql_fetch_array($result1))
{
echo "<a href='".$row["links"]."'>".$row["links"]."</a>";
}
if you want to download the contents of each url:
while ($row = mysql_fetch_array($result1))
{
$content_string = file_get_contents($row["links"]);
}
$content_string is a content of a page as string, not sure what you want to do with it.
I have been trying get data out of new reddit post, but theres limitation where you cant get data from more than 100 posts. can anybody help me to getover this below is my code
$output = "";
for($digit=0; $digit<1000; $digit+=25){
$jsondata = trim(file_get_contents("http://www.reddit.com/new/.json?count=$digit"));
$json = json_decode($jsondata, true);
$moviesChildren = $json['data']['children'];
foreach($moviesChildren as $movie){
$output .= '"'.$movie["data"]["title"].'", ';
$output .= $movie["data"]["ups"].", ";
$output .= $movie["data"]["num_comments"].", ";
$output .= $movie["data"]["domain"]."\n\r";
$output .= "<br />";
}
}
echo $output;
What is the output you get, and what are you expecting instead?
First off, you will want to follow the API rules about authentication or else you'll be quickly limited, and possibly banned.
Listings have before and after attributes to help with pagination. You will need to pass those into your subsequent GETs in order to fetch the next page.
I'm working on a project and it's something new for me. I'll need to fetch rss content from websites, and display Descripion, Title and Images (Thumbnails). Right now i've noticed that some feeds show thumbnails as Enclosure tag and some others dont. right now i have the code for both, but i need to understand how i can create a conditional like:
If the rss returns enclosure image { Do something }
Else { get the common thumb }
Here follow the code that grab the images:
ENCLOSURE TAG IMAGE:
if ($enclosure = $block->get_enclosure())
{
echo "<img src=\"" . $enclosure->get_link() . "\">";
}
NOT ENCLOSURE:
if ($enclosure = $block->get_enclosure())
{
echo '<img src="'.$enclosure->get_thumbnail().'" title="'.$block->get_title().'" width="200" height="200">';
}
=================================================================================================
PS: If we look at both codes they're almost the same, the difference are get_thumbnail and get_link.
Is there a way i can create a conditional to use the correct code and always shows the thumbnail?
Thanks everyone in advance!
EDITED
Here is the full code i have right now:
include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
$feed = fetch_feed('http://feeds.bbci.co.uk/news/world/africa/rss.xml'); // this is the external website's RSS feed URL
if (!is_wp_error($feed)) : $feed->init();
$feed->set_output_encoding('UTF-8'); // this is the encoding parameter, and can be left unchanged in almost every case
$feed->handle_content_type(); // this double-checks the encoding type
$feed->set_cache_duration(21600); // 21,600 seconds is six hours
$feed->handle_content_type();
$limit = $feed->get_item_quantity(18); // fetches the 18 most recent RSS feed stories
$items = $feed->get_items(0, $limit); // this sets the limit and array for parsing the feed
endif;
}
$blocks = array_slice($items, 0, 3); // Items zero through six will be displayed here
foreach ($blocks as $block) {
//echo $block->get_date("m d Y");
echo '<div class="single">';
if ($enclosure = $block->get_enclosure())
{
echo '<img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100">';
}
echo '<div class="description">';
echo '<h3>'. $block->get_title() .'</h3>';
echo '<p>'.$block->get_description().'</p>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
}
And here are the XML pieces with 2 different tags for images:
Using Thumbnails: view-source:http://feeds.bbci.co.uk/news/world/africa/rss.xml
Using Enclosure: http://feeds.news24.com/articles/news24/SouthAfrica/rss
Is there a way i can create a conditional to use the correct code and always shows the thumbnail?
Sure there is. You've not said in your question what blocks you so I have to assume the reason, but I can imagine multiple.
Is the reason a decisions with more than two alternations?
You handle the scenario of a feed item having no image or an image already:
if ($enclosure = $block->get_enclosure())
{
echo '<img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100">';
}
With your current scenario there is only one additional alternation which makes it three: if the enclosure is a thumbnail and not a link:
No image (no enclosure)
Image from link (enclosure with link)
Image from thumbnail (enclosure with thumbnail)
And you then don't know how to create a decision of that. This is what basically else-if is for:
if (!$enclosure = $block->get_enclosure())
{
echo "no enclosure: ", "-/-", "\n";
} elseif ($enclosure->get_link()) {
echo "enclosure link: ", $enclosure->get_link(), "\n";
} elseif ($enclosure->get_thumbnail()) {
echo "enclosure thumbnail: ", $enclosure->get_thumbnail(), "\n";
}
This is basically then doing the output based on that. However if you assign the image URL to a variable, you can decide on the output later on:
$image = NULL;
if (!$enclosure = $block->get_enclosure())
{
// nothing to do
} elseif ($enclosure->get_link()) {
$image = $enclosure->get_link();
} elseif ($enclosure->get_thumbnail()) {
$image = $enclosure->get_thumbnail();
}
if (isset($image)) {
// display image
}
And if you then move this more or less complex decision into a function of it's own, it will become even better to read:
$image = feed_item_get_image($block);
if (isset($image)) {
// display image
}
This works quite well until the decision becomes even more complex, but this would go out of scope for an answer on Stackoverflow.
I made this website that uses XML file to store news entries. So basically a person will "submit" his name, date and content to the file and then I use DOM and for loop to print out the entries in homepage.
Anyway I'm planning to add WYSIWYG editor for the posting part but for now they're forced to use 'a' tags to make the links clickable. The problem is I noticed links that start with http:// works but links that start with www. doesn't. Is there a way to make both work using 'a' tags?
I'm still new to web development so I'm wondering if someone can help me.
$doc->load('entries.xml');
$newsArray = $doc->getElementsByTagName ('entry');
for($i = $newsArray->length; $i > 0; $i--)
{
$ent = $newsArray->item ($i-1);
$title = $ent->childNodes->item (1)->nodeValue;
$message = $ent->childNodes->item (2)->nodeValue;
$name = $ent->childNodes->item (3)->nodeValue;
$date = $ent->childNodes->item (4)->nodeValue;
if (get_magic_quotes_gpc()) {
$message1 = stripslashes($message);
}
else {
$message1 = $message;
}
echo "<div id='newsSec'>";
echo "<p></p>";
echo "<div class='newsTitle'> <b> $title </b> </div>";
echo "<div class='newsMessage'> " .nl2br($message1) ."</div>";
echo "<div class='newsName'> <b>Posted by:</b> $name <b>$date</b> </div>";
echo "</div>";
Append double-slashes to the beginning of the URL.
It will take the same protocol as the host-page itself
...
Reference: Protocol relative links: rfc1808
Add $message1 = "http://".$message1; before the echos. This will add the http:// in front of the link output.
I'm using PHP and a for loop to prepare data into proper html and output the data using JSON to be appended and displayed on the page. JSON slash escaping is causing the html to be viewed incorrectly by the browser.
This is my PHP for loop:
$json = '<div id="rsec3" class="rsec">';
for($i=0; $i<count($array); $i++)
{
$coverart = $array[$i]['cover'];
if(empty($coverart))
{
$coverart = "nocoverart.gif";
}
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"></div>';
}
$json .= '</div>';
$json = json_encode(array('ok' => 'ok', 'html' => $json));
echo $json;
This is my javascript parsing and appending the json:
$.get('/index_get.php?iid='+this.id,function(data){
$('#indload').hide();
js=jQuery.parseJSON(data);
$('#indr').append(js.html);
});
This is what the browser is displaying, a bunch of useless jargon, and it is appending a </img="> on its own?
<img=" video cover thumbs img.png"></img=">
How can I prevent this from occuring, and having the image displayed properly?
I think problem could be invalid HTML tag <img> on the php code. In the <img> tag, src is missing and <img> tag was not closed.
Change the following
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"></div>';
to
$json .= '<div><img src="/video/cover/thumbs/' . $cover . '" /></div>';
you need to close the image tag in your string
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"/></div>';