Want to get contents inside a div via php - php

Okay, so I been trying to get specific content from different div classes to put in a table. I'm am customizing my wordpress site. Here is the code I'm having trouble with
//loops through posts
while ( have_posts() ) : the_post();
$doc = new DomDocument;
$doc ->validateOnParse = true;
$link = " ' " . get_permalink() . " ' ";
//this is where the trouble lies. get error
/*Warning: file_get_contents( ' http://www.uwarc.org/?p=60 ' ) [function.file-get-contents]: failed to open stream: No such file or directory in /home/uwarco5/public_html/wp-content/themes/thesis_185/custom/custom_post_loop.php on line 98
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Empty string supplied as input in /home/uwarco5/public_html/wp-content/themes/thesis_185/custom/custom_post_loop.php on line 98 */
$doc->loadHtml(file_get_contents( $link ));
echo "<tr onclick=" . $quote . "DoNav(". $quote1 . "get_post_class('site', null) " . $quote1. ");" . $quote . ">" ;
echo "<td align =" . $quote . "center" . $quote . " >" ;
echo "<div id =". $quote . "org_logo" . $quote . "> <img src =". $quote ;
var_dump($doc->getElementByClass('img')) ;
echo $quote . "width = ".$quote. '150' . $quote. "; </div> " ;
the_title();
echo "</td>";
echo "<td>" . var_dump($doc->getElementByClass('courses')) . "</td>";
echo "<td>";
var_dump($doc->getElementByClass('description'));
echo "</td>";
echo "</tr>
endwhile;?>

Load the post content:
$doc->loadHTML( apply_filters( 'the_content', get_the_content() ) );

Related

Array: Compare values from API with values from own database

