Dynamic html returns nothing - php

I'm in the process of learning php so forgive me if this is a silly question. I'm trying to make a dynamic gallery that looks through a folder(./JIN/snoii/) and produces the html code for the number of items in said folder. This code I've written isnt returning anything when the file has images(currently has over a hundred) can some one tell me where I messed up or point me in the right direction. Thanks in advance
$CurrentViewFile = "snoii";
function showGallery(){
global $CurrentViewFile;
$galleryHTML = "<h1>Dynamic gallery</h1>";
$galleryHTML .= "<ul>";
$folderToview = "JIN/";
$folderToview .= $CurrentViewFile;
$images = new DirectoryIterator($folderToview);
while($images->valid()){
$galleryHTML .= "<li>!!</li>";
$images->next();
}
$galleryHTML .= "</ul>";
return $galleryHTML;
}
return showGallery();

Your DirectoryIterator makes the problem,
Please try like this
$CurrentViewFile = "/snoii";
function showGallery(){
global $CurrentViewFile;
$galleryHTML = "<h1>Dynamic gallery</h1>";
$galleryHTML .= "<ul>";
$folderToview = "JIN/";
$CurrentViewFile .= $folderToview;
foreach (new DirectoryIterator($CurrentViewFile) as $images) {
if($images->valid())
$galleryHTML .= "<li>!!</li>";
$images->next();
}
$galleryHTML .= "</ul>";
return $galleryHTML;
}
$output_data = showGallery();
echo $output_data;
If you want to get file name from the iterator you should use like this,
$images->getFilename()

Related

Show to show php object um html modal

I'm trying to show a object in a html modal. Since I don't know the structure beforehand, and the child properties can also be objects or arrays, a simple recursive function seemed to be the way to go, but Im not sure. I have this:
<?php
class LogHelper extends Helper
{
public function warningLogError($body_error)
{
$data = [];
$html = '';
$html .= "<div name='bodyErrosr' class = 'form-group'>";
$html .= "<div class = 'alert alert-warning' role = 'alert'>";
if (!empty($body_error)) {
foreach($body_error as $key => $value) {
$html .= "<li><b>" . $key . "= " . $value. "</b></li>";
}
} else {
$html .= "<li><b>" . 'Error!' . "</b></li>";
}
$html .= "</ul>";
$html .= "</div>";
$html .= "</div>";
return $html;
}
}
But I don't know the structure beforehand, sometimes I receive an object inside object multiple times. How is the best way to do this?
Have a look at: https://packagist.org/packages/symfony/var-dumper
This also handles circular references (where print_r/vardump fail)

Output data of JSON Array as HTML

I'm trying to create an overlay for my twitch stream, which display not only the recent follower but a certain amount of them in chronological order.
Here a visual representation:
(Styling is done with HTML/CSS)
I've already managed to get the JSON but I don't know how to echo the names for each ['follows']['user']['display_name']
This is the API:
https://api.twitch.tv/kraken/channels/kazutode/follows?limit=5&offset=0&client_id=nht9j8w0u4xazpm1fdc2fmkrqvoici
Here's my code so far (It's not alot, basically nothing xD)
<?php
$clientID = "nht9j8w0u4xazpm1fdc2fmkrqvoici";
$channel = "kazutode";
$limit = 5;
$offset = 0;
$response = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.$channel.'/follows?limit='.$limit.'&offset='.$offset.'&client_id='.$clientID), true);
?>
I know, I know. I should use curl but I don't know how so I stick to json_decode.
Can someone teach me how to get the wanted data out of the ['follows'] array?
You need a foreach loop. Here is some PHP+HTML mixed to give you an idea:
$table = '';
$table .= '<table>';
foreach($response['follows'] as $entry) {
$table .= '<tr>';
$table .= '<td>'.$entry['created_at'].'</td>';
$table .= '<td>'.$entry['user']['display_name'].'</td>';
$table .= '<td><a class="your-custom-class" href="'.$entry['_links']['self'].'">View channel</a></td>';
$table .= '</tr>';
}
$table .= '</table>';
echo $table;

Populating Drop-Down in PHP using Array

I am trying to populate a drop-down menu with values from an array.
I have tried to follow other answers but the syntax doesn't seem to be working. (I'm still relatively new to PHP).
The following code which I am working on was produced by someone else.
$sqlite_query = "SELECT * FROM dis_kind";
$result = $db->query($sqlite_query);
$array = $result->fetchArray();
$output = "<select name=\"kind\" class=\"dis\" >\n";
$output .= "<option value=\"$this->wildcard_value\"></option>\n";
foreach ($result as $array) {
$value = $array['kind'];
$output .= "<option value=\"";
$output .= $value;
$output .= "\">";
$output .= $value;
$output .= " - ";
$output .= $array['description'];
$output .= "</option>\n";
}
$output .= "</select>\n";
I don't know why it has been done the way it has but I am stumped as to getting my drop-down to work.
Currently, the box appears but is populated with no values.
Thanks.
Eventually managed to solve the problem myself - before I pulled my hair out!
Instead of using the foreach loop, I used the following:
while($array = $result->fetchArray())
{
// Output code.
}
Which populated the drop-down with values from the array.

