How to add a link to a list in php - php

Hi I have a Api list that is been generated using php here is the code that is been used to generate the list
<html>
<title> API LIST</title>
<body>
<?php
$jsondata = file_get_contents("api_link");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach($output['A'] as $schools){
$output .= "<li>".$schools['name']."</li>";
}
$output .="</ul>";
echo $output;
?>
the list is populated successfully but how can i add a link to the list so that when a user click on one of the items it opens a particular linked page here are the ways i have tried
$output .= "<li ".$schools['name']."</li>";
$output .= "<li>".$schools['name']. "</li>";
$output .= "<li>".$schools['name']."</li>";
I don't know where or how to add the a href code

Use
$output .= '<li>'.$schools['name'].'</li>';

You just need some changes in your code.You can try for
$output .= '<a href=https://www.google.com'.$schools['name'].'><li>'.$schools['name'].'</li></a>';
As per the comments you can use . to concatenate(join) the string and variable in php.

Related

Link opens as part of page PHP

I have problem with simple link function. On submission form, there is textbox where user can include url. This url is then taken and changed into working link and placed in post. This part works fine. If user include http/https in front of url it will work fine. But if they don't include http/https or only www, then it will open link as example below:
mysiteexample.com/UrlTheyPasted.com
mysiteexample.com/www.UrlTheyPasted.com
(This should open as link to Video/Image/Site,..)
Here is code:
$output .= '<DIV CLASS="'.$outerclass.'">';
$output .= '<DIV CLASS="'.$innertclass.'">'.$title.'</DIV>';
$output .= '<DIV CLASS="'.$innervclass.'">'.$value.'</DIV>';
$output .= '</DIV>';
Anyone know solution for this or why is this happening?
During the processing of the user input, use parse_url() to find out if they have put the protocol on or not and act accordingly.
$url = 'test.com';
$urlscheme = parse_url($url, PHP_URL_SCHEME);
if (empty($urlscheme)) {
$url = 'http://'.$url;
}
die('<pre>'.print_r($url,true));
so for instance, just before your code, put the example:
$scheme = parse_url($value, PHP_URL_SCHEME);
if (empty($scheme)) $value = 'http://'.$value;
$output .= '<DIV CLASS="'.$outerclass.'">';
$output .= '<DIV CLASS="'.$innertclass.'">'.$title.'</DIV>';
$output .= '<DIV CLASS="'.$innervclass.'">'.$value.'</DIV>';
$output .= '</DIV>';

Grab data from than 100 reddit posts php

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.

SQL Query PHP, populate content quickly

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;
?>

PHP - generate & store xml as string in a variable

I'm trying to store a xml as string in a variable so that I can store it in my database.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
When I echo it, it's not displayed at all as if my web brower reads it as html tag. It doesn't even look like $xml is storing those as texts. Now, I'm trying to do it with DOMDocument... not not quite successful yet. Any tips? :(
Edited my stupid += mistakes..
PHP uses a . as a concatenate operator, or a .= as a shortcut, not a + or +=.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";

How do I include php within php?

I want to include:
<?php the_field('200_200_1', 'option'); ?>
before the opening div tag in the line below...
$output .= '<div class="datebarcolor">'.$dates4.'</div>';
I am not sure how to insert the php tag in these circumstances. This is a php file, btw.
Can someone point me in the right direction?
If you're using ACF in wordpress you can use get_field() over the_field() in order to store the output in your $output variable:
$output .= get_field('200_200_1', 'option');
$output .= '<div class="databarcolor">' . $date4 . '</div>';
If you want to include the output of some other PHP code (for instance, if the_field does some echo calls) and you want to add that to the $output variable, use ob_start and ob_get_clean, like so:
ob_start();
the_field('200_200_1', 'option');
$output .= ob_get_clean(); //This appends everything to $output that was echoed since the call to ob_start
$output .= '<div class="datebarcolor">'.$dates4.'</div>';
I think you either mean:
Include the file before executing this code:
include 'yourfile.php';
// ... some code ...
$output .= '<div class="datebarcolor">'.$dates4.'</div>';
or include the file and add it's output to $output:
// start output buffer
ob_start();
include 'yourfile.php';
// get buffer contents and clean the buffer
$output .= ob_get_clean();
$output .= '<div class="datebarcolor">'.$dates4.'</div>';

Categories