doing a mysql_query with while loop inside foreachloop in php - php

Hi am trying to go through a list of xml results and I am taking the id from these results and performing a query in my database to get more information. I have placed this into a foreach loop and then I have my while loop getting the mysql results. Everything works until I do the while loop. My page goes blank and I get no errors.. ANY help would be so appreciated!
Here is my code:
foreach($data->HotelList->HotelSummary as $info):
$hotelId = $info->hotelId;
$title=$info->name;
?>
<!---------------------------------------------------------------------->
<!-----------------Listed hotel results div----------------------------->
<!---------------------------------------------------------------------->
<div class="listResults">
<div class="hotelListing">
<div class="hotelThumb">
<?php
//----------Getting thumb url from database by HotelId-------
$getImages = mysql_query("SELECT Caption FROM HotelImages WHERE ID = '4110'") or die(mysql_error());
while($r=$mysql_fetch_array($getImages)):
$img = $r['Caption'];
?>
<img src="" width="200" height="180" alt="test image" class="thumb" />
<?php endwhile; ?></div>
<?php endforeach; ?>
Just as a note, I have tried to get num_rows and I do get the correct result. SO the query is executing, but something is happening in the while loop.

Your while loop is bad. Take out the $ in front of the function.
while($r=mysql_fetch_array($getImages)):

Your main problem is that you put a $ in front of a method call ($mysql_fetch_array()). It should just be mysql_fetch_array().
An important issue that you shouldn't overlook is that you have a static query being called within a loop. Perform the query outside of your loop, since the results will never change. You can then store the results in an array and iterate through the array within your loop. This will significantly improve your code's performance.
<?php
//----------Getting thumb url from database by HotelId-------
$getImages = mysql_query("SELECT Caption FROM HotelImages WHERE ID = '4110'") or die(mysql_error());
$images = array();
while($img = mysql_fetch_array($getImages)) {
$images[] = $img['Caption'];
}
foreach($data->HotelList->HotelSummary as $info):
$hotelId = $info->hotelId;
$title=$info->name;
?>
<!---------------------------------------------------------------------->
<!-----------------Listed hotel results div----------------------------->
<!---------------------------------------------------------------------->
<div class="listResults">
<div class="hotelListing">
<div class="hotelThumb">
<?php
foreach($images as $img) :
?>
<img src="" width="200" height="180" alt="test image" class="thumb" />
<?php endforeach; ?></div>
<?php endforeach; ?>

while($r=$mysql_fetch_array($getImages)):
$img = $r['Caption'];
?>
You have a $ before the mysql_fetch_array(), removing that should fixed your problem.

Related

Change part of URL string for images with PHP

