Jump to content

ph mysql - help with do/while and incrementing index


azukah

Recommended Posts

i have a slider that shows 2 images at a time.

i did a query and got all my images names from the database to be in an array so i can go form index zero to the last image incrementing by +1 but not working.

what it is doing now is displaying index[0] and index[1] in the first set of 2 images

in the second set is displaying index[1] and index [2]

in the third set is displaying index[2] and index[3] and so on...

 

here's my query

require_once('connections.php');
mysql_select_db($dbname, $db);
$sql = "SELECT *  
FROM table WHERE image IS NOT NULL LIMIT 6"; //limiting to six for testing only, will remove afterwords
$results = mysql_query($sql, $db) or die(mysql_error());
$row = mysql_fetch_assoc($results);
//
$result2 = mysql_query("SELECT *  FROM table WHERE image IS NOT NULL LIMIT 6");
$storeArray = Array();
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    $storeArray[] =  $row2['image'];  
}
?>

 

here's the html/php

<?php $index = 0;?>              
<?php do { ?>                
<div class="slide">
<div class="slidePromo">
<div class="promoImg"><a href="#"><img src="<?php echo $storeArray[$index];?>"/></a> </div>
</div>        
<!--end slide-->

<div class="slidePromo">
<div class="promoImg"><a href="#l"><img src="<?php echo $storeArray[++$index];?>"/></a></div>
</div>        
</div>
<!--end slide-->
<?php } while ($row = mysql_fetch_assoc($results)); ?>  

Link to comment
Share on other sites

For each result, you are outputting that result and the next one. You are also being redundant with the do/while loop, you end up selecting the data multiple times.

 

You will want to get all of the results into an array *(you've already done that) and then loop through it (using for), that way you can increment the iterator manually. You could also use foreach but it might be trickier given what you're trying to do.

Link to comment
Share on other sites

require_once('connections.php');
mysql_select_db($dbname, $db);
$result2 = mysql_query("SELECT *  FROM table WHERE image IS NOT NULL LIMIT 6");
$storeArray = Array();
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
?>
<div class="slide">
<div class="slidePromo">
<div class="promoImg"><a href="#"><img src="<?php echo $row2['image'];?>"/></a> </div>
</div>        
<!--end slide-->

<div class="slidePromo">
<div class="promoImg"><a href="#l"><img src="<?php echo $row2['image'];?>"/></a></div>
</div>        
</div>
<!--end slide-->
<?php 
}
?>

 

No idea why you've got multiple queries and then putting it into an array. Either way that should sort your issue...

Link to comment
Share on other sites

In the above example, I don't see $row ever defined.

 

It is not going to be easy to do it while you're selecting the data, you want to select the data then loop through it.

 

Thank you for pointing that out, was a typo. I just copied his code as you can see and made a few quick adjustments.

 

Regarding your second statement, there's no reason not to loop through the data as per the example. From what I understand his issue is, the example is a logical solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.