I have built a list (moviesSeen) where people can save their seen movies.
Now I want to get the director for each movie from an API and then compare which movies have already been seen. The output of the movies works without problems, but I can't get the result from the database (moviesSeen) to match the movies of the director. How can I achieve this?
The goal of the script is to show all movies of a director and mark all the ones you have already seen.
function getCrewDetails() {
global $apiKey;
global $language;
$personID = htmlspecialchars($_GET['personID']);
$url = "https://api.themoviedb.org/3/person/" . $personID . "?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personPrivate = json_decode($data); // decode the JSON feed
echo '<div class="row">';
echo '<div class="col">';
echo '<img src="https://image.tmdb.org/t/p/w300/' . $personPrivate->profile_path . '" class="img-thumbnail">';
echo '</div>'; // end div col
echo '<div class="col-8">';
echo '<h1>' . $personPrivate->name . '</h1>';
if (!empty($personPrivate->biography)) {
echo $personPrivate->biography;
} else {
echo '<p>Verdammmt! Zu dieser Person liegen keine biographischen Details vor.</p>';
echo '<p>Dabei hätte sie es doch so verdient :(</p>';
}
echo '</div>'; // end div col
echo '</div>'; // end div row
// echo '<pre>';
// print_r($person);
$url = "https://api.themoviedb.org/3/person/" . $personID . "/movie_credits?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personCareer = json_decode($data); // decode the JSON feed
echo '<div class="container mt-4">';
echo "<h2>Filmographie</h2>";
echo '<table class="table table-striped table-sm">';
echo '<thead>';
echo '<tr>';
echo '<th scope="col">Film</th>';
echo '<th scope="col">Gesehen</th>';
echo '<th scope="col">Funktion</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM movieSeen WHERE user_id = '" . $_SESSION['id'] . "'");
$stmt->execute();
foreach ($stmt as $row ) {
echo $row['movie_id'] . ', ';
} // output of the saved IDs in movieSeen
foreach ($personCareer->crew as $showCrewDetails) { //Output of the movies
echo '<tr>';
echo '<td>' . $showCrewDetails->title . ' ' . $showCrewDetails->id . ' (' . substr($showCrewDetails->release_date, 0, 4) . ')</td>';
echo '<td><span class="badge badge-danger">Movie Seen</span></td>';
echo '<td>' . $showCrewDetails->job . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
//echo '<pre>';
//var_dump($personCareer);
}
I know that I have to edit the last foreach loop, but I just can't get it to work :(

Woocommerce change Attribute when empty

I do have that code in my functions.php for my Product Overview Site:
add_action('woocommerce_shop_loop_item_title', 'wotom_show_attributes_title', 15);
function wotom_show_attributes_title()
{
global $product;
$ab1 = $product->get_attribute ('pa_weingut');
$ab2 = $product->get_attribute ('pa_rebsorte');
$ab3 = $product->get_attribute ('pa_dac-lage');
$ab4 = $product->get_attribute ('pa_jahrgang');
$ab5 = $product->get_attribute ('pa_region');
echo "<h1 class='shoppage_header1'>" . __($ab1,'woocommerce') . "</h1>";
echo "<h1 class='shoppage_header2'>" . __($ab2,'woocommerce') . "</h1>";
echo "<h2 class='shoppage_sub1'>" . __($ab3,'woocommerce') . "</h2>";
echo ($ab5) ? "<h2 class='shoppage_sub2'>" . __($ab4,'woocommerce') . "</h2>" : "";
}
Updated code!
but the echo($ab5) ? ... isnt working.

PHP -displaying 12 items of an associative array with specific key and value

<?php
foreach($catalog as $id=> $item) {
echo get_item_html($id,$item);
}
// here is the functions page code :
function get_item_html($id,$item){
$output = "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>View Details</p>"
. "</a></li>";
return $output;
}
?>
I have used it for displaying product categories i checked several time but I did find any error but it display undefined variable
please help me to find out this solution

Dependency of HTML rendering on function's return value that executes later?

I have a function that prints "Our data" first and then inside a loop checks for all the data in the database and then prints the data.
However, if there is no data, it shouldn't print "Our data", how do I achieve this since "Our data" is already printed?
The layout is as follows :
<h1> Our Data </h1>
<p> <!-- DATA --> </p>
The function is as follows:
function cpt_info($cpt_slug, $field_name, $title) {
echo "<h1>Our" . $title . "</h1>"; //here I want to print our data
$args = array('limit' => -1);
$cpt = pods($cpt_slug, $args);
$page_slug = pods_v('last', 'url');
echo "<h1 class='projects-under-" . $cpt_slug . "'>" . $title . "</h1>";
if ($cpt->total() > 0) :
while ($cpt->fetch()) :
if (!strcasecmp($cpt->field('post_name'), $page_slug)) :
$data = $cpt->field($field_name);
foreach ((array) $data as $key => $value) {
if ($value != null) : //here I check whether data is empty
$url = wp_get_attachment_url(get_post_thumbnail_id($value['ID']));
echo "<div class='col-sm-3 col-xs-6'><div class='border-to-circle'>";
echo "<div class='circle-container' style='background: url(" . $url . ")center center, url(" . get_template_directory_uri() . '/images/rwp-banner.jpg' . ")center center;'><h4><a href='" . get_permalink($value['ID']) . "'>" . $value['post_title'] . "</a></h4></div>";
echo "</div></div>";
endif;
}
endif;
endwhile;
endif;
}
Something like below:
if(count($data)){
echo '<h1> Our Data </h1>';
}

Database driven PHP menu with check for current page

I have a page with a persisted left nav that is populated based on a database. I need to visually mark the menu entry that corresponds with the content currently presented on the right side of the page. I am having problems getting the check of current page to work. (I am learning PHP and attempting to edit someone else's code, person long gone.)
Here is the code for the menu:
while( ( $row = mysql_fetch_array( $result ) ) && ( $count < $limit ) ) {
$count++;
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" >" . stripslashes( $row['title'] ) . "</a></li>\n";
..etc. Works fine to generate menu list.
Then I think what I want to do is to compare the URI this code produces with the currently loaded page, to determine if I should add a CSS style for current page or not.
So I attempted this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == "$this" ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Got syntax errors. So tried this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == $this ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Still syntax errors. So tried this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == $row['id'] ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Suggestions?
Success at last. I found a way to get this to work, though the code may be less than elegant.
I could not get the comparison of the URI and the $row['id'] to work within the echo statement. So I created a separate function to do the comparison and returned the result to the echo statement as follows:
echo "\t\t\t<li>" . stripslashes( $row['title'] ) . "</li>\n";
function checkRow($myID)
{
if( strstr( $_SERVER['REQUEST_URI'], $myID ) )
{
return " class=\"selected\"";
}
}
You have php tags in your echo statement in your first example
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == "$this" ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
try this
echo "\t\t\t<li><a href=\"".NEWS_URL."?show=news&action=show&id=".$row['id']."\". ($_SERVER['REQUEST_URI'] == $this ?"class=\"selected\"" :"").stripslashes( $row['title'] )."</a></li>\n";

Categories