Syntax problem with html in PHP - php

im getting a syntax error on this line of code and don't know the correct formatting for it.
This is the part i'm having problems with -
echo "
<ul>";
foreach($photos as $photo) {
$farm = $photo['farm'];
$server = $photo['server'];
$photo_id = $photo['id'];
$secret = $photo['secret'];
$photo_title = $photo['title'];
<li><img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>
The problem is with that li tag. How can i format it properly?

Per the comments, you probably mean to have the following for your last line:
echo '<li><img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>';

The <li> part should be quoted. Try:
echo "
<ul>";
foreach($photos as $photo) {
$farm = $photo['farm'];
$server = $photo['server'];
$photo_id = $photo['id'];
$secret = $photo['secret'];
$photo_title = $photo['title'];
echo '<li><img src="http://farm' . $photo['farm'] . 'static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_t.jpg" alt="' . $photo['title'] . '" ></li>';
}
echo '</ul>';

Related

How can I combine two arrays for a loop?

I'm stuck on this for a while now: I'm trying to combine two arrays, one is every image in a folder, and the other one is the description of the picture from the SQL Database. In the database I've added the filename and the apartment name. To make my query work I need it to be in a loop, but to show the results of my query, it needs to become a loop, so I would think it must be possible to combine it right? But I can't seem to figure out how to combine these two arrays in a loop. I've tried with two loops and this is what I've got:
<?php
$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
foreach ($images as $image) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
$titles = mysqli_query($link, $sqlaa);
while ($row = $titles->fetch_array()) {
echo '<form action="" method="post"><li>
<a href="' . $image . '" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="' . $image . '" />
</a>
</li>';
}
}
?>
If anyone knew, that'd be great!
Thanks in advance
You can try making the loop over the data and then check the image in the loop, this could be faster and avoids using a query inside a loop.
<?php
$directory = "images/photos/";
//$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$sqlaa = "SELECT filename,apartment FROM `afbeelding`";
$titles = mysqli_query($link, $sqlaa);
while($row = $titles->fetch_array())
{
$image=$row["filename"];
if (file_exists($directory.$image)){
echo '<form action="" method="post"><li>
<a href="'.$image.'" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="'.$image.'" />
</a>
</li>';
}
}
?>
I don't know if the image is stored using the full relative path "images/photos/img.png" or the base filename only "img.png".
If the data in afbeelding is huge, you can improve the sql query using an IN filter
function imageNames($item){
return "'$item'";
}
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$names=array_map("imageNames",$images);
$filter_names=implode(",",$names);
$sqlaa = "SELECT filename,apartment FROM `afbeelding` WHERE filename IN ($filter_names)";
And then proceed with the loop, without checking files.
How about... going about it differently.
You already have the filenames in database.
So just poll your database with your available data, and then just echo it.
I'm assuiming it's stored as images/photos/filename.jpg in your database.
But without a data sample as is in your database I can't speculate more.
$directory = "images/photos/";
foreach ($images as $image) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename like '%images/photos/%'";
$titles = mysqli_query($link, $sqlaa);
while ($row = $titles->fetch_array()) {
echo '<form action="" method="post"><li>
<a href="' . $image . '" title="' . $row['apartment'] . '" >
<img style="width:150px;height:150px;" src="' . $row['filename'] . '" />
</a>
</li>';
}
}
first: loops on db-calls are not really best practice..
A possible result to your question:
<?php
$directory = "images/photos/";
$images = glob('images/photos/*.{jpg,png,gif}', GLOB_BRACE);
$images = array_flip($images);
// get data first
foreach($images as $image => $v) {
$sqlaa = "SELECT * FROM `afbeelding` WHERE filename = ' . $image . '";
$titles = mysqli_query($link, $sqlaa);
if ($title->num_rows == 1) {
$row = $titles->fetch_array();
$images[$image] = $row['apartment'];
} else {
$images[$image] = '';
}
}
// render data
foreach ($images as $filename => $description) {
echo '<form action="" method="post"><li>
<a href="'.$filename.'" title="' . $description . '" >
<img style="width:150px;height:150px;" src="'.$filename.'" />
</a>
</li>';
}
?>
Not really nice, but plain simple..

Dynamic link with absolute URI gets changed to localhost

