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.
Related
This question already has answers here:
Echo current number of row
(3 answers)
Closed 1 year ago.
I have a query that I make using PHP and it returns me all the rows that satisfied the condition that was given, now, I'm wondering if there's a way to count each number of row a result of a query has and pass that number to an id of an element on HTML?
<?php
$res2 = mysqli_query($con,"SELECT NAME, PRICE FROM product WHERE AVAILABLE = 1 AND ID_CATEGORY = 17");
?>
<body>
<?php
while($row = mysqli_fetch_row($res2)) {
print_r($row);
?>
<div class="item">
<img class="trj_img_cls" src="RSC/COMING_SOON.jpg" alt="" id="imgDG<?php ?>">
<div class="texto_trj">
<div class="nombre_producto">
<h2 id="ttlDG<?php ?>">título</h2>
</div>
<div class="precio_producto">
<i class="icon-dollar"></i>
<label id="prDG<?php ?>">0.00</label>
</div>
<div class="caracteristicas">
<ul id="caractDG<?php ?>"></ul>
</div>
</div>
</div>
<?php
}
?>
</body>
I put <?php ?> because there I want to add the variable (number of the row) so it complements the actual id and then i'm able to look for that id with javascript. Another thing is that I use the print_r() just to see if the elements were appearing.
This can be done by setting an $increment variable to 1 at the start of the while loop, and incrementing it at the end of the loop.
<?php
$increment = 1;
while($row = mysqli_fetch_row($res2)) {
And then using it like this:
id="imgDG<?php echo $increment; ?>">
And then incrementing it at the end:
<?php
$increment++;
}
?>
For parsing a list of articles, i have this code to parse all the articles:
while($article = $articles->fetch())
{
$date = strtotime($article['createdAt']);
$formatted_date = date("F Y",$date);
?>
<br />
<div class="news-content">
<div class="news-image">
<?php echo $article['title']; ?>
</div>
<div class="news-article">
<h3>
<span><?php $date = strtotime($article['createdAt']); echo /*date("F j",$date);*/ strftime('%e %B',$date) ?></span>
<br />
<?php echo $article['title']; ?>
</h3>
</div>
</div>
<?php
} //end while loop
?>
What i want to achieve: only the first 5 <div class="news-content">...</div> should be shown.
I know i have to do something with a for loop
but i do not know exactly how to use the for loop for this situation...
Can someone help me with that?
There are a lot of different ways to limit a loop. One possibility is to use a for loop instead of a while loop. for is often a good option if you want something to happen a specific number of times. Adding something else like fetch into the continuation condition will mean it happens up to a specific number of times.
for ($i = 0; $i < 5 && $article = $articles->fetch(); $i++) {
// output article
}
If you look at the jsfiddle you'll notice modules in yellow and red. I have all my code working except that I have to manually input the html/php for the modules.
http://jsfiddle.net/W6zYA/1/
My module code is:
<?php $row = mysql_fetch_row($result); ?>
<a href='#' data-reveal-id="<?php echo $row[0]; ?>" data-animation="none" >
<div class="grid_3">
<p id="contexttitle">
<?php echo $row[1]; ?>
<span style="float:right;"> <?php echo $row[3]; ?> </span>
</p>
<p id="accesssubmenu">Last Update: <?php echo $row[6]; ?> </p>
</div>
</a>
<div id="<?php echo $row[0]; ?>" class='reveal-modal'>
<h1> <?php echo $row[1]; ?> </h1>
<p> <?php echo $row[4]; ?> </p>
<p4>Last Update: <?php echo $row[6]; ?> </p4>
<a class="close-reveal-modal">×</a>
</div>
What I'm wanting to do is have it create X amount of these modules based on a SQL Query where I count the # of rows in each section (red, yellow, green).
I tried to store the php in the SQL database and after research found out that this is bad practice and shouldn't be done even when using eval() as there will be user input. I also read about PHP templates such as Smarty but wasn't certain if that was the correct solution for me.
If someone can point me in the right direction I'd greatly appreciate it I'm just not sure where to start/what to google.
mysql_fetch_row only fetches one row. If you have multiple rows you need mysql_fetch_assoc or mysql_fetch_array and use something like a foreach loop to loop through the results. This will output the html as many times as results in the MySQL query.
Try mysql_num_rows($result) that will give you the number of rows returned by the query. I would try to upgrade to PDO or mysql since the mysql_* functions are going to be deprecated in PHP 5.5
http://php.net/manual/en/function.mysql-num-rows.php
I assume, that you get multiple records from you database? That way you can use a while loop to loop trough your result.
while ($row = mysql_fetch_row($result)) {
// do you html code here.
}
That should do the trick.
Yes, I've looked around and yes I've used mysql_fetch_array hundreds of times successfully before. I want a php file that will output an html file with some constants and a few interpreted php variables. After an hour, this is where I'm at.
<?php
require "(this is sql statement to get all rows from the db).php";
$i = 1;
while($row = mysqli_fetch_array($results_getpieces) && $i < 11 ){ ?>
<li>
<a href="#image-1">
<img src="<?php echo $row['content']; ?>" alt="image01">
<span><?php echo $row['title']; ?></span>
</a>
<div class="lb-overlay" id="image-1">
<img src="<?php echo $row['thumb']; ?>" alt="image01" />
<div>
<h3>blahblah <span>bluelue</h3>
<p>sherpas don't dance</p>
Prev
Next
</div>
x Close
</div>
</li>
<?php
$i++;
}
?>
When I display the $row results in a table, there's no problem. Before I added that i++ business, I could get the first record from my results, but it would just repeat those results for all ten instances.
Help almighty stackers, and overflow me with your knowledge. Please. I can't go to sleep until I get this. Where am I going wrong?
You need to parenthesize the condition appropriately, so that the array that you get back from the function call doesn't get "and"-ed with the condition "$i < 11":
($row = mysqli_fetch_array($results_getpieces)) && $i < 11
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.