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)
Related
I am having problem in fetching data from database table, after loading data into array and loop using foreach it return blank instead of the values. I have tried using this way $row[''] it works fine but i want to use this operand -> but its fetching bank data.
$query = "SELECT * FROM pages_words";
$select_posts = mysqli_query($connection, $query);
$rows[] = array();
while ($row = mysqli_fetch_assoc($select_posts))
$rows[] = $row;
foreach ($rows as $key => $row) {
$idpw = $row['idpw'];
$pages = $row['pages'];
$words = $row['words'];
$amount_added = $row['amount_added'];
$page_type = $row['page_type'];
if ($page_type == 'double') {
if ($row->idpw == $post['no_of_pages']) {
echo '<option selected="selected" id="' . $row->idpw . 'nop" value="' . $row->idpw . '" title="' . $row->amount_added . '"> ' . $row->pages . ' Page(s) / ' . $row->words . ' Words</option>';
} else {
echo '<option id="' . $row->idpw . 'nop" value="' . $row->idpw . '" title="' .
$row->amount_added . '"> ' . $row->pages . ' Page(s) / ' . $row->words . ' Words</option>';
}
}
}
?>
blank data
kindly help solve the problem
You used mysqli_fetch_assoc(), which fetches rows as associative arrays. In other places in the foreach loop, you're accessing the row correctly using array syntax, such as $idpw = $row['idpw'];. You can't access array values using object syntax. Instead of using $row->idpw when outputting the option, you could either use $row['idpw'] or just use the $idpw variable you created earlier.
If you want the arrow operator to work, you'll need to use mysqli_fetch_object() instead, but if you do that you'll need to change the places where you're using array syntax to use object syntax as well.
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..
I need some help with echoing a <img> tag with attributes like title, rel, and class.
I have made it this far, when I'm echoing a filename from a db to search in a catalogue to find it. But I'm not sure how to write some attributes to it since I'm going to display it with Pirobox.
This is what i got working:
echo '<a href="uploads/'.$row['bildnamn'].'">';
echo '<img src="uploads/'.$row['thumb_bildnamn'].'">';
echo '</a>';
But I also need these attributes for the <A> tag which makes the image large.
rel="gallery" class="pirobox_gall" title="$row['uploaded']" . " " . "$row['user']";
What I don't get to work is how to get that line together with:
echo '<a href="uploads/'.$row['bildnamn'].'">';
You should be able to concatenate everything in the <a> tag like so:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';
I inserted spaces to help emphasize where PHP does concatenation. In your case, a single quote starts/ends the string for PHP; a double quote is ignored and goes into the HTML. So this part:
title="' . $row['uploaded'] . ' ' . $row['user'] . '"
will make the title be the value of the uploaded column, then a space, then the value of the user column. Then just end the a tag with a >.
you could continue to concatenate the string
echo '<a href="uploads/'.$row['bildnamn'].'"'. 'rel="gallery" class="pirobox_gall" title="'.$row['uploaded'].' '.$row['user'].'">';
Try this:
echo '<a href="uploads/' . $row['bildnamn'] . '" rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
echo '<img src="uploads/' . $row['thumb_bildnamn'] . '">';
echo '</a>';
You can do this using string concatenation, like this:
$anchor = '<a href="uploads/'.$row['bildnamn'].'"';
$anchor .= 'rel="gallery" class="pirobox_gall" title="' . $row['uploaded'] . ' ' . $row['user'] . '">';
$anchor .= '<img src="uploads/'.$row['thumb_bildnamn'].'"></a>';
echo $anchor;
I am really new to PHP programming, and I'm frustrated because I think this should work and it doesn't. I'm really missing something...
<?php
foreach ($datas as $name)
{
if ($name['state'] === 'MA')
{
echo
'<input type="hidden" name="id" value="' . $name['id'] . '" />' .
'<h2>' . htmlentities($name['name']) . '</h2>' .
'<p>' .
htmlentities($name['description']) . ' ' .
...
'<h1>' . $name['id'] . '</h1>';
foreach ($commentdatas as $name2)
{
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
}
?>
</div>
Everything worked well until I got to the second foreach statement. The foreach works. When I test, $name['id'] echos properly, as does $name2['parkid'].
There doesn't seem to be a problem with the system correctly identifying these, even in the loop. But inside the if statement, nothing echos at all.
Obviously something is wrong with the if statement. I've looked everywhere I can find and I'm having trouble figuring out the proper way to compare these variables. Can anyone help?
Try in_array() function
foreach ($commentdatas as $name2) {
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
replace with
if(in_array($name['id'],$commentdatas)){
echo $name['id'] . $name2['parkid'];
}
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>';