Quick php question about ordering row results - php

Consider the following :
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = $result-1;
while ($resultf>=0){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf--; }
This currently prints images one after the other. All I need to do is to invert the order in which these images are printed to the user. I am not sure where, or how I would insert something similar to ORDER by in this code? Thanks in advance!

Just loop the other way:
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = 0;
while ($resultf<$result){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf++; }

Maybe you can use array_reverse

Related

How to add else into PHP Code line?

I have this following code
<td>
<?php if($rec->telegram){ ?><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?>
<?php if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?>
<?php if($rec->instagram){ ?><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?>
<?php if($rec->facebook){ ?><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?>
</td>
I would like to add "else" into this code that if ALL those icons facebook, instagram, google plus and facebook weren't available then it will show another image file like "nophoto.jpg"
Note: If all those icons (4 icons) weren't available then it will show nophoto.jpg
I highly appreciate if someone guide me how to add else into above code.
Regards
It'll be easier to just add an extra if:
if (!($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus)) { //etc
But really you want to attach that kind of logic to the $rec object, so that it's neatly separated and it's easier to add new types of social media in the future. Hopefully your $rec object is indeed backed by a class and not just a stdClass from a database result or cast.
public function hasNoSocialIcon() {
return !($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus);
}
And then use that in your template:
<?php if ($rec->hasNoSocialIcon()) { ?>
<img class="social" src="<?php echo base_url(); ?>social/nophoto.jpg" />
<?php } ?>
You should take the use of PHP's conditional operators like this:
<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) { ?>
<a href="default-pic.jpg">
<img class="social" src="default-pic.jpg" />
</a>
<?php } else { ?>
<?php $url = echo base_url() . 'social/telegram.jpg'; ?>
<a href="<?php echo $url; ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/google_plus.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/instagram.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/facebook.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php } ?>
</td>
Hope this helps!

mysqli_fetch_array to php variable trash

I upload 4 photos in a folder, and the names of the photos on mysql. Then i "SELECT" the names of the files, and put them in the array picArray as shown. But when i try to echo the contents, although i take the right names, it seem that they cant be used as a directory path.
Here is the code:
$con=mysqli_connect("localhost","root","mypass","fixit");
$con->set_charset("utf8");
$repId=$repId+1;
$qr = mysqli_query($con,"SELECT * FROM photos WHERE report='$repId'");
$picArray = Array();
while( $row = mysqli_fetch_array($qr) ) {
$picArray[] = $row['name'];
}
?>
<div>
<img id="img1" src="uploads/<?php echo $repId ?>/<?php echo $picArray[0] ?>" />
<img id="img2" src="uploads/<?php echo $repId ?>/<?php echo $picArray[1] ?>" />
</div>
<div>
<img id="img3" src="uploads/<?php echo $repId ?>/<?php echo $picArray[2] ?>" />
<img id="img4" src="uploads/<?php echo $repId ?>/<?php echo $picArray[3] ?>" />
</div>
What i should mention, is that when i:
echo $picArray[0];
echo $picArray[1];
echo $picArray[2];
echo $picArray[3];
i get:
rafiki.png rafiki2.png
rafiki3.pngrafiki4.png
which means that the array has newline and spaces on it... Why is this happening?
There are probably new lines stored with the picture names in the database this is a fix but you should clean up you data
while( $row = mysqli_fetch_array($qr) ) {
$picArray[] = trim($row['name']);
}

Change image without reloading page

I am trying to figure out how to change the photo without the page refreshing. I have seen some of the example but just cannot figure out how to implement it in to my working page.
This is what I have right now:
<div id="propertyDetailsImage">
<img class="image photo" src="<?php echo $property->photos->photo[$mainPhoto - 1]->url; ?>" width="<?php echo $property->mainPhotoWidth * 0.77 ?>" height="<?php echo $property->mainPhotoHeight * 0.77 ?>" alt="<?php echo $property->address->full; ?>"/>
</div>
<div class="photoPosition">
<?php
$previousPhoto = $mainPhoto - 1;
if($previousPhoto == 0) {
$previousPhoto = $property->totalPhotos;
}
$nextPhoto = $mainPhoto + 1;
if ($nextPhoto > $property->totalPhotos) {
$nextPhoto = intval(1);
}
?>
<img src="images/previous.png" alt="Previous photo" height="12" width="13" border="none"/>
<span id="photoPosition"><?php echo $mainPhoto; ?></span> of <?php echo $property>totalPhotos; ?>
<img src="images/next.png" alt="Next photo" height="12" width="13" border="none" />
</div>
</div>
<div class="col-md-6">
<div id="thumbnails">
<h3 class="additional">Photos</h3>
<?php
// Iterate throught the list of photos
foreach($property->photos->photo as $photo) {
?>
<script type="text/javascript">
addPhoto(
<?php echo $photo->id; ?>,
<?php echo $photo->width; ?>,
<?php echo $photo->height; ?>,
"<?php echo $photo->caption; ?>");
</script>
<img src="<?php echo $photo->url; ?>" width="<?php echo $photo->widthSmall; ?>" height="<?php echo $photo->heightSmall; ?>" class="image photo" id="photo<?php echo $photo->position; ?>" alt="Additional Photo of <?php echo $photo->address->advertising; ?>" onclick="return showPhoto(<?php echo $photo->position; ?>)" />
<?php }
?>
Any help is appreciated. Cheers
Dima
You should use ajax to get like this results.
you should go here,
The image slider shown in this demo is for free.
For detailed instructions, please visit online
http://www.menucool.com/slider/javascript-image-slider-demo1

Passing multiple arrays to for each loop..i think

I love jQuery and dont really understand php.
I am making a slider with albums. So far so good except I am now stuck trying to add two arrays into one for each loop. At least I think that is the best solution.
In my code you can see I have achieved what I need by hard coding 6 images for the 6 custom fields in the custom post type (I am using wordpress). The problem is that if there are not six images then the slider show a blank image (as it exists but doesnt have a src). I tried removing the element with jquery but that was no good. Here is the code I have so far, perhaps there is something i am missing, I just cant seem to get the logic quite right.
<?php
$args = array(
'post_type' => 'albums_gallery',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$album_name = get_the_ID();
$image_1 = get_field('image_1');
$image_1_url = $image_1['url'];
$image_1_caption = get_field('image_1_caption');
$image_2 = get_field('image_2');
$image_2_url = $image_2['url'];
$image_2_caption = get_field('image_2_caption');
$image_3 = get_field('image_3');
$image_3_url = $image_3['url'];
$image_3_caption = get_field('image_3_caption');
$image_4 = get_field('image_4');
$image_4_url = $image_4['url'];
$image_4_caption = get_field('image_4_caption');
$image_5 = get_field('image_5');
$image_5_url = $image_5['url'];
$image_5_caption = get_field('image_5_caption');
$image_6 = get_field('image_6');
$image_6_url = $image_6['url'];
$image_6_caption = get_field('image_6_caption');
?>
<div class="album album_<?php echo $album_name ?>">
<div class="slider-wrapper theme-default">
<div class="slider" class="nivoSlider">
<!--<img src="<?php echo $image_1['url']; ?>" alt="<?php echo $image_1['alt']; ?>" title="<?php echo $image_1_caption; ?>" />
<img src="<?php echo $image_2['url']; ?>" alt="<?php echo $image_2['alt']; ?>" title="<?php echo $image_2_caption; ?>" />
<img src="<?php echo $image_3['url']; ?>" alt="<?php echo $image_3['alt']; ?>" title="<?php echo $image_3_caption; ?>" />
<img src="<?php echo $image_4['url']; ?>" alt="<?php echo $image_4['alt']; ?>" title="<?php echo $image_4_caption; ?>" />
<img src="<?php echo $image_5['url']; ?>" alt="<?php echo $image_5['alt']; ?>" title="<?php echo $image_5_caption; ?>" />
<img src="<?php echo $image_6['url']; ?>" alt="<?php echo $image_6['alt']; ?>" title="<?php echo $image_6_caption; ?>" />-->
<?php
$images = array("$image_1_url","$image_2_url","$image_3_url","$image_4_url", "$image_5_url", "$image_6_url");
foreach ($images as $image) {
if ($image != "") {
echo "<img src='";
echo $image;
echo "' ";
echo "title='caption'";
echo "/>";
}
};
?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
I need to add the image caption, and probably a link into the for each and if statements, little out of my depth being a designer.
Thanks for any help.
This is all you need
<div class = "slider-wrapper theme-default">
<div class = "slider" class = "nivoSlider">
<?php
$images = Array();
for($i = 1; $i <= 6; $i++) {
$image = get_field("image_{$i}");
if(!$image || !$image['url']) {
break;
}
$caption = get_field("image_{$i}_caption");
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $caption; ?>" />
<?php
}
?>

Show specific logo for user group in Joomla 3

I can't seem to see a logical way to do this, I've tried many different functions, some of which i get blank page errors, other which it seems to work but just skips and goes to the else function everytime.
In a nutshell, I'm trying to have it so that certain logos will be displayed depending on the User Group ID.
At the moment the code below is producing a blank error page, and I can't see why.
Could anyone help me with this? Joomla 3.1 by the way.
<?php $user = JFactory::getUser();
$usergroup=$user->getAuthorisedGroups();
if ($usergroup == '10') : ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="/images/fordlogo.png" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php elseif ($usergroup == '7') : ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="/images/tescologo.png" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php else; ?>
<a href="<?php echo JURI::root(); ?>" id="gkLogo">
<img src="<?php echo $logo_image; ?>" alt="<?php echo $this->API->getPageName(); ?>" />
</a>
<?php endif; ?>
Try something like this,
$user = JFactory::getUser();
$usergroup = $user->getAuthorisedGroups();
if(in_array('10',$usergroup)){
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}elseif(in_array('7',$usergroup)){
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}else{
echo '<a href="'.JURI::root().'" id="gkLogo">
<img src="/images/fordlogo.png" alt="'.$this->API->getPageName().'" />
</a>';
}
Hope this will help you.

Categories