In my MySQL database, the image links are saved in the format like http://www.old.com/image1.jpg. But I had to change the domains of the images and the new links appear like images.new.com/image1.jpg. I have been changing the images links with jQuery with the following function:
$(document).ready(function() {
$('img').each(function() {
var src = $(this).attr('src');;
$(this).attr('src', src.replace('www.old', 'images.new'));
});
});
But I am wondering is there any way to change part of the URL strings with PHP. I am getting the image URLs with the following PHP function.
<?php
$imageQuery = $mysqli->query("SELECT imageURL FROM images WHERE album = 'UK' ORDER BY date ASC");
$images = $imageQuery->fetch_all(MYSQLI_ASSOC);
$images = array_chunk($images, 2);
?>
<div class="row">
<div class="col-4" id="box1">
<?php foreach (array_column($images, 0) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
<div class="col-4" id="box2">
<?php foreach (array_column($images, 1) as $image): ?>
<img class="img-fluid" src="<?= $image['imageURL']; ?>">
<?php endforeach; ?>
</div>
</div>
With PHP how can I echo the new links for the images directly here in the img src? <img class="img-fluid" src="<?= $image['imageURL']; \\modified link here ?>">
PHP has it's own method for replacing strings, str_replace. The equivalent to your jQuery in PHP is:
str_replace('www.old', 'images.new', $image['imageURL'])
A better idea would be to update the values in your database.
An even better idea would be to not store the duplicate root URLs anywhere, and stitch them together in the application if you need absolute URLs, for some reason.
You could alternatively convert the text in the sql select statement:
SELECT REPLACE(imageURL, 'www.old', 'images.new') AS imageURL FROM ...
By using AS imageURL none of the rest of the code would need to change.

FuelPHP- Using Asset::img() with images fetched from database by for loop

<?php foreach($works as $work) : ?>
<?php echo Asset::img('project-icons/icon/$work->cover_img', array('class'=>'img-responsive', 'alt'=>'...')); ?>
<?php endforeach; ?>
I am using FuelPHP to build a portfolio website. PHP v5.6
This is the "works" section where images and details of a "work" are fetched from database. Using foreach loop. I want to get the "cover_img" (which is the image name), inside the Asset::img(..)
How to do this? what to put in place of $work->cover_img?
Edit: I was able to get the result by using this:
<img src="<?php echo Uri::base(false); ?>/assets/img/project-icons/icon/<?php echo $work->cover_url; ?>" class="img-responsive" alt="..." />
Can this be achieved by using Asset::img() instead of Uri::base()?
Can't you just do:
<?php
echo Asset::img('project-icons/icon/'.$work->cover_img, array('class'=>'img-responsive', 'alt'=>'...'));
?>
...?

Using if-statements in PHP to print HTML

I'm fairly new to PHP and I've been trying to construct some code to print basic HTML, however the code causes an error 500 whenever used. I am guessing it is a syntax error since I've tried the code in a couple of forms and nothing seems to work (including removing the database lookup and just trying to compare to set values to each other). The script needs to get a variable from the db, compare it to a set value and print the HTML if true, here is the code I am trying:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult(); ?>
<?php if strcmp($category,"Blog")==0 : ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php endif; ?>
Any help will be appreciated, thanks!
You if is incorrect, try like this
<?php if (strcmp($category,"Blog")==0) { ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php } ?>

Loading picture from database to jquery slider using php

I am trying to load pictures from database to jquery slider
infact what is stored at database is the name of the picture
i implemented this code, it's working without errors but it showing me nothing at slider all is empty
<div id="slider">
<?php
while ($result = mysqli_fetch_object($banner)):
?>
<img src="images/banner/<? $banner['picture'];?>/" width="950" height="400"alt="" title="<strong><? echo $banner['title'];?></strong><span><? echo $banner['description'];?> </span>" />
<?php
endwhile
?>
</div>
name of the table at database is banner in which i have id(INT), picture(varchar 50) title(varchar(100), description(longblob)
query is working and returning number of selected rows
but nothing is shown
You need to echo the result rather than just use the variable...
<div id="slider">
<?php
while ($result = mysqli_fetch_object($banner)):
?>
<img src="images/banner/<?php echo $banner['picture'];?>" width="950" height="400" alt="" title="<strong><? echo $banner['title'];?></strong><span><?php echo $banner['description'];?></span>" />
<?php
endwhile;
?>
</div>

Two identical while loop functions, the second doesn't work

I've got a problem with my php script. The script generates two <ul>'s, one with the images, another with the thumbnails. The php runs through the first while loop perfectly, but my <ul> for thumbnails always comes back empty. I've been working through it to no luck, and I can't seem to get the script to print any errors. Help?
include "../backend/connect.php";
$query = "select image_id from images where thumb != '';";
$result = mysql_query($query);
<div id="main">
<div id="container">
<ul>
<?php
while ($row = mysql_fetch_array($result)) {
$img_id = $row['image_id'];
?>
<li class="hidden" id="<?php print($img_id); ?>" style="background-image:url(/pull.php?id=<?php print($img_id);?>&thumb=false)"></li>
<?php } ?>
<li class="" id="focus"></li>
</ul>
</div>
</div>
<div id="side">
<div id="photos"><ul>
<?php
while ($row = mysql_fetch_array($result)) {
$img_id = $row['image_id'];
?>
<div class="imgDiv" id="<?php print($img_id); ?>" >
<li>
<img src="pull.php?id=<?php print($img_id);?>&thumb=true" class="thumbnail"/>
</li>
</div>
<?php
}
?>
</ul></div>
</div>
</div>
you're using a while loop to loop through the results, you cant do it twice.
Call mysql_data_seek($result, 0); to reset the internal pointer back to the first record after your first time through the results to start over.
It sounds like maybe you read all the rows in the first loop, and there's nothing left to read by the time you get to the second loop :)
Your first while loops until mysql_fetch_array($result) is false/empty. You never assign a new value to $result, so when you get to your second while it skips the loop.
If you want to iterate through the results twice, dump them into an array and use foreach to iterate through it.
The first time you loop through the results, you are actually clearing the $result object. You could possibly loop through once doing something like:
$image_ids = array();
while ($row = mysql_fetch_array($result)) {
array_push($img_ids, $row['image_id']);
}
?>
and then loop through that array while generating your code.

Categories