This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
i have the following code:
<?php
$events = Event::with('skoler')->get();
$skoleIsSet = $_GET['skoleId'];
$eventIsSet = $_GET['event'];
if($skoleIsSet && !$eventIsSet) {
foreach($events as $event) {
if($event['skole_id'] == $skoleIsSet) {
***$img = $event['img_url'];***
echo '<div id="events">';
***echo '<img src="$img" height="100" width="100"/>';***
echo '<h1>' . $event['title'] . '</h1>';
echo '<p>Beskrivelse: ' . $event['description'] . '</p>';
echo '<p>Pris: ' . $event['pris'] . '</p>';
echo '<p>Dato: ' . $event['date'] . '</p></div>';
}
}
}
else {
foreach ($events as $event) {
if ($event['skole_id'] == $skoleIsSet) {
$bg = $event['img_url'];
'<div id="events">';
echo '<h1>' . $event['title'] . '</h1>';
echo '<p>Beskrivelse: ' . $event['description'] . '</p>';
echo '<p>Pris: ' . $event['pris'] . '</p>';
echo '<p>Dato: ' . $event['date'] . '</p></div>';
}
}
?>
I am supposed to show all events, with a picture. The url for the pictures is in the img_url column in my db. What is wrong with line number 8 and 10? The strong font option is not working, but the lines im referring to is marked with 6x *'s.
Thanks!
echo '<img src="$img" height="100" width="100"/>';
You cannot include a variable directly into a string, if you're using single quotes. Either use double quotes (and escape the double quotes inside the string), or interrupt your string:
echo '<img src="' . $img . '" height="100" width="100"/>';
echo "<img src=\"$img\" height=\"100\" width=\"100\"/>";
Related
Forgive me if this is simple but I have a for each loop that searches JSON data for results from a search. I then have some preg_match statements that will look at some of the tags within the JSON and if their is a match display the thumbnail in a div. and currently this all works. But it currently displays every result in its own Div and i want just five divs with multiple images within if there is a match.
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
if(preg_match("/_cm/i",$target)) {
echo '<div id="div5">';
echo '<h2>Creative</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SS/i",$target)) {
echo '<div id="div6">';
echo '<h2>Styled</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SH/i",$target)) {
echo '<div id="div7">';
echo '<h2>Group</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/still life/i",$target1) && preg_match("/_sm_00|_sd_00/i",$target)) {
echo '<div id="div8">';
echo '<h2>Cutout</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/worn/i",$target1)) {
echo '<div id="div9">';
echo '<h2>Worn</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
}
I cant quite figure out how to accomplish this, would it be the case of putting the results into an array and then displaying the results within the div?
Any help would be greatly appreciated.
You are right and answered the question yourself :)
Collect the results in a first step and create the markup in a second step. Something like this will do it:
$creative = [];
$styled = [];
/* ... */
function getHitInfos($theHit)
{
return [
"url" => $theHit->thumbnailUrl,
"name" => $theHit->metadata->baseName
];
}
function printResults($results, $title) {
echo '<div>';
echo '<h2>'.$title.'</h2>';
foreach ($results as $key => $val) {
echo "<img src='" . $val["url"] . "' alt='error'>";
echo $val["name"];
}
echo '</div>';
}
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
echo '<div>';
if(preg_match("/_cm/i",$target)) {
$creative[] = getHitInfos($hit)
}
if(preg_match("/_SS/i",$target)) {
$styled[] = getHitInfos($hit)
}
/* ... */
}
printResults($creative, "Creative");
printResults($styled, "Styled");
/* ... */
Disclaimer: My last contact with php is some years ago, but I hope you will see the point here.
(I also created some helping functions to DRY the code)
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 :(
I need some help with solving duplicating problem. To deal:
I have 2 filed custom meta
It displays this way :
Only 2 image placed, but it duplicate <li>.
First and third <li> is only video, second and fourth works fine, looks like this :
foreach($slider_xml->childNodes as $slider){
$test_test = get_post_meta(9,'test_1', false);
foreach($test_test as $test_test){
echo '<li class="img-vid-slide">';
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
}
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
$image_full_url = wp_get_attachment_image_src(find_xml_value($slider, 'image'), 'full');
echo '<a href="' . $image_full_url[0] . '" data-rel="prettyPhoto[flexgal]" title="" >';
}else if($link_type != 'No Link'){
echo '<a href="' . $link . '" >';
}
echo '<img class="'. $i .'" src="' . $image_url[0] . '" alt="' . $alt_text . '" />';
echo '</div>';
if( !empty($title) ){
echo '<div class="flex-caption gdl-slider-caption">';
echo '<div class="gdl-slider-title">' . $title . '</div>';
echo '<div class="slider-title-bar"></div>';
echo $caption;
echo '</div>'; // flex-caption
}
if($link_type != 'No Link'){
echo '</a>';
}
echo '</li>';
}
I find solution.
Filed custom fields: prntscr.com/7oqqvt
I named them like "name_1","name_2"..."name_4".
$i is equal of last nubmer in custom field.
Result on screen: prntscr.com/7oqsaw
$i = 1;
while ($i <= 3):
foreach( $slider_xml->childNodes as $slider ){
// ======= SAME AS FIRST ====== //
$test_test = get_post_meta(9,'test_'. $i .'', true);
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
// ======= SAME AS FIRST ====== //
}
echo '</li>';
$i++;
}
endwhile;
Works fine for me, do what i searching for!
Thanks, Greg, for a little hint, but i go for "while" instead of "for"! Good luck
Don't use the a foreach inside a foreach loop. It a function problem.
Use the alternative "for" loop instead of the double foreach.
Ok, So Im creating a while loop for multiple accordions in php. I marked up some php code that ALMOST works. The problem I am having is that I had to nest five different IF statements in a while loop for the five different accordions. The If statements contain the content to be in the accordion. The content is a list of songs in a particular album. What I need to happen is for the If statement containing the content for one accordion to break, but still continue with the original while statement and do this for each accordion. Here is my PHP Code.
if ($result)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body"> <ol>';
if($result1)
{
while ($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC))
{
echo '<li>' . $row1['Name'] . '</li>';
};
};
if($result2)
{
while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC))
{
echo '<li>' . $row2['Name'] . '</li>';
}
};
if($result3)
{
while($row3 = mysqli_fetch_array($result3, MYSQLI_ASSOC))
{
echo '<li>' . $row3['Name'] . '</li>';
}
};
if($result4)
{
while($row4 = mysqli_fetch_array($result4, MYSQLI_ASSOC))
{
echo '<li>' . $row4['Name'] . '</li>';
}
};
if($result5)
{
while($row5 = mysqli_fetch_array($result5, MYSQLI_ASSOC))
{
echo '<li>' . $row5['Name'] . '</li>';
}
};
if($result6)
{
while($row6 = mysqli_fetch_array($result6, MYSQLI_ASSOC))
{
echo '<li>' . $row6['Name'] . '</li>';
}
};
echo '</ol>';
echo '<h3>available at:</h3><a href=' . $row['Location'] . '>iTunes</a>
<a href=' . $row['Location2'] . '>Amazon</a>
<a href=' . $row['Location3'] . '>United Interests</a></div>';
echo '</div></div></div></div>';
}
mysqli_free_result ($result);
}
else
{
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Any insights on how to make this work for me would be greatly appreciated. I cant use Jquery for the accordion because of how the code was previously structured.
use continue http://php.net/manual/en/control-structures.continue.php
continue is used within looping structures to skip the rest of the
current loop iteration and continue execution at the condition
evaluation and then the beginning of the next iteration.
Note: Note that in PHP the switch statement is considered a looping
structure for the purposes of continue.
also, you should probably use a switch instrad of multiple if conditions
Can someone help me with this, if you go to: https://developers.facebook.com/tools/explorer and use Graph API, GET Method on Connections->Home - You can grab your news feed.
In that newsfeed, the array shows some information based on each individual feed item.
One thing on there is likes which has a sub value of count which counts the number of likes.
Problem is, when I change it to use my applications and access token, the information is still there, expect the count part.
This is really confusing, I have read_stream as a scope.
My code the fetch the likes is:
if(isset($news['likes']['count'])) $likes_count = $news['likes']['count'];
else $likes_count = 0;
Which is part of my foreach loop.
For good measure, here is the function:
function newsitem($profile_pic,$from,$to,$message,$picture,$name,$link,$caption,$description,$icon,$time,$comments,$likes)
{
if($to) $to_section = '<div class="to" >to</div><div class="news-friend-name"><strong><a>' . $to . '</a></strong></div><div class="clear"></div>';
else $to_section = '';
if($message) $message_section = '<div class="message">' . $message . '</div>';
else $message_section = '';
if($picture) $picture_section = '<div class="external-image"><img src="' . $picture . '"/></div><div class="news-external">';
else $picture_section = '<div class="news-external" style="width: 410px;">';
if(!$link) $link='#';
if($name) $name_section = '<div class="news-title"><h3>' . $name . '</h3></div>';
else $name_section = '';
if($caption) $caption_section = '<div class="news-caption"><i>' . $caption . '</i></div>';
else $caption_section = '';
if($description) $description_section = '<div class="news-desc">' . $description . '</div>';
else $description_section = '';
if($icon) $icon_section = '<div class="news-icon" ><img src="' . $icon . '" /></div>';
else $icon_section = '';
$time_converted = time_elapsed($time);
$news = '<div class="news">
<div class="news-friend-thumb"><img src="' . $profile_pic . '"/></div>
<div class="news-content">
<div class="news-friend-name"><strong><a>'. $from . '</a></strong></div>'.$to_section.
'<div class="clear"></div>' . $message_section . $picture_section . $name_section . $caption_section . $description_section .
'</div>
<div class="clear"></div>
<div class="comment-like">' . $icon_section . $time_converted . ' ago • ' . $comments . ' comments • ' . $likes . ' likes</div>
</div>
</div>';
return $news;
}`
I'd appreciate the help, thanks!