$refPointer = $site_to_be_extracted . $a->href;
echo $refPointer . '<br>';
generates in output (Chrome browser):
http://www.hackingwithphp.com/1/1/0/is-this-book-for-you
You would expect the following line to generate a correct <a> tag. That is a correct link
print '<a href=' . $refPointer . '>' . $a . '</a>';
Instead it generates in output:
localhost/1/1/0/is-this-book-for-you
So both the protocol (HTTP) and the $site_to_be_extracted (www.hackingwithphp.com) are lost. And replaced by "localhost"
Why? What could be the problem
This is the function where it's all happening. Now it works fine
include('simple_html_dom.php');
.
.
function createChapterContent ($site_to_be_extracted, $chapter_to_be_extracted) {
$html = file_get_html($chapter_to_be_extracted);
$refPointer;
foreach($html->find('ol') as $ol) {
foreach($ol->find('li') as $li) {
// echo '<br>' . $li->innertext ;
foreach($li->find('a') as $a) {
// Original. Causing the reported problem
// $refPointer = $site_to_be_extracted . $a->href;
// echo $refPointer . '<br>';
// changed to (see below). Now the output is correct
// Why?
$a->href = $site_to_be_extracted . $a->href;
$refPointer = $a->href;
print '<a href=' . $refPointer . '>' . $li->innertext . '</a>' . '<br>';
break;
};
}
}

limit results in foreach loop

The code below I have works fine but I wanted to check with you, the experts, to make sure I was using best practices.
I want to limit the loop results to 16. Does the code below seem like the best method?
Thanks,
Jeffrey
foreach ($flickr_set['items'] as $id => $photos) {
$ctr=0;
foreach ($photos as $photo) {
if($ctr>=16) break; else $ctr++; /* limits results to 16 */
echo '<a href="' . $photo['large'] . '" title="' . $photo['title'] . '" rel="flickr-set" ><img src="' . $photo['thumb'] . '" /></a>';
}
}
Your solution is fine, if you want a more structured solution which might be also more understandable, you can use array_slice:
foreach ($flickr_set['items'] as $id => $photos) {
foreach (array_slice($photos, 0, 16) as $photo) {
echo '<a href="' . $photo['large'] . '" title="' . $photo['title'] . '" rel="flickr-set" ><img src="' . $photo['thumb'] . '" /></a>';
}
}
Your code is fine but..
I woudn't check if it is higher or equal i would only check if it equal like this:
if($ctr == 16)

Php Xpath query returning "Array"

ive got my xml file and also heres my php script
$db = simplexml_load_file("BIN/videos.xml");
$id = $_GET['id'];
$tq = "//video['#id=" . $id . "']/title[0]";
$dq = "//video['#id=" . $id . "']/description[1]";
$eq = "//video['#id=" . $id . "']/embed[2]";
$title = $db->xpath($tq);
$description = $db->xpath($dq);
$embed = $db->xpath($eq);
include("design/lyt.php");
echo $embed . '<br>
<h1>' . $title . '</h1>
<p>' . $description . '</p>';
?>
Its supposed to display "Test" for them all! but it says "Array"
Access the elements of the array returned from xpath...
PHP >= 5.4:
$title = $db->xpath($tq)[0];
PHP < 5.4:
Update PHP :-)
or
list($title,) = $db->xpath($tq);
or
$title = $db->xpath($tq);
$title = $title[0];

Need to add second line and third delimiter on php explode

This is tailing off my other question which was successfully answered: stackoverflow.com/questions/8597929/need-to-create-li-with-list-of-different-links-using-php-explode-method
so now I have this:
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
and that, using the following in a textarea which is defined to "my_custom_output":
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
and the result is
text1
text2
text3
which are successfully linked and styled. (i didnt make them links because stackoverflow doesn't let me post more than two links because i only have 3 rep).
So my next and final desired task is to do this:
text1
description 1
text2
description 2
text3
description 3
where the text1 etc are linked like before but the descriptions are not linked.
So I will do my best, right here in stackoverflow, to try it. However, I expect I will need some help. Let's go:
<?php
$separator1 = "\n";
$separator2 = ":";
$separator3 = ";";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
and to use the following in the textarea which is is defined to "my_custom_output":
text1:text1-page-url;Text1 Description
text2:new-text2-page;Description For Text2
text3:different-page-text3;A Text3 Description
and I need the output to be:
text1
Text1 Description
text2
Description For Text2
..etc
I don't know if semicolon will work, but I can't use a space (\s) because there are spaces in the description. I am open to suggestions.
====================================================
MY NEWEST TRY:
<?php
$separator1 = "\n";
$separator2 = ":";
$separator3 = ";";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$itemarray = explode($separator2, trim($item));
$item_text = $itemarray[0];
list($item_links, $item_desc) = explode($separator3,$itemarray[1]);
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
IT WORKS!!! =D
Not sure if I understand this well enough (I'm not sure what get_custom_field() gets you - can't find that as a regular PHP function), but when you explode an item that has multiple instances of the delimiter, you'll get multiple arrays.
So:
$textarea = "text1:text1-page-url;Text1 Description";
$data = explode(':',$textarea);
// at this point $data[0] will contain "text1", while $data[1] contains text1-page-url;Text1 Description"
$descarray = explode(';',$data[1]);
// then $descarray[0] contains "text1-page-url" and $descarray[1] contains "Text1 Description" so you can echo this out however you like.
To work with your code..
Assume each $item is each row at this point, like this:
$item = "text1:text1-page-url;Text1 Description";
Then this will do the trick:
foreach ($array as $item) {
$itemarray = explode($separator2, trim($item));
$item_text = $itemarray[0];
list($item_links, $item_desc) = explode(';',$itemarray[1]);
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
I'd slightly optimize the algorithm as there is no need for second explode(). Just separate substrings within one row with the same separators (and don't forget to escape that separator inside all your data):
<?php
$row_separator = "\n";
$line_separator = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($row_separator, $textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links, $item_desc) = explode($line_separator, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And here is the data format I suggest to use (separate all fields by :)
text1:text1-page-url:Text1 Description
text2:new-text2-page:Description For Text2
text3:different-page-text3:A Text3 Description

Categories