Build php response for ajax

I'm having a logical problem with my script. The point would be to get a some rows formatted in a table but the header should not be repeated and under all the items should be outputted and than as variable passed to ajax . But I don't see how to solve this.
function abc()
{
global $mainframe;
$db =& JFactory::getDBO();
// Check for request forgeries
if(isset($this->message)){
$this->display('message');
}
// custom: generate token for ajax request
$ajax_token = JHTML::_( 'ajax.token' );
// custom end
// JRequest::checkToken( 'get' ) or jexit( 'Invalid Token' );
$letter_raw = JRequest::getVar('val');
$letter = substr($letter_raw, -1);
$response = '<div class="no-rec">not found</div>';
$html = '';
if (!empty($letter)) {
$query = " SELECT * FROM #__glossary WHERE substr(tterm,1,1) LIKE '$letter%'";
$db->setQuery( $query );
$rows = $db->loadObjectList();
if (count($rows)) {
$header='<table class="stripeMe"><tbody><thead><tr><th>Begriff</th><th>Definition</th></tr></thead><tr>';
foreach($rows as $key => $row) {
$body='<td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr></tbody></table>';
}
$response = $header.$body;
}
$html = $response;
echo $html;
}
}
What exactly is the problem? :)
You probably should not make it a function, since I guess you are just going to load the content in with AJAX?
And you should ADD to the string not override it in each row.
UPDATED, FIXED HTML ERRORS
if (count($rows)) {
// CREATE TABLE AND HEAD
$body = '<table class="stripeMe"><thead><tr><th>Begriff</th><th>Definition</th></tr></thead>';
// TBODY FOR REPEAT INSIDE
$body .= '<tbody>'
foreach($rows as $key => $row) {
$body .= '<tr><td><span class="title">'.$rows[$key]->tterm.'</span></td><td>'.$rows[$key]->tdefinition.'</td></tr>';
}
$body .= '</tbody></table>';
$response = $body;
}
$html = $response;
echo $html;
Well if you are passing the data back as HTML than this will work so your jquery would be:
$('#holderdiv').load('abc.php');
If you are using Something like .post .ajax .get, then you will need to decide what format to pass the data back with so if it's JSON then you will need to format it as such and make sure that your jQuery is told to expect such a response. I can give more help if you can be specific about your situation and what problems you are having.

Read returned XML by function with SimpleXML

This is sort of confusing to explain, so thank you ahead of time for bearing with me.
I am using Kohana PHP framework to develop an application. I have a model function that accepts parameters for a search, and should return an XML styled page. I need this to be read by the controller with SimpleXML. Any ideas how to do this?
$o = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$o .= "<feed>\n";
$o .= "\t<search_phrase>$q</search_phrase>\n";
if(isset($entries)){
uasort($entries, 'compare_weight');
/**
* Build the xml data
*/
foreach($modules as $module){
$o .= "\t<search_location>$module</search_location>\n";
}
foreach($entries as $k=>$entry){
$o .= "\n\t<entry>\n";
$o .= "\t\t<title>$entry[title]</title>\n";
$o .= "\t\t<url>$entry[url]</url>\n";
$o .= "\t\t<weight>$entry[weight]</weight>\n";
$o .= "\t\t<module>$entry[module]</module>\n";
if($entry['owners']){
foreach($entry['owners'] as $owner){
$o .= "\t\t<owners>\n";
$o .= "\t\t\t<owner_id>$owner[owner_id]</owner_id>\n";
$o .= "\t\t\t<owner_name>$owner[owner_name]</owner_name>\n";
$o .= "\t\t\t<profile_link>$owner[profile_link]</profile_link>\n";
$o .= "\t\t</owners>\n";
}
}
$o .= "\t</entry>\n";
}
}else{
$o .= "\t<noresult>true</noresult>\n";
}
$o .= "</feed>\n";
return $o;
The controller functions like this... It's the closest I'm able to come to wrapping my head how to do this.
$return= $this->search->search($_GET);
$xml = new SimpleXMLElement($return);
die($xml);
It returns a blank document with 44 blank lines. Any direction would be helpful.
insted of using the constructor of simpleXML try using the simplexml_load_string() function
Try changing the return of your $xml feed to:
return echo $o;

Categories