I am sure this is a simple principle of PHP, however it is one I am yet to learn. In principle the code works:
<?php
for($i=0;$i<count($photos);$i++){
if($photos[$i]->image != ""){
if(JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photos[$i]->image){
if(!$photocount) {
$photocount = $photocount + 1;
$photoclass = "property_photo_main property_photo_main_" . $photocount; // First Photo Class
}
?>
<img src="<?php echo JURI::root()?>images<?php echo $row->id;?>/medium/<?php echo $photos[$i]->image?>" class="<?php echo $photoclass; ?>" alt="<?php echo $photos[$i]->image_desc;?>" title="<?php echo $photos[$i]->image_desc;?>"/>
<?php
}
}
}
?>
That outputs the images correctly, however the "Photo Count" does not increase and thus each photo gets the "First Photo Class" (property_photo_main_1). I fully appreciate that the problem here is because the count is not within the loop to print each photo, but as that is directly before that image output, where is the loop, and how can I implement that the count increases?
The HTML Output is:
<img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100282240dc03c72cb44ce05eb9e56021d0c05.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
<img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283f9f748ca556070c2d09553298dc26d8f.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
<img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283b280e25f329d8cf1518bda4700b07765.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
<img src="http://msa.eighttwentydesign.com/images/osproperty/properties/5/medium/51384100283c801f9afb73308c7fd77a77ea00129bb.jpg" class="property_photo_main property_photo_main_1" alt="" title=""/>
</div>
You never increment $photocount outside of that if. Also you never reset $photoclass
foreach ($photos as $photo) {
if (!empty($photo->image)) {
if (JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photo->image) {
if (!isSet($photocount))
$photocount = 1;
else
$photocount++;
$photoclass = "property_photo_main property_photo_main_" . $photocount;
//HTML...
}
}
}
This way $photoclass gets reset in each iteration, also $photocount gets incremented by 1 if it has been set. I also took the liberty of using PHP's internal structures like foreach or empty, you can read about the usage in the manual.
Since JPATH_ROOT.'/images/'.$row->id.'/medium/'.$photo->image has no logic and is not null/false, it will always return true, maybe you meant to use something like file_exists?
Using !$photoclass instead of !isSet($photoclass) will throw an undefined variable notice.
Related
So I want to include this code many times on one page with different data:
if (!empty($faq[$fisf][$title]) ) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image]) ) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title]) ) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
What I normally do is put this code in a separate file and then include it as many times as I need it and depending on where that code is in a loop or foreach it will show the relevant data!.
But I'm trying to figure out a better way of doing it, like using a function!
Kind of like this:
function faqmodal() {
if (!empty($faq[$fisf][$title])) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image])) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title])) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
}
I want this function to run within different foreach's and loading the relevant data to where its run.
faqmodal();
So this part will depend on what the foreach telling it to load it with :
$faq[$fisf][$title]
But now the function does not convert the code above to be relevant whats in the foreach loop is telling it to fill it with!
But this might not be the smartest way of doing it, I'm open for any ideas!
I'm not sure I understood correctly, this should be solvable by passing the data as an argument of the function like this:
function faqmodal($title, $image, $text){
if (!empty($title) && !empty($image) && if (!empty($text)) {
echo '
<div class="card card-body col-12">
<h5 class="card-title">'.$title.'</h5>
<img src="'.$image.'" alt="'.$title.'" class="img-fluid">
'.$text.'</div>'; //you might wanna think about moving the text into e.g. a <span> tag
}
}
If you now want to call the function you'll do it like so:
faqmodal($faq[$fisf][$title], $faq[$fisf][$image], $faq[$fisf][$title]);
And the data you pass will vary according to the data you get via the foreach loop. If this isn't what you mean, please let me know via a comment and we'll work our way through this.
I have Tried Many Solution , Inside and Outside PHP Code , None of them are working
I am able to fetch Image Path from Array But Not able to assign it ..
I Tried following solutions :
foreach ($data as $item) {
echo "Image Path is ".$item[0]["Image"] ; //--> Returning - img01.jpg
$imageNumber= "img01";
//sol 1.
echo '<img src="'.$item[0]["Image"]'">';
//sol 2.
// echo "<img src=\"{$imageNumber}.jpg\">";
//sol 3.
//echo '<img src="'.$coverlink.'" alt="Cover">';
}
//sol 4.
// <img src="<?php echo $imageNumber ?>.jpg">
//sol 5.
// <img src="<?php echo $item[0]["Image"]; ?>">
I need some guidence what I am missing ?Any Help would be appreciated
From what you've posted, it seems like $item[0]["Image"] returns the image name along with the extension. E.g: img01.jpg.
So, you can simply put this within the img src.
foreach ($data as $item) {
echo "<img src='".$item[0]["Image"]."' />";
}
If your question is explained clearly, then this should work just fine.
I have a straightforward if statement to evaluate whether or not an image is set to appear as the logo on a WordPress build. If it returns empty/false, it displays a default image whose location is set as an absolute value.
The problem is when an image ISN'T set, the else statement is failing. I'm not receiving an error, but the code returned is simply an image tag without any source i.e. "< img src >".
Here is the statement:
<?php
$logo = $wp_options['header_logo'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo['url']; ?>">
<?php } else { ?>
<img src="wp-content/themes/wpdev/images/logo.png">
<?php } ;?>
I guess that if statement is failing, because you are treating $logo variable as a string.
It seems you're using $logo variable as an array, if you want to check out if it is empty you can use is_null() function of PHP.
By the way, we can't understand your problem this way, you should be more specific. Share your error or warning messages, the way it's behaving, etc..
Found a solution to this by specifically referring to the url of the image array when the variable $logo was defined. This way, the IF is evaluating the URL of the image.
The problem seemed to be that the initial IF was being evaluated as TRUE but obviously not returning the URL as this wasn't originally specified in $logo. By looking for the URL in the 'header_logo' array, it corrected the problem.
<?php
$logo = $wp_options['header_logo']['url'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo; ?>">
<?php } else { ?>
<img src="<!-- Image Location -->">
<?php } ;?>
hi guys i am trying to do this like i have 5 frames (fancy border) and i have items list. when items load every item load different frame. when 5 frame done then 6th frame repeat frames list. below my script
<?php
$allgfts=mysql_query("select id,image_url from {$statement} order by id limit {$startpoint}, {$limit}");
while($gfts=mysql_fetch_array($allgfts))
{
$id=$gfts['id'];
$image=$gfts['image_url'];
?>
<div id="pic-1">
<div class="thumbnail-item">
<?php echo '<img src="images/'.$image.'" alt="" width="161" height="161" class="thumbnail g-size" />'; ?>
<span><?php echo 'Readmore';?></span>
<?php echo '<a class="gtbtn" href="g_buy.php?id='.$id.'">Get This</a>';?>
</div>
</div>
<?php
}
?>
I think you're asking how to echo a list of images, with that list wrapping to a new line every fifth item.
Given an array of results from a table (consider using PDO, by the way), I would do the following:
//$arr being the array
$x=1; //start counter
$list = '<ul>'; //using a list, because that's what it is
for($i=0;$i<count($arr);$i++) {
$list.='<li class="thumbnail-item">';
$thumb ='<a href="g_detail.php?id='.$arr[$i][id].'">';
$thumb.='<img src="images/'.$arr[$i][image_url].'" alt="" class="thumbnail g-size" /></a>';
$thumb.='<span><a href="g_detail.php?id='.$arr[$i][id].'>Readmore</a></span>';
$thumb.='<a class="gtbtn" href="g_buy.php?id='.$arr[$i][id].'">Get This</a>';
$list.=$thumb;
$list.='</li>';
if($x%5 == 0)||($x==count($arr)) {
$list.='</ul>';
if($x<count($arr)) {
$list.='<ul>';
}
}
$x++;
}
echo $list;
This is untested, but broadly speaking should work.
Use the remainder operator("%"). I don't know what your table structure looks like, but I am going to assume your product ID loads in sequential order, starting with 1.
In your WHILE loop, use the following:
$remainder = $id % 5;
if($remainder == 1){
//load my DIV with frame 1
}
else($remainder == 2){
//load my DIV with frame 2
}
......
I am trying to find picid in array but it's not working. When I echo it, nothing appears.
Here is my code
<?php $psql=mysql_query("select * from gallery where userId='$miid'");
$photo1 = array();
while($photo=mysql_fetch_assoc($psql)) {
$photo1[]=$photo;
print_r($photo);
}
foreach($photo1 as $k => $v){
if($v['picId']==$mipic){
$pic="uploads/".$v['photo'];
echo ">>>". $key=array_search($v['picId'],$photo1);
?>
NEXT
<img src="<?php echo $pic; ?>" width="300px" height="300px">
PREVIOUS
<?php
}
}?>
array_search is not recursive. $v exists in $photo1, while $v['picId'] only exists in $v.
That makes $key=array_search($v['picId'],$photo1) return false which, when you echo it, will print as nothing.
I am not sure why you are using array_search at all. In order to retrieve the next and previous picId, try this:
NEXT
<img src="<?php echo $pic; ?>" width="300px" height="300px">
PREVIOUS
Beware though that one of the hrefs is modules/gallery/miloader.php while the other is just miloader.php. So unless you actually have two different miloader.php files (one in each of the directories), one of them is wrong.