mysqli_fetch_array to php variable trash - php

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']);
}

Related

Get the image url using key after decoding json array into normal php array

I really have no idea how to do this. Can't figure it out. Any help would be great, please and thank you.
JSON code (it is stored in 'images' column in tblproducts table in the database)
{
"200x200":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-200x200-imaezt6hypjzhdug.jpeg",
"400x400":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-400x400-imaezt6hypjzhdug.jpeg",
"800x800":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-800x800-imaezt6hypjzhdug.jpeg",
"unknown":"http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-original-imaezt6hypjzhdug.jpeg"
}
I need to decode that json array into normal php array using json_decode function⁠⁠ and get the image url using key, fetch each of the image and display it in different tag.
<?php
$category_id = $_GET['category_id'];
$result = mysql_query("select * from tblproducts where category_id = '$category_id");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$json = $row['images'];
$image = var_dump(json_decode($json, true));
?>
<img src="<?php echo $image[0] ?>" alt=" " class="img-responsive" />
<img src="<?php echo $image[1] ?>" alt=" " class="img-responsive" />
<img src="<?php echo $image[2] ?>" alt=" " class="img-responsive" />
<?php } ?>
change your php code to iterate over the fetched array, and remove var_dump() from your code as
<?php
$category_id = $_GET['category_id'];
$result = mysql_query("select * from tblproducts where category_id = '$category_id");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$json = $row['images'];
$image = json_decode($json, true);
foreach($image as $key =>$val){
?>
<img src="<?php echo $val ?>" alt=" " class="img-responsive" />
<?php }
}
?>
After this
$image = var_dump(json_decode($json, true));
do
foreach($image as $key =>$val)
{?>
<img src="<?php echo $val ?>" alt=" " class="img-responsive" />
<?php }
you need to iterate using foreach on each value
Simply try this:
$obj = json_decode($json, true);
echo $obj['unknown'];
// http://img.fkcdn.com/image/mobile/p/s/u/lenovo-k6-power-k33a42-original-imaezt6hypjzhdug.jpeg
Working Example

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
}
?>

How do I show all the results from an SQL statement?

I wanted to display all of my shirts but I always get the same error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/36/d362778419/htdocs/home/specials.php on line 34
Here is the code I have currently in place
<?php
require "connect2.php";
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
$runrows = mysql_fetch_array($sql);
$title = $runrows['title'];
$picture = $runrows['picture'];
$newp = $runrows['newp'];
$date = strftime("%b %d, %Y %l:%M %p" ,strtotime($runrows['date']));
$oldp=$runrows['oldp'];
$viewl=$runrows['viewl'];
$shirtl = $runrows['shirtl'];
echo "";
?>
<div class="specialsListBoxContents centeredContent back" style="width:25%;"><div class="product-col" > <div class="img">
<a href="detail.php?id=<?php echo $id; ?>"><img src="<?php echo $picture; ?>" alt="<?php echo $title; ?>" title=" <?php echo $title; ?> " width="190"
height="160" /></a> </div> <div class="prod-info"> <div class="wrapper">
<div class="price"> <strong><span class="normalprice"><?php echo $oldp; ?
></span><br /><span class="productSpecialPrice"><?php echo $newp; ?></span></strong> </div>
<div class="button"><a href="detail.php?id=<?php echo $id; ?>"><img src="images/button_add_to_cart.gif" alt="Add to Cart" title=" Add to Cart " width="54"
height="49" /></a></div> </div> </div> </div></div>
<?php
?>
mysql_*() functions return boolean FALSE if they fail, which means your query call did not succeed. Add this code to find out the reason why:
$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
if ($sql === FALSE) {
die(mysql_error());
}
Here's some sample code:
<?php
$sql = mysql_query("SELECT * FROM shirts WHERE `all`='1'");
if (!$sql ) {
echo "Could not successfully run query from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($sql) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
while ($row = mysql_fetch_assoc($sql))
{
?>
<a href="detail.php?id=<?php echo $row['id']; ?>"><img src="<?php echo $row['picture']; ?>" alt="<?php echo echo $row['title']; ?>" title=" <?php echo $row['title']; ?> " width="190" height="160" />
<?php
}
?>
And here is more info. Notice the exiting php mode inside the while.

Quick php question about ordering row results

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

Categories